Skip to content

Commit e14b5e8

Browse files
owencac-rhodes
authored andcommitted
[clang-format] Fix a bug in wrapping { after else (llvm#161048)
Fixes llvm#160775 (cherry picked from commit 56d920d)
1 parent b54051a commit e14b5e8

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
@@ -4000,29 +4000,28 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
40004000
}
40014001
}
40024002

4003-
if (IsCpp &&
4004-
(LineIsFunctionDeclaration ||
4005-
(FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4006-
Line.endsWith(tok::semi, tok::r_brace)) {
4007-
auto *Tok = Line.Last->Previous;
4008-
while (Tok->isNot(tok::r_brace))
4009-
Tok = Tok->Previous;
4010-
if (auto *LBrace = Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4011-
assert(LBrace->is(tok::l_brace));
4012-
Tok->setBlockKind(BK_Block);
4013-
LBrace->setBlockKind(BK_Block);
4014-
LBrace->setFinalizedType(TT_FunctionLBrace);
4003+
if (IsCpp) {
4004+
if ((LineIsFunctionDeclaration ||
4005+
(FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4006+
Line.endsWith(tok::semi, tok::r_brace)) {
4007+
auto *Tok = Line.Last->Previous;
4008+
while (Tok->isNot(tok::r_brace))
4009+
Tok = Tok->Previous;
4010+
if (auto *LBrace = Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4011+
assert(LBrace->is(tok::l_brace));
4012+
Tok->setBlockKind(BK_Block);
4013+
LBrace->setBlockKind(BK_Block);
4014+
LBrace->setFinalizedType(TT_FunctionLBrace);
4015+
}
40154016
}
4016-
}
40174017

4018-
if (IsCpp && SeenName && AfterLastAttribute &&
4019-
mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
4020-
AfterLastAttribute->MustBreakBefore = true;
4021-
if (LineIsFunctionDeclaration)
4022-
Line.ReturnTypeWrapped = true;
4023-
}
4018+
if (SeenName && AfterLastAttribute &&
4019+
mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
4020+
AfterLastAttribute->MustBreakBefore = true;
4021+
if (LineIsFunctionDeclaration)
4022+
Line.ReturnTypeWrapped = true;
4023+
}
40244024

4025-
if (IsCpp) {
40264025
if (!LineIsFunctionDeclaration) {
40274026
// Annotate */&/&& in `operator` function calls as binary operators.
40284027
for (const auto *Tok = FirstNonComment; Tok; Tok = Tok->Next) {
@@ -4068,6 +4067,11 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
40684067
}
40694068
}
40704069

4070+
if (First->is(TT_ElseLBrace)) {
4071+
First->CanBreakBefore = true;
4072+
First->MustBreakBefore = true;
4073+
}
4074+
40714075
bool InFunctionDecl = Line.MightBeFunctionDecl;
40724076
bool InParameterList = false;
40734077
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)