From: Daniel Jasper Date: Mon, 4 Feb 2013 07:30:30 +0000 (+0000) Subject: Fix an error in formatting of for-loops. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8159d2f271c9142b46a672ac2c45821911171a7d;p=clang Fix an error in formatting of for-loops. Two minor changes: * Slight penalty for breaking at "," as opposed to ";". * Don't apply bin-packing rules to for-loops. Before: for (int aaaaaa = aaaaaaaaaa; aaaaaa < bbbbbbbb; ++aaaaaa, ++ccccccccccccccc) {} After: for (int aaaaaa = aaaaaaaaaa; aaaaaa < bbbbbbbb; ++aaaaaa, ++ccccccccccccccc) {} git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174308 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 6d227cca62..2f14fc0ce1 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -493,7 +493,8 @@ private: if (Newline && Previous.is(tok::l_brace)) State.Stack.back().BreakBeforeClosingBrace = true; - if (State.Stack.back().AvoidBinPacking && Newline) { + if (State.Stack.back().AvoidBinPacking && Newline && + (Line.First.isNot(tok::kw_for) || ParenLevel != 1)) { // If we are breaking after '(', '{', '<', this is not bin packing unless // AllowAllParametersOfDeclarationOnNextLine is false. if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace) && @@ -505,7 +506,8 @@ private: // Any break on this level means that the parent level has been broken // and we need to avoid bin packing there. for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { - State.Stack[i].BreakAfterComma = true; + if (Line.First.isNot(tok::kw_for) || i != 1) + State.Stack[i].BreakAfterComma = true; } } diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 1eb7021ac6..0676df6c0e 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -714,8 +714,10 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedToken &Tok) { (Left.isNot(tok::comma) && Left.isNot(tok::semi))) return 20; - if (Left.is(tok::semi) || Left.is(tok::comma)) + if (Left.is(tok::semi)) return 0; + if (Left.is(tok::comma)) + return 1; // In Objective-C method expressions, prefer breaking before "param:" over // breaking after it. diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 7b0f1d61d3..28d19d5ac5 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -267,6 +267,19 @@ TEST_F(FormatTest, FormatsForLoop) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" " ++aaaaaaaaaaa) {\n}"); + + verifyGoogleFormat( + "for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" + " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" + "}"); + verifyGoogleFormat( + "for (int aaaaaaaaaaa = 1;\n" + " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaa);\n" + " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" + "}"); } TEST_F(FormatTest, RangeBasedForLoops) { @@ -1357,8 +1370,8 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat( - "a(a(aaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaa));"); + "a(\n" + " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); } TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {