-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Correct FixIt ranges for unused capture warnings #141148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0db205b
2e67f4c
00f1e7d
357995f
e721495
2f769f0
1e1872f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,28 @@ SourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) { | |
return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts); | ||
} | ||
|
||
SourceRange | ||
Sema::getRangeForNextToken(SourceLocation Loc, bool IncludeMacros, | ||
bool IncludeComments, | ||
std::optional<tok::TokenKind> ExpectedToken) { | ||
if (!Loc.isValid()) | ||
return SourceRange(); | ||
std::optional<Token> NextToken = | ||
Lexer::findNextToken(Loc, SourceMgr, LangOpts, IncludeComments); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use findLocationAfterToken here instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using this one function for two purposes:
It seemed like a reasonable approach, and given we're producing a fixit diagnostic I don't think the perf matters. Basically what I'm trying to do is say given an unused capture: auto foo = [a, unused, b] ... // just a pile of white space for illustrative purposes
auto bar = [a, unused, /* ... */ b] ...
auto wibble = [a, /* ... */ unused, b] ...
auto thingy = [a, unused /* ... */, b] ... produce fixits that result in auto foo = [a, b] ... // just a pile of white space for illustrative purposes
auto bar = [a, /* ... */ b] ...
auto wibble = [a, b] ...
auto thingy = [a, b] ... and for that what we want to do is find the end of the comma, and the start of the next token, simply using the "findNextToken" to just get those tokens seems reasonable. Of course as we discussed in discord, that function doesn't handle macros, and handling this correctly for macros would imply the Sema "wrapper" excludes macro wrappers (so would not just be a wrapper) or every user has to check for macros. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cor3ntin ping for your copious spare time :D |
||
if (!NextToken) | ||
return SourceRange(); | ||
if (ExpectedToken && NextToken->getKind() != *ExpectedToken) | ||
return SourceRange(); | ||
SourceLocation TokenStart = NextToken->getLocation(); | ||
SourceLocation TokenEnd = NextToken->getLastLoc(); | ||
if (!TokenStart.isValid() || !TokenEnd.isValid()) | ||
return SourceRange(); | ||
Comment on lines
+101
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check TokenEnd.isMacroID() ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup! But that kind of results in this no longer being just a wrapper so I'm not sure if that's "good" - I might make it a flag? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cor3ntin thoughts? basically should the Lexer function take a "allow tokens that intersect macros", should the Sema wrapper, or should - because of use case - the Sema wrapper always exclude macros? I don't particularly like the last option as it means the Sema and Lexer versions of the same function name behave differently There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cor3ntin I've added a "include macros" parameter and removed the default parameters because I think that given the lack of existing usages there's no real reason for the default args, and they present a hazard. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks reasonable |
||
if (!IncludeMacros && (TokenStart.isMacroID() || TokenEnd.isMacroID())) | ||
return SourceRange(); | ||
|
||
return SourceRange(TokenStart, TokenEnd); | ||
} | ||
|
||
ModuleLoader &Sema::getModuleLoader() const { return PP.getModuleLoader(); } | ||
|
||
DarwinSDKInfo * | ||
|
Uh oh!
There was an error while loading. Please reload this page.