[PATCH] D133725: Searching for tokens including comments
Bartłomiej Cieślar via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 12 13:45:31 PDT 2022
barcisz created this revision.
Herald added a project: All.
barcisz requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
This is a small diff that adds an optional argument to the Lexer::findNextToken that allows for searching for the token while including comments (instead of having to
instantiate the lexer separately)
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D133725
Files:
clang/include/clang/Lex/Lexer.h
clang/lib/Lex/Lexer.cpp
clang/unittests/Lex/LexerTest.cpp
Index: clang/unittests/Lex/LexerTest.cpp
===================================================================
--- clang/unittests/Lex/LexerTest.cpp
+++ clang/unittests/Lex/LexerTest.cpp
@@ -622,6 +622,24 @@
"xyz", "=", "abcd", ";"));
}
+TEST_F(LexerTest, FindNextTokenWithComments) {
+ Lex("int /* type */ abcd = 0; // vardecl\n"
+ "int /*other type*/ xyz = abcd; //other vardecl \n");
+ std::vector<std::string> GeneratedByNextToken;
+ SourceLocation Loc =
+ SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
+ while (true) {
+ auto T = Lexer::findNextToken(Loc, SourceMgr, LangOpts, true);
+ ASSERT_TRUE(T);
+ if (T->is(tok::eof))
+ break;
+ GeneratedByNextToken.push_back(getSourceText(*T, *T));
+ Loc = T->getLocation();
+ }
+ EXPECT_THAT(GeneratedByNextToken, ElementsAre("/* type */", "abcd", "=", "0", ";", "// vardecl", "int", "/*other type*/",
+ "xyz", "=", "abcd", ";", "//other vardecl "));
+}
+
TEST_F(LexerTest, CreatedFIDCountForPredefinedBuffer) {
TrivialModuleLoader ModLoader;
auto PP = CreatePP("", ModLoader);
Index: clang/lib/Lex/Lexer.cpp
===================================================================
--- clang/lib/Lex/Lexer.cpp
+++ clang/lib/Lex/Lexer.cpp
@@ -1258,7 +1258,8 @@
Optional<Token> Lexer::findNextToken(SourceLocation Loc,
const SourceManager &SM,
- const LangOptions &LangOpts) {
+ const LangOptions &LangOpts,
+ bool IncludeComments) {
if (Loc.isMacroID()) {
if (!Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc))
return None;
@@ -1274,11 +1275,13 @@
if (InvalidTemp)
return None;
- const char *TokenBegin = File.data() + LocInfo.second;
+ const char *const TokenBegin = File.data() + LocInfo.second;
// Lex from the start of the given location.
Lexer lexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
TokenBegin, File.end());
+ lexer.SetCommentRetentionState(IncludeComments);
+
// Find the token.
Token Tok;
lexer.LexFromRawLexer(Tok);
Index: clang/include/clang/Lex/Lexer.h
===================================================================
--- clang/include/clang/Lex/Lexer.h
+++ clang/include/clang/Lex/Lexer.h
@@ -554,7 +554,8 @@
/// Returns the next token, or none if the location is inside a macro.
static Optional<Token> findNextToken(SourceLocation Loc,
const SourceManager &SM,
- const LangOptions &LangOpts);
+ const LangOptions &LangOpts,
+ bool IncludeComments = false);
/// Checks that the given token is the first token that occurs after
/// the given location (this excludes comments and whitespace). Returns the
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133725.459550.patch
Type: text/x-patch
Size: 3051 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220912/593369e0/attachment.bin>
More information about the cfe-commits
mailing list