Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -512,11 +512,18 @@ TT_LineComment)) return 0; // Only inline simple if's (no nested if or else), unless specified - if (Style.AllowShortIfStatementsOnASingleLine != FormatStyle::SIS_Always) { + if (Style.AllowShortIfStatementsOnASingleLine == FormatStyle::SIS_Never) { if (I + 2 != E && Line.startsWith(tok::kw_if) && I[2]->First->is(tok::kw_else)) return 0; + } else if (Style.AllowShortIfStatementsOnASingleLine == + FormatStyle::SIS_WithoutElse) { + if (I + 2 != E && I + 3 != E && Line.startsWith(tok::kw_if) && + I[2]->First->is(tok::kw_else) && + (I[2]->Last->is(tok::l_brace) || I[3]->First->is(tok::l_brace))) + return 0; } + return 1; } @@ -685,6 +692,14 @@ if (Tok->Next && Tok->Next->is(tok::kw_else)) return 0; + // Don't merge a if statement if followed by a compound else + if (Style.AllowShortIfStatementsOnASingleLine == + FormatStyle::SIS_WithoutElse && + I + 3 != E && I[3]->First->is(tok::kw_else) && + (I[3]->Last->is(tok::l_brace) || + (I + 4 != E && I[4]->First->is(tok::l_brace)))) + return 0; + // Don't merge a trailing multi-line control statement block like: // } else if (foo && // bar) Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -511,8 +511,7 @@ " g();\n" "}", AllowsMergedIf); - verifyFormat("if (a)\n" - " f();\n" + verifyFormat("if (a) f();\n" "else\n" " g();\n", AllowsMergedIf); @@ -631,6 +630,11 @@ " f();\n" "}", AllowSimpleBracedStatements); + verifyFormat("if (true) {\n" + " f();\n" + "} else\n" + " f();\n", + AllowSimpleBracedStatements); verifyFormat("struct A2 {\n" " int X;\n" @@ -645,6 +649,22 @@ "};", AllowSimpleBracedStatements); + AllowSimpleBracedStatements.BraceWrapping.BeforeElse = true; + + verifyFormat("if (true) {\n" + " f();\n" + "}\n" + "else {\n" + " f();\n" + "}", + AllowSimpleBracedStatements); + verifyFormat("if (true) { f(); }\n" + "else\n" + " f();\n", + AllowSimpleBracedStatements); + + AllowSimpleBracedStatements.BraceWrapping.BeforeElse = false; + AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; verifyFormat("if (true) {}", AllowSimpleBracedStatements);