Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -638,7 +638,8 @@ // we're not in a control flow statement and the last token is an opening // brace. AnnotatedLine &Line = **I; - if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, + if (Line.First->MustBreakBefore || + Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, tok::kw_else, tok::kw_try, tok::kw_catch, tok::kw_for, tok::kw_case, // This gets rid of all ObjC @ keywords and methods. @@ -654,9 +655,16 @@ Tok->CanBreakBefore = true; return 1; } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) { - // Check that we still have three lines and they fit into the limit. + // Check that we still have three lines if (I + 2 == E || I[2]->Type == LT_Invalid) return 0; + + // We have already checked for current line. Check if other two lines + // allows merging + if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore) + return 0; + + // Check if they fit into the limit. Limit = limitConsideringMacros(I + 2, E, Limit); if (!nextTwoLinesFitInto(I, Limit)) @@ -664,7 +672,7 @@ // Second, check that the next line does not contain any braces - if it // does, readability declines when putting it into a single line. - if (I[1]->Last->Type == TT_LineComment || Tok->MustBreakBefore) + if (I[1]->Last->Type == TT_LineComment) return 0; do { if (Tok->isOneOf(tok::l_brace, tok::r_brace)) @@ -674,8 +682,7 @@ // Last, check that the third line contains a single closing brace. Tok = I[2]->First; - if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) || - Tok->MustBreakBefore) + if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace)) return 0; return 2; Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -7336,6 +7336,25 @@ "}\n" "}", BreakBeforeBrace); + + verifyFormat("#ifdef _DEBUG\n" + "int foo(int i = 0)\n" + "#else\n" + "int foo(int i = 5)\n" + "#endif\n" + "{\n" + " return i;\n" + "}\n" + "void foo1()\n" + "#ifdef _DEBUG\n" + "{\n" + " foo();\n" + "}\n" + "#else\n" + "{\n" + "}\n" + "#endif", + BreakBeforeBrace); } TEST_F(FormatTest, AllmanBraceBreaking) { @@ -7418,6 +7437,25 @@ "}\n", BreakBeforeBrace); + verifyFormat("#ifdef _DEBUG\n" + "int foo(int i = 0)\n" + "#else\n" + "int foo(int i = 5)\n" + "#endif\n" + "{\n" + " return i;\n" + "}\n" + "void foo1()\n" + "#ifdef _DEBUG\n" + "{\n" + " foo();\n" + "}\n" + "#else\n" + "{\n" + "}\n" + "#endif", + BreakBeforeBrace); + BreakBeforeBrace.ColumnLimit = 19; verifyFormat("void f() { int i; }", BreakBeforeBrace); BreakBeforeBrace.ColumnLimit = 18; @@ -7538,6 +7576,25 @@ " Y = 0,\n" "}\n", GNUBraceStyle); + + verifyFormat("#ifdef _DEBUG\n" + "int foo(int i = 0)\n" + "#else\n" + "int foo(int i = 5)\n" + "#endif\n" + "{\n" + " return i;\n" + "}\n" + "void foo1()\n" + "#ifdef _DEBUG\n" + "{\n" + " foo();\n" + "}\n" + "#else\n" + "{\n" + "}\n" + "#endif", + GNUBraceStyle); } TEST_F(FormatTest, CatchExceptionReferenceBinding) { verifyFormat("void f() {\n"