Invalid getReturnTypeSourceRange for function with trailing return type
| Bugzilla Link | 39567 |
| Version | trunk |
| OS | Linux |
| CC | @ymand |
Extended Description
Given this source file:
int foo();
auto bar() -> int;
and this clang-tidy check:
void FuncReturnRangeCheck::registerMatchers(MatchFinder *Finder) { // FIXME: Add matchers. Finder->addMatcher(functionDecl().bind("x"), this); }
void FuncReturnRangeCheck::check(const MatchFinder::MatchResult &Result) {
// FIXME: Add callback implementation.
const auto *MatchedDecl = Result.Nodes.getNodeAs
MatchedDecl->dump();
auto SR = MatchedDecl->getReturnTypeSourceRange();
SR.dump(*Result.SourceManager); }
we get this output:
FunctionDecl 0x55dc893341f0 <func-return.cpp:2:1, col:9> col:5 foo 'int (void)'
func-return.cpp:2:1
FunctionDecl 0x55dc89334350 <func-return.cpp:4:1, col:15> col:6 bar 'auto (void) -> int'
<
The second source range should not be invalid.
FunctionDecl::getReturnTypeSourceRange returns an invalid range when the return type is after the name info.
I don't have a suggestion, sorry. I know lots of source locations which are interesting for tooling.
My hope is that by making it easier to paste a link showing that, that more locations can be added to the AST API. The modernize-use-override check is also doing mad things because the locations of the virtual and override keywords are not semantically represented:
http://ec2-52-14-16-249.us-east-2.compute.amazonaws.com:10240/z/W-KmLT
This bug is independent of the exact use-case of readability-const-return-type of course and should be fixed regardless.
Unfortunately, the typeloc doesn't include the qualifiers, which is crucial for this check. E.g. http://ec2-52-14-16-249.us-east-2.compute.amazonaws.com:10240/z/BuanPD
notice that the const qualifier is never highlited. Do you have a suggestion which would encompass the entirety of the type?
Yes, working on a patch to use that now (a number of folks pointed me to your blog post, actually).
Very much looking forward to your clang-query and godbolt changes landing upstream. :)
The
getReturnTypeSourceRange
API
should just be a shortcut for
getTypeSourceInfo()->getTypeLoc().getAsclang::FunctionTypeLoc().getReturnLoc().getSourceRange()
No need to jump through hoops to get it.
See
http://ec2-52-14-16-249.us-east-2.compute.amazonaws.com:10240/z/XNmGqB
and
https://steveire.wordpress.com/2018/11/11/future-developments-in-clang-query/
if you were unaware of it.
+1
I ran into this when writing the check readability-const-return-type. Because of this, the check doesn't support trailing returns and, generally, jumps through hoops to guess the range of the return type:
https://reviews.llvm.org/source/clang-tools-extra/browse/clang-tools-extra/trunk/clang-tidy/readability/ConstReturnTypeCheck.cpp;346856$34
Once this is fixed, we can clean up that code and (simultaneously) add support for trailing return types.
Bump from years ago, it seems there's still no solution to this.
I've run into this while trying to write a clang-tidy plugin that fixes up return types. It's trivial to create fixups for old-fashioned returns, but I'm not sure how to get the source range for trailing returns in a supported way.
Without a solution, I'm unsure how I can replace the return type of a lambda.
I see hasTrailingReturn, for which I would expect an accompanying getTrailingReturnTypeSourceRange, but it seems none exists.
Maybe I'm missing something that's been added in the last few years?