Skip to content

Commit edad025

Browse files
authored
[clang-format] Correctly annotate braces of empty functions (#72733)
Also fixed some existing test cases. Fixed #57305. Fixed #58251.
1 parent cb3a605 commit edad025

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,6 +3477,19 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
34773477
}
34783478
}
34793479

3480+
if (IsCpp && LineIsFunctionDeclaration &&
3481+
Line.endsWith(tok::semi, tok::r_brace)) {
3482+
auto *Tok = Line.Last->Previous;
3483+
while (Tok->isNot(tok::r_brace))
3484+
Tok = Tok->Previous;
3485+
if (auto *LBrace = Tok->MatchingParen; LBrace) {
3486+
assert(LBrace->is(tok::l_brace));
3487+
Tok->setBlockKind(BK_Block);
3488+
LBrace->setBlockKind(BK_Block);
3489+
LBrace->setFinalizedType(TT_FunctionLBrace);
3490+
}
3491+
}
3492+
34803493
if (IsCpp && SeenName && AfterLastAttribute &&
34813494
mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
34823495
AfterLastAttribute->MustBreakBefore = true;

clang/unittests/Format/FormatTest.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,7 +3415,7 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
34153415
verifyFormat("myFunc(public);");
34163416
verifyFormat("std::vector<int> testVec = {private};");
34173417
verifyFormat("private.p = 1;");
3418-
verifyFormat("void function(private...){};");
3418+
verifyFormat("void function(private...) {};");
34193419
verifyFormat("if (private && public)");
34203420
verifyFormat("private &= true;");
34213421
verifyFormat("int x = private * public;");
@@ -16093,83 +16093,83 @@ TEST_F(FormatTest, ZeroTabWidth) {
1609316093
Tab.IndentWidth = 8;
1609416094
Tab.UseTab = FormatStyle::UT_Never;
1609516095
Tab.TabWidth = 0;
16096-
verifyFormat("void a(){\n"
16097-
" // line starts with '\t'\n"
16096+
verifyFormat("void a() {\n"
16097+
" // line starts with '\t'\n"
1609816098
"};",
1609916099
"void a(){\n"
1610016100
"\t// line starts with '\t'\n"
1610116101
"};",
1610216102
Tab);
1610316103

16104-
verifyFormat("void a(){\n"
16105-
" // line starts with '\t'\n"
16104+
verifyFormat("void a() {\n"
16105+
" // line starts with '\t'\n"
1610616106
"};",
1610716107
"void a(){\n"
1610816108
"\t\t// line starts with '\t'\n"
1610916109
"};",
1611016110
Tab);
1611116111

1611216112
Tab.UseTab = FormatStyle::UT_ForIndentation;
16113-
verifyFormat("void a(){\n"
16114-
" // line starts with '\t'\n"
16113+
verifyFormat("void a() {\n"
16114+
" // line starts with '\t'\n"
1611516115
"};",
1611616116
"void a(){\n"
1611716117
"\t// line starts with '\t'\n"
1611816118
"};",
1611916119
Tab);
1612016120

16121-
verifyFormat("void a(){\n"
16122-
" // line starts with '\t'\n"
16121+
verifyFormat("void a() {\n"
16122+
" // line starts with '\t'\n"
1612316123
"};",
1612416124
"void a(){\n"
1612516125
"\t\t// line starts with '\t'\n"
1612616126
"};",
1612716127
Tab);
1612816128

1612916129
Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16130-
verifyFormat("void a(){\n"
16131-
" // line starts with '\t'\n"
16130+
verifyFormat("void a() {\n"
16131+
" // line starts with '\t'\n"
1613216132
"};",
1613316133
"void a(){\n"
1613416134
"\t// line starts with '\t'\n"
1613516135
"};",
1613616136
Tab);
1613716137

16138-
verifyFormat("void a(){\n"
16139-
" // line starts with '\t'\n"
16138+
verifyFormat("void a() {\n"
16139+
" // line starts with '\t'\n"
1614016140
"};",
1614116141
"void a(){\n"
1614216142
"\t\t// line starts with '\t'\n"
1614316143
"};",
1614416144
Tab);
1614516145

1614616146
Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16147-
verifyFormat("void a(){\n"
16148-
" // line starts with '\t'\n"
16147+
verifyFormat("void a() {\n"
16148+
" // line starts with '\t'\n"
1614916149
"};",
1615016150
"void a(){\n"
1615116151
"\t// line starts with '\t'\n"
1615216152
"};",
1615316153
Tab);
1615416154

16155-
verifyFormat("void a(){\n"
16156-
" // line starts with '\t'\n"
16155+
verifyFormat("void a() {\n"
16156+
" // line starts with '\t'\n"
1615716157
"};",
1615816158
"void a(){\n"
1615916159
"\t\t// line starts with '\t'\n"
1616016160
"};",
1616116161
Tab);
1616216162

1616316163
Tab.UseTab = FormatStyle::UT_Always;
16164-
verifyFormat("void a(){\n"
16164+
verifyFormat("void a() {\n"
1616516165
"// line starts with '\t'\n"
1616616166
"};",
1616716167
"void a(){\n"
1616816168
"\t// line starts with '\t'\n"
1616916169
"};",
1617016170
Tab);
1617116171

16172-
verifyFormat("void a(){\n"
16172+
verifyFormat("void a() {\n"
1617316173
"// line starts with '\t'\n"
1617416174
"};",
1617516175
"void a(){\n"

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,6 +2410,60 @@ TEST_F(TokenAnnotatorTest, StartOfName) {
24102410
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_StartOfName);
24112411
}
24122412

2413+
TEST_F(TokenAnnotatorTest, BraceKind) {
2414+
auto Tokens = annotate("void f() {};");
2415+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2416+
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2417+
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
2418+
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
2419+
EXPECT_BRACE_KIND(Tokens[5], BK_Block);
2420+
2421+
Tokens = annotate("void f() override {};");
2422+
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2423+
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2424+
EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_FunctionLBrace);
2425+
EXPECT_BRACE_KIND(Tokens[5], BK_Block);
2426+
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
2427+
2428+
Tokens = annotate("void f() noexcept(false) {};");
2429+
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
2430+
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2431+
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace);
2432+
EXPECT_BRACE_KIND(Tokens[8], BK_Block);
2433+
EXPECT_BRACE_KIND(Tokens[9], BK_Block);
2434+
2435+
Tokens = annotate("auto f() -> void {};");
2436+
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2437+
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2438+
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
2439+
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
2440+
EXPECT_BRACE_KIND(Tokens[7], BK_Block);
2441+
2442+
Tokens = annotate("void f() { /**/ };");
2443+
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2444+
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2445+
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
2446+
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
2447+
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
2448+
2449+
Tokens = annotate("void f() { //\n"
2450+
"};");
2451+
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2452+
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2453+
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
2454+
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
2455+
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
2456+
2457+
Tokens = annotate("void f() {\n"
2458+
" //\n"
2459+
"};");
2460+
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2461+
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2462+
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
2463+
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
2464+
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
2465+
}
2466+
24132467
} // namespace
24142468
} // namespace format
24152469
} // namespace clang

0 commit comments

Comments
 (0)