Skip to content

Commit 042f92b

Browse files
committed
release/18.x: [clang-format] Correctly annotate braces in macros (llvm#87953)
Backport 58323de
1 parent e6c3289 commit 042f92b

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,16 +534,6 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
534534
if (Style.Language == FormatStyle::LK_Proto) {
535535
ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
536536
} else {
537-
// Skip NextTok over preprocessor lines, otherwise we may not
538-
// properly diagnose the block as a braced intializer
539-
// if the comma separator appears after the pp directive.
540-
while (NextTok->is(tok::hash)) {
541-
ScopedMacroState MacroState(*Line, Tokens, NextTok);
542-
do {
543-
NextTok = Tokens->getNextToken();
544-
} while (NextTok->isNot(tok::eof));
545-
}
546-
547537
// Using OriginalColumn to distinguish between ObjC methods and
548538
// binary operators is a bit hacky.
549539
bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
@@ -602,6 +592,16 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
602592
NextTok = Tokens->getNextToken();
603593
ProbablyBracedList = NextTok->isNot(tok::l_square);
604594
}
595+
596+
// Cpp macro definition body that is a nonempty braced list or block:
597+
if (Style.isCpp() && Line->InMacroBody && PrevTok != FormatTok &&
598+
!FormatTok->Previous && NextTok->is(tok::eof) &&
599+
// A statement can end with only `;` (simple statement), a block
600+
// closing brace (compound statement), or `:` (label statement).
601+
// If PrevTok is a block opening brace, Tok ends an empty block.
602+
!PrevTok->isOneOf(tok::semi, BK_Block, tok::colon)) {
603+
ProbablyBracedList = true;
604+
}
605605
}
606606
if (ProbablyBracedList) {
607607
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) {
@@ -10865,7 +10872,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
1086510872
verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
1086610873

1086710874
verifyFormat("#define FOO(typeName, realClass) \\\n"
10868-
" { #typeName, foo<FooType>(new foo<realClass>(#typeName)) }",
10875+
" {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
1086910876
getLLVMStyleWithColumns(60));
1087010877
}
1087110878

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,14 +1885,20 @@ TEST_F(TokenAnnotatorTest, UnderstandHashInMacro) {
18851885
" #Bar \\\n"
18861886
" }");
18871887
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1888-
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
1889-
EXPECT_BRACE_KIND(Tokens[9], BK_Block);
1888+
EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
1889+
EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
18901890

18911891
Tokens = annotate("#define Foo(Bar) \\\n"
18921892
" { #Bar }");
18931893
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1894-
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
1895-
EXPECT_BRACE_KIND(Tokens[9], BK_Block);
1894+
EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
1895+
EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
1896+
1897+
Tokens = annotate("#define FOO(typeName, realClass) \\\n"
1898+
" {#typeName, foo<Foo>(new foo<realClass>(#typeName))}");
1899+
ASSERT_EQ(Tokens.size(), 29u) << Tokens;
1900+
EXPECT_BRACE_KIND(Tokens[8], BK_BracedInit);
1901+
EXPECT_BRACE_KIND(Tokens[27], BK_BracedInit);
18961902
}
18971903

18981904
TEST_F(TokenAnnotatorTest, UnderstandsAttributeMacros) {

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)