r195546 - clang-format: Prefer column layout if possible.

Daniel Jasper djasper at google.com
Sat Nov 23 02:23:00 PST 2013


Author: djasper
Date: Sat Nov 23 04:22:59 2013
New Revision: 195546

URL: http://llvm.org/viewvc/llvm-project?rev=195546&view=rev
Log:
clang-format: Prefer column layout if possible.

Add a severe penalty for not using column layout for braced lists. If
there are solutions with column layout, these are generally preferable
over bin-packed solutions.

Before:
  std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{ aaaaaaa, aaaaaaaaaa, aaaaa,
                                             aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,
                                             aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,
                                             aaaaaaaaaaaaaaaaaaa +
                                                 aaaaaaaaaaaaaaaaaaa,
                                             aaaaaaa, a };
After:
  std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{
    aaaaaaa,      aaaaaaaaaa,
    aaaaa,        aaaaaaaaaaaaaaa,
    aaa,          aaaaaaaaaa,
    a,            aaaaaaaaaaaaaaaaaaaaa,
    aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,
    aaaaaaa,      a
  };

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=195546&r1=195545&r2=195546&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.cpp (original)
+++ cfe/trunk/lib/Format/FormatToken.cpp Sat Nov 23 04:22:59 2013
@@ -48,8 +48,11 @@ unsigned CommaSeparatedList::format(Line
 
   // Find the best ColumnFormat, i.e. the best number of columns to use.
   const ColumnFormat *Format = getColumnFormat(RemainingCodePoints);
+  // 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
+  // prefered if possible.
   if (!Format)
-    return 0;
+    return 10000;
 
   // Format the entire list.
   unsigned Penalty = 0;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=195546&r1=195545&r2=195546&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sat Nov 23 04:22:59 2013
@@ -4698,6 +4698,14 @@ TEST_F(FormatTest, LayoutCxx11Constructo
     verifyFormat(
         "std::this_thread::sleep_for(\n"
         "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);");
+    verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
+                 "  aaaaaaa,      aaaaaaaaaa,\n"
+                 "  aaaaa,        aaaaaaaaaaaaaaa,\n"
+                 "  aaa,          aaaaaaaaaa,\n"
+                 "  a,            aaaaaaaaaaaaaaaaaaaaa,\n"
+                 "  aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
+                 "  aaaaaaa,      a\n"
+                 "};");
 
     FormatStyle NoSpaces = getLLVMStyle();
     NoSpaces.Cpp11BracedListStyle = true;





More information about the cfe-commits mailing list