aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Lex/LexerTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang/unittests/Lex/LexerTest.cpp')
-rw-r--r--clang/unittests/Lex/LexerTest.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/clang/unittests/Lex/LexerTest.cpp b/clang/unittests/Lex/LexerTest.cpp
index c897998..29c61fe 100644
--- a/clang/unittests/Lex/LexerTest.cpp
+++ b/clang/unittests/Lex/LexerTest.cpp
@@ -640,6 +640,41 @@ TEST_F(LexerTest, FindNextTokenIncludingComments) {
"=", "abcd", ";"));
}
+TEST_F(LexerTest, FindPreviousToken) {
+ Lex("int abcd = 0;\n"
+ "// A comment.\n"
+ "int xyz = abcd;\n");
+ std::vector<std::string> GeneratedByPrevToken;
+ SourceLocation Loc = SourceMgr.getLocForEndOfFile(SourceMgr.getMainFileID());
+ while (true) {
+ auto T = Lexer::findPreviousToken(Loc, SourceMgr, LangOpts, false);
+ if (!T.has_value())
+ break;
+ GeneratedByPrevToken.push_back(getSourceText(*T, *T));
+ Loc = Lexer::GetBeginningOfToken(T->getLocation(), SourceMgr, LangOpts);
+ }
+ EXPECT_THAT(GeneratedByPrevToken, ElementsAre(";", "abcd", "=", "xyz", "int",
+ ";", "0", "=", "abcd", "int"));
+}
+
+TEST_F(LexerTest, FindPreviousTokenIncludingComments) {
+ Lex("int abcd = 0;\n"
+ "// A comment.\n"
+ "int xyz = abcd;\n");
+ std::vector<std::string> GeneratedByPrevToken;
+ SourceLocation Loc = SourceMgr.getLocForEndOfFile(SourceMgr.getMainFileID());
+ while (true) {
+ auto T = Lexer::findPreviousToken(Loc, SourceMgr, LangOpts, true);
+ if (!T.has_value())
+ break;
+ GeneratedByPrevToken.push_back(getSourceText(*T, *T));
+ Loc = Lexer::GetBeginningOfToken(T->getLocation(), SourceMgr, LangOpts);
+ }
+ EXPECT_THAT(GeneratedByPrevToken,
+ ElementsAre(";", "abcd", "=", "xyz", "int", "// A comment.", ";",
+ "0", "=", "abcd", "int"));
+}
+
TEST_F(LexerTest, CreatedFIDCountForPredefinedBuffer) {
TrivialModuleLoader ModLoader;
auto PP = CreatePP("", ModLoader);