Skip to content

Commit

Permalink
[clang-format] Correctly annotate braces in macro definition (llvm#10…
Browse files Browse the repository at this point in the history
…7352)

Also add a test case for llvm#107096.

Fixes llvm#106418.
  • Loading branch information
owenca authored Sep 6, 2024
1 parent 16df489 commit 616a8ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,8 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
Keywords.kw_as));
ProbablyBracedList =
ProbablyBracedList || (IsCpp && NextTok->is(tok::l_paren));
ProbablyBracedList || (IsCpp && (PrevTok->Tok.isLiteral() ||
NextTok->is(tok::l_paren)));

// If there is a comma, semicolon or right paren after the closing
// brace, we assume this is a braced initializer list.
Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3278,6 +3278,26 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
EXPECT_BRACE_KIND(Tokens[10], BK_Block);
EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_StructRBrace);
EXPECT_BRACE_KIND(Tokens[11], BK_Block);

Tokens = annotate("#define MACRO \\\n"
" struct hash<type> { \\\n"
" void f() { return; } \\\n"
" };");
ASSERT_EQ(Tokens.size(), 20u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);
EXPECT_BRACE_KIND(Tokens[8], BK_Block);
EXPECT_TOKEN(Tokens[10], tok::identifier, TT_FunctionDeclarationName);
EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[13], BK_Block);
EXPECT_BRACE_KIND(Tokens[16], BK_Block);
EXPECT_TOKEN(Tokens[17], tok::r_brace, TT_StructRBrace);
EXPECT_BRACE_KIND(Tokens[17], BK_Block);

Tokens = annotate("#define MEMBER(NAME) NAME{\"\"}");
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
}

TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) {
Expand Down

0 comments on commit 616a8ce

Please sign in to comment.