Skip to content

Commit 7c9c38e

Browse files
authored
[clang-format] Fix a regression in annotating BK_BracedInit (llvm#87450)
Fixes llvm#86539.
1 parent b76eb1d commit 7c9c38e

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,20 +495,22 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
495495
};
496496
SmallVector<StackEntry, 8> LBraceStack;
497497
assert(Tok->is(tok::l_brace));
498+
498499
do {
499-
// Get next non-comment, non-preprocessor token.
500500
FormatToken *NextTok;
501501
do {
502502
NextTok = Tokens->getNextToken();
503503
} while (NextTok->is(tok::comment));
504-
if (!Style.isTableGen()) {
505-
// InTableGen, '#' is like binary operator. Not a preprocessor directive.
506-
while (NextTok->is(tok::hash) && !Line->InMacroBody) {
507-
NextTok = Tokens->getNextToken();
504+
505+
if (!Line->InMacroBody && !Style.isTableGen()) {
506+
// Skip PPDirective lines and comments.
507+
while (NextTok->is(tok::hash)) {
508508
do {
509509
NextTok = Tokens->getNextToken();
510-
} while (NextTok->is(tok::comment) ||
511-
(NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof)));
510+
} while (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof));
511+
512+
while (NextTok->is(tok::comment))
513+
NextTok = Tokens->getNextToken();
512514
}
513515
}
514516

@@ -640,6 +642,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
640642
default:
641643
break;
642644
}
645+
643646
PrevTok = Tok;
644647
Tok = NextTok;
645648
} while (Tok->isNot(tok::eof) && !LBraceStack.empty());

clang/unittests/Format/FormatTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27272,6 +27272,18 @@ TEST_F(FormatTest, PPBranchesInBracedInit) {
2727227272
"};");
2727327273
}
2727427274

27275+
TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
27276+
verifyFormat("{\n"
27277+
" char *a[] = {\n"
27278+
" /* abc */ \"abc\",\n"
27279+
"#if FOO\n"
27280+
" /* xyz */ \"xyz\",\n"
27281+
"#endif\n"
27282+
" /* last */ \"last\"};\n"
27283+
"}",
27284+
getLLVMStyleWithColumns(30));
27285+
}
27286+
2727527287
TEST_F(FormatTest, StreamOutputOperator) {
2727627288
verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
2727727289
verifyFormat("std::cout << \"foo\\n\"\n"

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,6 +2809,19 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
28092809
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
28102810
EXPECT_BRACE_KIND(Tokens[13], BK_Block);
28112811
EXPECT_BRACE_KIND(Tokens[14], BK_Block);
2812+
2813+
Tokens = annotate("{\n"
2814+
" char *a[] = {\n"
2815+
" /* abc */ \"abc\",\n"
2816+
"#if FOO\n"
2817+
" /* xyz */ \"xyz\",\n"
2818+
"#endif\n"
2819+
" /* last */ \"last\"};\n"
2820+
"}");
2821+
ASSERT_EQ(Tokens.size(), 25u) << Tokens;
2822+
EXPECT_BRACE_KIND(Tokens[0], BK_Block);
2823+
EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
2824+
EXPECT_BRACE_KIND(Tokens[21], BK_BracedInit);
28122825
}
28132826

28142827
TEST_F(TokenAnnotatorTest, StreamOperator) {

0 commit comments

Comments
 (0)