-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][refactor] Refactor findNextTokenIncludingComments
#123060
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
005a730
c1f4603
d256171
66acd22
7df85b4
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -118,47 +118,19 @@ findMembersUsedInInitExpr(const CXXCtorInitializer *Initializer, | |||||||||
return Results; | ||||||||||
} | ||||||||||
|
||||||||||
/// Returns the next token after `Loc` (including comment tokens). | ||||||||||
static std::optional<Token> getTokenAfter(SourceLocation Loc, | ||||||||||
const SourceManager &SM, | ||||||||||
const LangOptions &LangOpts) { | ||||||||||
if (Loc.isMacroID()) { | ||||||||||
return std::nullopt; | ||||||||||
} | ||||||||||
Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts); | ||||||||||
|
||||||||||
// Break down the source location. | ||||||||||
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); | ||||||||||
|
||||||||||
// Try to load the file buffer. | ||||||||||
bool InvalidTemp = false; | ||||||||||
StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp); | ||||||||||
if (InvalidTemp) | ||||||||||
return std::nullopt; | ||||||||||
|
||||||||||
const char *TokenBegin = File.data() + LocInfo.second; | ||||||||||
|
||||||||||
Lexer lexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(), | ||||||||||
TokenBegin, File.end()); | ||||||||||
lexer.SetCommentRetentionState(true); | ||||||||||
// Find the token. | ||||||||||
Token Tok; | ||||||||||
lexer.LexFromRawLexer(Tok); | ||||||||||
return Tok; | ||||||||||
} | ||||||||||
|
||||||||||
/// Returns the end of the trailing comments after `Loc`. | ||||||||||
static SourceLocation getEndOfTrailingComment(SourceLocation Loc, | ||||||||||
const SourceManager &SM, | ||||||||||
const LangOptions &LangOpts) { | ||||||||||
// We consider any following comment token that is indented more than the | ||||||||||
// first comment to be part of the trailing comment. | ||||||||||
const unsigned Column = SM.getPresumedColumnNumber(Loc); | ||||||||||
std::optional<Token> Tok = getTokenAfter(Loc, SM, LangOpts); | ||||||||||
std::optional<Token> Tok = | ||||||||||
Lexer::findNextToken(Loc, SM, LangOpts, /*IncludeComments=*/true); | ||||||||||
Comment on lines
+128
to
+129
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.
Suggested change
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. We can do that, right? |
||||||||||
while (Tok && Tok->is(tok::comment) && | ||||||||||
SM.getPresumedColumnNumber(Tok->getLocation()) > Column) { | ||||||||||
Loc = Tok->getEndLoc(); | ||||||||||
Tok = getTokenAfter(Loc, SM, LangOpts); | ||||||||||
Tok = Lexer::findNextToken(Loc, SM, LangOpts, /*IncludeComments=*/true); | ||||||||||
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.
Suggested change
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 don't think clang-reorder-fields is supposed to depend on stuff from 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. yeah, i think it's not worth it (adding a dependency on clang-tidy for the sake of findNextTokenIncludingComments seems like overkill). 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. Agreed actually, thanks |
||||||||||
} | ||||||||||
return Loc; | ||||||||||
} | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like just a better formatted
findNextTokenIncludingComments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's why we wanted to get rid of that code, it does not really make sense to have 3 quasi-identical copies of the same code.