Skip to content

Commit

Permalink
[clang-format] Fix a regression in annotating BK_BracedInit (#87450)
Browse files Browse the repository at this point in the history
Fixes #86539.
  • Loading branch information
owenca committed Apr 5, 2024
1 parent b76eb1d commit 7c9c38e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
17 changes: 10 additions & 7 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -640,6 +642,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
default:
break;
}

PrevTok = Tok;
Tok = NextTok;
} while (Tok->isNot(tok::eof) && !LBraceStack.empty());
Expand Down
12 changes: 12 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 13 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7c9c38e

Please sign in to comment.