Skip to content

Commit

Permalink
[clang-format] Fix an invalid code generation in RemoveBracesLLVM
Browse files Browse the repository at this point in the history
Fixes #55706.

Differential Revision: https://reviews.llvm.org/D126438
  • Loading branch information
owenca authored and memfrob committed Oct 4, 2022
1 parent 1b27e14 commit b197d48
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
11 changes: 7 additions & 4 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2587,10 +2587,13 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
FormatTok->setFinalizedType(TT_ElseLBrace);
ElseLeftBrace = FormatTok;
CompoundStatementIndenter Indenter(this, Style, Line->Level);
if (parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
/*MunchSemi=*/true,
KeepElseBraces) == IfStmtKind::IfOnly) {
Kind = IfStmtKind::IfElseIf;
const IfStmtKind ElseBlockKind =
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
/*MunchSemi=*/true, KeepElseBraces);
if ((ElseBlockKind == IfStmtKind::IfOnly ||
ElseBlockKind == IfStmtKind::IfElseIf) &&
FormatTok->is(tok::kw_else)) {
KeepElseBraces = true;
}
addUnwrappedLine();
} else if (FormatTok->is(tok::kw_if)) {
Expand Down
24 changes: 24 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25385,6 +25385,30 @@ TEST_F(FormatTest, RemoveBraces) {
"}",
Style);

verifyFormat("if (a)\n"
" if (b)\n"
" c;\n"
" else {\n"
" if (d)\n"
" e;\n"
" }\n"
"else\n"
" f;",
Style);

verifyFormat("if (a)\n"
" if (b)\n"
" c;\n"
" else {\n"
" if (d)\n"
" e;\n"
" else if (f)\n"
" g;\n"
" }\n"
"else\n"
" h;",
Style);

verifyFormat("if (a)\n"
" b;\n"
"else if (c)\n"
Expand Down

0 comments on commit b197d48

Please sign in to comment.