diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 140bda219382..255f5142f079 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1710,6 +1710,9 @@ private: unsigned Limit) { if (Limit == 0) return; + if (Style.BreakBeforeBraces == FormatStyle::BS_Allman && + (I + 1)->First->is(tok::l_brace)) + return; if ((I + 1)->InPPDirective != I->InPPDirective || ((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline)) return; diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index c80c297de22f..56fe81f3f2be 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -930,6 +930,8 @@ void UnwrappedLineParser::parseEnum() { nextToken(); } if (FormatTok->Tok.is(tok::l_brace)) { + if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) + addUnwrappedLine(); nextToken(); addUnwrappedLine(); ++Line->Level; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 2b1b94c4c61c..2969e568ebc6 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5530,6 +5530,37 @@ TEST_F(FormatTest, AllmanBraceBreaking) { " }\n" "}\n", BreakBeforeBrace); + + verifyFormat("enum X\n" + "{\n" + " Y = 0,\n" + "}\n", + BreakBeforeBrace); + + FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace; + BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; + BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; + verifyFormat("void f(bool b)\n" + "{\n" + " if (b)\n" + " {\n" + " return;\n" + " }\n" + "}\n", + BreakBeforeBraceShortIfs); + verifyFormat("void f(bool b)\n" + "{\n" + " if (b) return;\n" + "}\n", + BreakBeforeBraceShortIfs); + verifyFormat("void f(bool b)\n" + "{\n" + " while (b)\n" + " {\n" + " return;\n" + " }\n" + "}\n", + BreakBeforeBraceShortIfs); } TEST_F(FormatTest, UnderstandsPragmas) {