diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 9fa6e9e638853..7076a9f5cb3f4 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -711,16 +711,23 @@ class LineJoiner { if (Tok && Tok->is(tok::colon)) return 0; } - if (Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while, tok::kw_do, - tok::kw_try, tok::kw___try, tok::kw_catch, - tok::kw___finally, tok::kw_for, TT_ForEachMacro, - tok::r_brace, Keywords.kw___except)) { - if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) - return 0; - if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Empty && - !I[1]->First->is(tok::r_brace)) { + + auto IsCtrlStmt = [](const auto &Line) { + return Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while, + tok::kw_do, tok::kw_for, TT_ForEachMacro); + }; + + const bool IsSplitBlock = + Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never || + (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Empty && + I[1]->First->isNot(tok::r_brace)); + + if (IsCtrlStmt(Line) || + Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch, + tok::kw___finally, tok::r_brace, + Keywords.kw___except)) { + if (IsSplitBlock) return 0; - } // Don't merge when we can't except the case when // the control statement block is empty if (!Style.AllowShortIfStatementsOnASingleLine && @@ -763,6 +770,11 @@ class LineJoiner { } if (Line.Last->is(tok::l_brace)) { + if (IsSplitBlock && Line.First == Line.Last && + I > AnnotatedLines.begin() && + (I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) { + return 0; + } FormatToken *Tok = I[1]->First; auto ShouldMerge = [Tok]() { if (Tok->isNot(tok::r_brace) || Tok->MustBreakBefore) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 57a6d83578f1d..d330c65470f0d 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -1860,6 +1860,69 @@ TEST_F(FormatTest, FormatShortBracedStatements) { " f();\n" "}", AllowSimpleBracedStatements); + + FormatStyle Style = getLLVMStyle(); + Style.BreakBeforeBraces = FormatStyle::BS_Custom; + Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; + + verifyFormat("while (i > 0)\n" + "{\n" + " --i;\n" + "}", + Style); + + verifyFormat("if (a)\n" + "{\n" + " ++b;\n" + "}", + Style); + + verifyFormat("if (a)\n" + "{\n" + " b = 1;\n" + "} else\n" + "{\n" + " b = 0;\n" + "}", + Style); + + verifyFormat("if (a)\n" + "{\n" + " b = 1;\n" + "} else if (c)\n" + "{\n" + " b = 2;\n" + "} else\n" + "{\n" + " b = 0;\n" + "}", + Style); + + Style.BraceWrapping.BeforeElse = true; + + verifyFormat("if (a)\n" + "{\n" + " b = 1;\n" + "}\n" + "else\n" + "{\n" + " b = 0;\n" + "}", + Style); + + verifyFormat("if (a)\n" + "{\n" + " b = 1;\n" + "}\n" + "else if (c)\n" + "{\n" + " b = 2;\n" + "}\n" + "else\n" + "{\n" + " b = 0;\n" + "}", + Style); } TEST_F(FormatTest, UnderstandsMacros) { @@ -25281,8 +25344,6 @@ TEST_F(FormatTest, InsertBraces) { Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; - // TODO: Delete the following line after #57421 is fixed. - Style.BraceWrapping.AfterFunction = true; verifyFormat("if (a) //\n" "{\n"