Skip to content

Commit 56d920d

Browse files
authored
[clang-format] Fix a bug in wrapping { after else (#161048)
Fixes #160775
1 parent c6d3b51 commit 56d920d

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace format {
5555
TYPE(ConflictAlternative) \
5656
TYPE(ConflictEnd) \
5757
TYPE(ConflictStart) \
58-
/* l_brace of if/for/while */ \
58+
/* l_brace of if/for/while/switch/catch */ \
5959
TYPE(ControlStatementLBrace) \
6060
TYPE(ControlStatementRBrace) \
6161
TYPE(CppCastLParen) \

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4021,29 +4021,28 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
40214021
}
40224022
}
40234023

4024-
if (IsCpp &&
4025-
(LineIsFunctionDeclaration ||
4026-
(FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4027-
Line.endsWith(tok::semi, tok::r_brace)) {
4028-
auto *Tok = Line.Last->Previous;
4029-
while (Tok->isNot(tok::r_brace))
4030-
Tok = Tok->Previous;
4031-
if (auto *LBrace = Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4032-
assert(LBrace->is(tok::l_brace));
4033-
Tok->setBlockKind(BK_Block);
4034-
LBrace->setBlockKind(BK_Block);
4035-
LBrace->setFinalizedType(TT_FunctionLBrace);
4024+
if (IsCpp) {
4025+
if ((LineIsFunctionDeclaration ||
4026+
(FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4027+
Line.endsWith(tok::semi, tok::r_brace)) {
4028+
auto *Tok = Line.Last->Previous;
4029+
while (Tok->isNot(tok::r_brace))
4030+
Tok = Tok->Previous;
4031+
if (auto *LBrace = Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4032+
assert(LBrace->is(tok::l_brace));
4033+
Tok->setBlockKind(BK_Block);
4034+
LBrace->setBlockKind(BK_Block);
4035+
LBrace->setFinalizedType(TT_FunctionLBrace);
4036+
}
40364037
}
4037-
}
40384038

4039-
if (IsCpp && SeenName && AfterLastAttribute &&
4040-
mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
4041-
AfterLastAttribute->MustBreakBefore = true;
4042-
if (LineIsFunctionDeclaration)
4043-
Line.ReturnTypeWrapped = true;
4044-
}
4039+
if (SeenName && AfterLastAttribute &&
4040+
mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
4041+
AfterLastAttribute->MustBreakBefore = true;
4042+
if (LineIsFunctionDeclaration)
4043+
Line.ReturnTypeWrapped = true;
4044+
}
40454045

4046-
if (IsCpp) {
40474046
if (!LineIsFunctionDeclaration) {
40484047
// Annotate */&/&& in `operator` function calls as binary operators.
40494048
for (const auto *Tok = FirstNonComment; Tok; Tok = Tok->Next) {
@@ -4089,6 +4088,11 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
40894088
}
40904089
}
40914090

4091+
if (First->is(TT_ElseLBrace)) {
4092+
First->CanBreakBefore = true;
4093+
First->MustBreakBefore = true;
4094+
}
4095+
40924096
bool InFunctionDecl = Line.MightBeFunctionDecl;
40934097
bool InParameterList = false;
40944098
for (auto *Current = First->Next; Current; Current = Current->Next) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,27 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
13641364
AllowsMergedIf);
13651365
}
13661366

1367+
TEST_F(FormatTest, WrapMultipleStatementIfAndElseBraces) {
1368+
auto Style = getLLVMStyle();
1369+
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1370+
Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_AllIfsAndElse;
1371+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1372+
Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1373+
Style.BraceWrapping.BeforeElse = true;
1374+
1375+
verifyFormat("if (x)\n"
1376+
"{\n"
1377+
" ++x;\n"
1378+
" --y;\n"
1379+
"}\n"
1380+
"else\n"
1381+
"{\n"
1382+
" --x;\n"
1383+
" ++y;\n"
1384+
"}",
1385+
Style);
1386+
}
1387+
13671388
TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
13681389
verifyFormat("while (true)\n"
13691390
" ;");

0 commit comments

Comments
 (0)