[clang] [clang-tools-extra] [clang][refactor] Refactor `findNextTokenIncludingComments` (PR #123060)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 16 00:30:12 PST 2025
================
@@ -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);
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);
----------------
cor3ntin wrote:
```suggestion
Tok = findNextTokenIncludingComments(Loc, SM, LangOpts);
```
https://github.com/llvm/llvm-project/pull/123060
More information about the cfe-commits
mailing list