[clang] 7c9c38e - [clang-format] Fix a regression in annotating BK_BracedInit (#87450)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 4 17:55:22 PDT 2024
Author: Owen Pan
Date: 2024-04-04T17:55:18-07:00
New Revision: 7c9c38eaa9b74debe5dd094535653c5d3f6ca591
URL: https://github.com/llvm/llvm-project/commit/7c9c38eaa9b74debe5dd094535653c5d3f6ca591
DIFF: https://github.com/llvm/llvm-project/commit/7c9c38eaa9b74debe5dd094535653c5d3f6ca591.diff
LOG: [clang-format] Fix a regression in annotating BK_BracedInit (#87450)
Fixes #86539.
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 98ae1c8f62bbc2..55c627d87f08b5 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -495,20 +495,22 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
};
SmallVector<StackEntry, 8> LBraceStack;
assert(Tok->is(tok::l_brace));
+
do {
- // Get next non-comment, non-preprocessor token.
FormatToken *NextTok;
do {
NextTok = Tokens->getNextToken();
} while (NextTok->is(tok::comment));
- if (!Style.isTableGen()) {
- // InTableGen, '#' is like binary operator. Not a preprocessor directive.
- while (NextTok->is(tok::hash) && !Line->InMacroBody) {
- NextTok = Tokens->getNextToken();
+
+ if (!Line->InMacroBody && !Style.isTableGen()) {
+ // Skip PPDirective lines and comments.
+ while (NextTok->is(tok::hash)) {
do {
NextTok = Tokens->getNextToken();
- } while (NextTok->is(tok::comment) ||
- (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof)));
+ } while (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof));
+
+ while (NextTok->is(tok::comment))
+ NextTok = Tokens->getNextToken();
}
}
@@ -640,6 +642,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
default:
break;
}
+
PrevTok = Tok;
Tok = NextTok;
} while (Tok->isNot(tok::eof) && !LBraceStack.empty());
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ec0267da468ab2..91a8ff11889d6f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27272,6 +27272,18 @@ TEST_F(FormatTest, PPBranchesInBracedInit) {
"};");
}
+TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
+ verifyFormat("{\n"
+ " char *a[] = {\n"
+ " /* abc */ \"abc\",\n"
+ "#if FOO\n"
+ " /* xyz */ \"xyz\",\n"
+ "#endif\n"
+ " /* last */ \"last\"};\n"
+ "}",
+ getLLVMStyleWithColumns(30));
+}
+
TEST_F(FormatTest, StreamOutputOperator) {
verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
verifyFormat("std::cout << \"foo\\n\"\n"
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 9425647d99980b..5ba5e0fbd16f9e 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2809,6 +2809,19 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[13], BK_Block);
EXPECT_BRACE_KIND(Tokens[14], BK_Block);
+
+ Tokens = annotate("{\n"
+ " char *a[] = {\n"
+ " /* abc */ \"abc\",\n"
+ "#if FOO\n"
+ " /* xyz */ \"xyz\",\n"
+ "#endif\n"
+ " /* last */ \"last\"};\n"
+ "}");
+ ASSERT_EQ(Tokens.size(), 25u) << Tokens;
+ EXPECT_BRACE_KIND(Tokens[0], BK_Block);
+ EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
+ EXPECT_BRACE_KIND(Tokens[21], BK_BracedInit);
}
TEST_F(TokenAnnotatorTest, StreamOperator) {
More information about the cfe-commits
mailing list