Skip to content

Commit 58323de

Browse files
authored
[clang-format] Correctly annotate braces in macros (#87953)
Also fix unit tests and reformat polly. Fixes #86550.
1 parent 000f2b5 commit 58323de

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -538,16 +538,6 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
538538
if (Style.Language == FormatStyle::LK_Proto) {
539539
ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
540540
} else {
541-
// Skip NextTok over preprocessor lines, otherwise we may not
542-
// properly diagnose the block as a braced intializer
543-
// if the comma separator appears after the pp directive.
544-
while (NextTok->is(tok::hash)) {
545-
ScopedMacroState MacroState(*Line, Tokens, NextTok);
546-
do {
547-
NextTok = Tokens->getNextToken();
548-
} while (NextTok->isNot(tok::eof));
549-
}
550-
551541
// Using OriginalColumn to distinguish between ObjC methods and
552542
// binary operators is a bit hacky.
553543
bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
@@ -606,6 +596,16 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
606596
NextTok = Tokens->getNextToken();
607597
ProbablyBracedList = NextTok->isNot(tok::l_square);
608598
}
599+
600+
// Cpp macro definition body that is a nonempty braced list or block:
601+
if (IsCpp && Line->InMacroBody && PrevTok != FormatTok &&
602+
!FormatTok->Previous && NextTok->is(tok::eof) &&
603+
// A statement can end with only `;` (simple statement), a block
604+
// closing brace (compound statement), or `:` (label statement).
605+
// If PrevTok is a block opening brace, Tok ends an empty block.
606+
!PrevTok->isOneOf(tok::semi, BK_Block, tok::colon)) {
607+
ProbablyBracedList = true;
608+
}
609609
}
610610
if (ProbablyBracedList) {
611611
Tok->setBlockKind(BK_BracedInit);

clang/unittests/Format/FormatTest.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,13 @@ TEST_F(FormatTest, UnderstandsMacros) {
18651865
verifyFormat("MACRO(co_return##something)");
18661866

18671867
verifyFormat("#define A x:");
1868+
1869+
verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1870+
" { \\\n"
1871+
" #Bar \\\n"
1872+
" }");
1873+
verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1874+
" { #Bar }");
18681875
}
18691876

18701877
TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
@@ -11033,7 +11040,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
1103311040
verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
1103411041

1103511042
verifyFormat("#define FOO(typeName, realClass) \\\n"
11036-
" { #typeName, foo<FooType>(new foo<realClass>(#typeName)) }",
11043+
" {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
1103711044
getLLVMStyleWithColumns(60));
1103811045
}
1103911046

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,14 +1933,20 @@ TEST_F(TokenAnnotatorTest, UnderstandHashInMacro) {
19331933
" #Bar \\\n"
19341934
" }");
19351935
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1936-
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
1937-
EXPECT_BRACE_KIND(Tokens[9], BK_Block);
1936+
EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
1937+
EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
19381938

19391939
Tokens = annotate("#define Foo(Bar) \\\n"
19401940
" { #Bar }");
19411941
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1942-
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
1943-
EXPECT_BRACE_KIND(Tokens[9], BK_Block);
1942+
EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
1943+
EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
1944+
1945+
Tokens = annotate("#define FOO(typeName, realClass) \\\n"
1946+
" {#typeName, foo<Foo>(new foo<realClass>(#typeName))}");
1947+
ASSERT_EQ(Tokens.size(), 29u) << Tokens;
1948+
EXPECT_BRACE_KIND(Tokens[8], BK_BracedInit);
1949+
EXPECT_BRACE_KIND(Tokens[27], BK_BracedInit);
19441950
}
19451951

19461952
TEST_F(TokenAnnotatorTest, UnderstandsAttributeMacros) {
@@ -2822,6 +2828,13 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
28222828
EXPECT_BRACE_KIND(Tokens[0], BK_Block);
28232829
EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
28242830
EXPECT_BRACE_KIND(Tokens[21], BK_BracedInit);
2831+
2832+
Tokens =
2833+
annotate("#define SCOP_STAT(NAME, DESC) \\\n"
2834+
" {\"polly\", #NAME, \"Number of rejected regions: \" DESC}");
2835+
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
2836+
EXPECT_BRACE_KIND(Tokens[8], BK_BracedInit);
2837+
EXPECT_BRACE_KIND(Tokens[16], BK_BracedInit);
28252838
}
28262839

28272840
TEST_F(TokenAnnotatorTest, StreamOperator) {

polly/lib/Analysis/ScopDetectionDiagnostic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ using namespace llvm;
4545
#define DEBUG_TYPE "polly-detect"
4646

4747
#define SCOP_STAT(NAME, DESC) \
48-
{ "polly-detect", "NAME", "Number of rejected regions: " DESC }
48+
{"polly-detect", "NAME", "Number of rejected regions: " DESC}
4949

5050
static Statistic RejectStatistics[] = {
5151
SCOP_STAT(CFG, ""),

0 commit comments

Comments
 (0)