r290259 - clang-format: Fix bug in handling of single-column lists.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 21 09:02:06 PST 2016


Author: djasper
Date: Wed Dec 21 11:02:06 2016
New Revision: 290259

URL: http://llvm.org/viewvc/llvm-project?rev=290259&view=rev
Log:
clang-format: Fix bug in handling of single-column lists.

Members that are themselves wrapped in fake parentheses would lead to
AvoidBinPacking be set on the wrong ParenState.

After:
  vector<int> aaaa = {
      aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
      aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
      aaaaaa.aaaaaaa,
      aaaaaa.aaaaaaa,
      aaaaaa.aaaaaaa,
      aaaaaa.aaaaaaa,
  };

Before we were falling back to bin-packing these.

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

Modified: cfe/trunk/lib/Format/FormatToken.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.cpp?rev=290259&r1=290258&r2=290259&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.cpp (original)
+++ cfe/trunk/lib/Format/FormatToken.cpp Wed Dec 21 11:02:06 2016
@@ -77,6 +77,9 @@ unsigned CommaSeparatedList::formatAfter
   if (State.NextToken == nullptr || !State.NextToken->Previous)
     return 0;
 
+  if (Formats.size() == 1)
+    return 0; // Handled by formatFromToken
+
   // Ensure that we start on the opening brace.
   const FormatToken *LBrace =
       State.NextToken->Previous->getPreviousNonComment();
@@ -93,13 +96,6 @@ unsigned CommaSeparatedList::formatAfter
   // Find the best ColumnFormat, i.e. the best number of columns to use.
   const ColumnFormat *Format = getColumnFormat(RemainingCodePoints);
 
-  // Formatting with 1 Column isn't really a column layout, so we don't need the
-  // special logic here. We can just avoid bin packing any of the parameters.
-  if (Format && Format->Columns == 1) {
-    State.Stack.back().AvoidBinPacking = true;
-    return 0;
-  }
-
   // If no ColumnFormat can be used, the braced list would generally be
   // bin-packed. Add a severe penalty to this so that column layouts are
   // preferred if possible.
@@ -137,7 +133,9 @@ unsigned CommaSeparatedList::formatAfter
 unsigned CommaSeparatedList::formatFromToken(LineState &State,
                                              ContinuationIndenter *Indenter,
                                              bool DryRun) {
-  if (HasNestedBracedList)
+  // Formatting with 1 Column isn't really a column layout, so we don't need the
+  // special logic here. We can just avoid bin packing any of the parameters.
+  if (Formats.size() == 1 || HasNestedBracedList)
     State.Stack.back().AvoidBinPacking = true;
   return 0;
 }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=290259&r1=290258&r2=290259&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Dec 21 11:02:06 2016
@@ -6800,6 +6800,14 @@ TEST_F(FormatTest, FormatsBracedListsInC
                "    aaaaaaaa,\n"
                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
                getLLVMStyleWithColumns(30));
+  verifyFormat("vector<int> aaaa = {\n"
+               "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+               "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+               "    aaaaaa.aaaaaaa,\n"
+               "    aaaaaa.aaaaaaa,\n"
+               "    aaaaaa.aaaaaaa,\n"
+               "    aaaaaa.aaaaaaa,\n"
+               "};");
 }
 
 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {




More information about the cfe-commits mailing list