r174308 - Fix an error in formatting of for-loops.

Daniel Jasper djasper at google.com
Sun Feb 3 23:30:30 PST 2013


Author: djasper
Date: Mon Feb  4 01:30:30 2013
New Revision: 174308

URL: http://llvm.org/viewvc/llvm-project?rev=174308&view=rev
Log:
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) {}

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=174308&r1=174307&r2=174308&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Feb  4 01:30:30 2013
@@ -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;
       }
     }
 

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=174308&r1=174307&r2=174308&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Feb  4 01:30:30 2013
@@ -714,8 +714,10 @@ unsigned TokenAnnotator::splitPenalty(co
       (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.

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=174308&r1=174307&r2=174308&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Feb  4 01:30:30 2013
@@ -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, WrapsTemplateDeclarat
       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
 
   verifyFormat(
-      "a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(a(aaaaaaaaaaaaaaaaaa,\n"
-      "                                                aaaaaaaaaaaaaaaa));");
+      "a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
+      "    a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
 }
 
 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {





More information about the cfe-commits mailing list