Skip to content

Commit 51f1681

Browse files
authored
[clang-format] Don't merge a short block for SBS_Never (#88238)
Also fix unit tests. Fixes #87484.
1 parent fca5191 commit 51f1681

File tree

7 files changed

+110
-26
lines changed

7 files changed

+110
-26
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace format {
3535
TYPE(BinaryOperator) \
3636
TYPE(BitFieldColon) \
3737
TYPE(BlockComment) \
38+
/* l_brace of a block that is not the body of a (e.g. loop) statement. */ \
39+
TYPE(BlockLBrace) \
3840
TYPE(BracedListLBrace) \
3941
/* The colon at the end of a case label. */ \
4042
TYPE(CaseLabelColon) \

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,12 @@ class LineJoiner {
796796
}
797797
}
798798

799-
if (const auto *LastNonComment = Line.getLastNonComment();
800-
LastNonComment && LastNonComment->is(tok::l_brace)) {
799+
if (Line.endsWith(tok::l_brace)) {
800+
if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never &&
801+
Line.First->is(TT_BlockLBrace)) {
802+
return 0;
803+
}
804+
801805
if (IsSplitBlock && Line.First == Line.Last &&
802806
I > AnnotatedLines.begin() &&
803807
(I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) {

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,10 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
395395
ParseDefault();
396396
continue;
397397
}
398-
if (!InRequiresExpression && FormatTok->isNot(TT_MacroBlockBegin) &&
399-
tryToParseBracedList()) {
400-
continue;
398+
if (!InRequiresExpression && FormatTok->isNot(TT_MacroBlockBegin)) {
399+
if (tryToParseBracedList())
400+
continue;
401+
FormatTok->setFinalizedType(TT_BlockLBrace);
401402
}
402403
parseBlock();
403404
++StatementCount;

clang/unittests/Format/BracesRemoverTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ TEST_F(BracesRemoverTest, RemoveBraces) {
209209
verifyFormat("if (a) {\n"
210210
" b;\n"
211211
"} else {\n"
212-
" { c; }\n"
212+
" {\n"
213+
" c;\n"
214+
" }\n"
213215
"}",
214216
Style);
215217

clang/unittests/Format/FormatTest.cpp

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
5252
}
5353

5454
TEST_F(FormatTest, FormatsNestedBlockStatements) {
55-
verifyFormat("{\n {\n {}\n }\n}", "{{{}}}");
55+
verifyFormat("{\n"
56+
" {\n"
57+
" {\n"
58+
" }\n"
59+
" }\n"
60+
"}",
61+
"{{{}}}");
5662
}
5763

5864
TEST_F(FormatTest, FormatsNestedCall) {
@@ -5669,7 +5675,10 @@ TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
56695675
getLLVMStyleWithColumns(14));
56705676
}
56715677

5672-
TEST_F(FormatTest, LayoutRemainingTokens) { verifyFormat("{}"); }
5678+
TEST_F(FormatTest, LayoutRemainingTokens) {
5679+
verifyFormat("{\n"
5680+
"}");
5681+
}
56735682

56745683
TEST_F(FormatTest, MacroDefinitionInsideStatement) {
56755684
verifyFormat("int x,\n"
@@ -6577,7 +6586,11 @@ TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
65776586
}
65786587

65796588
TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
6580-
verifyFormat("{\n { a #c; }\n}");
6589+
verifyFormat("{\n"
6590+
" {\n"
6591+
" a #c;\n"
6592+
" }\n"
6593+
"}");
65816594
}
65826595

65836596
TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
@@ -6937,13 +6950,13 @@ TEST_F(FormatTest, FormatNestedBlocksInMacros) {
69376950
}
69386951

69396952
TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
6940-
verifyFormat("{}");
69416953
verifyFormat("enum E {};");
69426954
verifyFormat("enum E {}");
69436955
FormatStyle Style = getLLVMStyle();
69446956
Style.SpaceInEmptyBlock = true;
69456957
verifyFormat("void f() { }", "void f() {}", Style);
69466958
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
6959+
verifyFormat("{ }", Style);
69476960
verifyFormat("while (true) { }", "while (true) {}", Style);
69486961
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
69496962
Style.BraceWrapping.BeforeElse = false;
@@ -11527,10 +11540,18 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
1152711540
"void new (link p);\n"
1152811541
"void delete (link p);");
1152911542

11530-
verifyFormat("{ p->new(); }\n"
11531-
"{ p->delete(); }",
11532-
"{ p->new (); }\n"
11533-
"{ p->delete (); }");
11543+
verifyFormat("{\n"
11544+
" p->new();\n"
11545+
"}\n"
11546+
"{\n"
11547+
" p->delete();\n"
11548+
"}",
11549+
"{\n"
11550+
" p->new ();\n"
11551+
"}\n"
11552+
"{\n"
11553+
" p->delete ();\n"
11554+
"}");
1153411555

1153511556
FormatStyle AfterPlacementOperator = getLLVMStyle();
1153611557
AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
@@ -12352,7 +12373,9 @@ TEST_F(FormatTest, FormatsCasts) {
1235212373
// FIXME: single value wrapped with paren will be treated as cast.
1235312374
verifyFormat("void f(int i = (kValue)*kMask) {}");
1235412375

12355-
verifyFormat("{ (void)F; }");
12376+
verifyFormat("{\n"
12377+
" (void)F;\n"
12378+
"}");
1235612379

1235712380
// Don't break after a cast's
1235812381
verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
@@ -13575,7 +13598,8 @@ TEST_F(FormatTest, IncorrectAccessSpecifier) {
1357513598
verifyFormat("public\n"
1357613599
"B {}");
1357713600
verifyFormat("public\n"
13578-
"{}");
13601+
"{\n"
13602+
"}");
1357913603
verifyFormat("public\n"
1358013604
"B { int x; }");
1358113605
}
@@ -13632,10 +13656,31 @@ TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1363213656
}
1363313657

1363413658
TEST_F(FormatTest, IncorrectCodeErrorDetection) {
13635-
verifyFormat("{\n {}", "{\n{\n}");
13636-
verifyFormat("{\n {}", "{\n {\n}");
13637-
verifyFormat("{\n {}", "{\n {\n }");
13638-
verifyFormat("{\n {}\n}\n}", "{\n {\n }\n }\n}");
13659+
verifyFormat("{\n"
13660+
" {\n"
13661+
" }",
13662+
"{\n"
13663+
"{\n"
13664+
"}");
13665+
verifyFormat("{\n"
13666+
" {\n"
13667+
" }",
13668+
"{\n"
13669+
" {\n"
13670+
"}");
13671+
verifyFormat("{\n"
13672+
" {\n"
13673+
" }");
13674+
verifyFormat("{\n"
13675+
" {\n"
13676+
" }\n"
13677+
"}\n"
13678+
"}",
13679+
"{\n"
13680+
" {\n"
13681+
" }\n"
13682+
" }\n"
13683+
"}");
1363913684

1364013685
verifyFormat("{\n"
1364113686
" {\n"
@@ -14080,10 +14125,14 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
1408014125
"}");
1408114126
verifyFormat("void foo() {\n"
1408214127
" { // asdf\n"
14083-
" { int a; }\n"
14128+
" {\n"
14129+
" int a;\n"
14130+
" }\n"
1408414131
" }\n"
1408514132
" {\n"
14086-
" { int b; }\n"
14133+
" {\n"
14134+
" int b;\n"
14135+
" }\n"
1408714136
" }\n"
1408814137
"}");
1408914138
verifyFormat("namespace n {\n"
@@ -14095,7 +14144,8 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
1409514144
" }\n"
1409614145
" }\n"
1409714146
" }\n"
14098-
" {}\n"
14147+
" {\n"
14148+
" }\n"
1409914149
"}\n"
1410014150
"} // namespace n");
1410114151

clang/unittests/Format/FormatTestMacroExpansion.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ TEST_F(FormatTestMacroExpansion, UnexpandConfiguredMacros) {
4343
"STMT",
4444
Style);
4545
verifyFormat("void f() { ID(a *b); }", Style);
46-
verifyFormat(R"(ID(
47-
{ ID(a *b); });
48-
)",
46+
verifyFormat("ID(\n"
47+
" {\n"
48+
" ID(a *b);\n"
49+
" });",
4950
Style);
5051
verifyIncompleteFormat("ID3({, ID(a *b),\n"
5152
" ;\n"

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,6 +2856,30 @@ TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) {
28562856
EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_FunctionLBrace);
28572857
}
28582858

2859+
TEST_F(TokenAnnotatorTest, BlockLBrace) {
2860+
auto Tokens = annotate("{\n"
2861+
" {\n"
2862+
" foo();\n"
2863+
" }\n"
2864+
"}");
2865+
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2866+
EXPECT_TOKEN(Tokens[0], tok::l_brace, TT_BlockLBrace);
2867+
EXPECT_BRACE_KIND(Tokens[0], BK_Block);
2868+
EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_BlockLBrace);
2869+
EXPECT_BRACE_KIND(Tokens[1], BK_Block);
2870+
2871+
Tokens = annotate("void bar() {\n"
2872+
" {\n"
2873+
" foo();\n"
2874+
" }\n"
2875+
"}");
2876+
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
2877+
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
2878+
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
2879+
EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_BlockLBrace);
2880+
EXPECT_BRACE_KIND(Tokens[5], BK_Block);
2881+
}
2882+
28592883
} // namespace
28602884
} // namespace format
28612885
} // namespace clang

0 commit comments

Comments
 (0)