aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Lex/LexerTest.cpp
diff options
context:
space:
mode:
authorClement Courbet <courbet@google.com>2025-01-22 13:42:00 +0100
committerGitHub <noreply@github.com>2025-01-22 13:42:00 +0100
commitfbd86d05fe51d45f19df8d63aee41d979c268f8f (patch)
tree3fb1c0916b619344f7ff12c3ecaf16084842955f /clang/unittests/Lex/LexerTest.cpp
parentc6c647588f911770170d2f7975b325f3d70cf89b (diff)
downloadllvm-fbd86d05fe51d45f19df8d63aee41d979c268f8f.zip
llvm-fbd86d05fe51d45f19df8d63aee41d979c268f8f.tar.gz
llvm-fbd86d05fe51d45f19df8d63aee41d979c268f8f.tar.bz2
[clang-reorder-fields] Reorder leading comments (#123740)
Similarly to https://github.com/llvm/llvm-project/pull/122918, leading comments are currently not being moved. ``` struct Foo { // This one is the cool field. int a; int b; }; ``` becomes: ``` struct Foo { // This one is the cool field. int b; int a; }; ``` but should be: ``` struct Foo { int b; // This one is the cool field. int a; }; ```
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);