Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -559,7 +559,7 @@ Limit -= 2; unsigned MergedLines = 0; - if (MergeShortFunctions) { + if (MergeShortFunctions && !I[1]->First->MustBreakBefore) { MergedLines = tryMergeSimpleBlock(I + 1, E, Limit); // If we managed to merge the block, count the function header, which is // on a separate line. @@ -664,7 +664,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 +674,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; @@ -698,6 +697,8 @@ bool nextTwoLinesFitInto(SmallVectorImpl::const_iterator I, unsigned Limit) { + if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore) + return false; return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit; } Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -7361,6 +7361,31 @@ "}\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\n" + "void foobar() { foo(); }\n" + "#ifdef _DEBUG\n" + "void bar() {}\n" + "#else\n" + "void bar() { foobar(); }\n" + "#endif", + BreakBeforeBrace); } TEST_F(FormatTest, AllmanBraceBreaking) { @@ -7443,6 +7468,31 @@ "}\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\n" + "void foobar() { foo(); }\n" + "#ifdef _DEBUG\n" + "void bar() {}\n" + "#else\n" + "void bar() { foobar(); }\n" + "#endif", + BreakBeforeBrace); + BreakBeforeBrace.ColumnLimit = 19; verifyFormat("void f() { int i; }", BreakBeforeBrace); BreakBeforeBrace.ColumnLimit = 18; @@ -7563,6 +7613,31 @@ " 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\n" + "void foobar() { foo(); }\n" + "#ifdef _DEBUG\n" + "void bar() {}\n" + "#else\n" + "void bar() { foobar(); }\n" + "#endif", + GNUBraceStyle); } TEST_F(FormatTest, CatchExceptionReferenceBinding) { verifyFormat("void f() {\n"