[cfe-commits] r171964 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Daniel Jasper djasper at google.com
Wed Jan 9 02:16:05 PST 2013


Author: djasper
Date: Wed Jan  9 04:16:05 2013
New Revision: 171964

URL: http://llvm.org/viewvc/llvm-project?rev=171964&view=rev
Log:
Don't simply give up when exceeding 80cols, choose an "ok" option.

This addresses llvm.org/PR14847.

We can now format something like:
int aaaaaaaaaaaaaaaaaaaaaaaaaaa =
    // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;

clang-format unavoidably exceeds the column limit, but does not just
flush everything into a single line. Moreover, it tries to minimize the
number of characters beyond the column limit.

Modified:
    cfe/trunk/lib/Format/Format.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=171964&r1=171963&r2=171964&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Jan  9 04:16:05 2013
@@ -116,6 +116,7 @@
 struct OptimizationParameters {
   unsigned PenaltyIndentLevel;
   unsigned PenaltyLevelDecrease;
+  unsigned PenaltyExcessCharacter;
 };
 
 class UnwrappedLineFormatter {
@@ -131,6 +132,7 @@
         Replaces(Replaces), StructuralError(StructuralError) {
     Parameters.PenaltyIndentLevel = 15;
     Parameters.PenaltyLevelDecrease = 30;
+    Parameters.PenaltyExcessCharacter = 1000000;
   }
 
   /// \brief Formats an \c UnwrappedLine.
@@ -419,6 +421,10 @@
     return 3;
   }
 
+  unsigned getColumnLimit() {
+    return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0);
+  }
+
   /// \brief Calculate the number of lines needed to format the remaining part
   /// of the unwrapped line.
   ///
@@ -454,9 +460,11 @@
 
     addTokenToState(NewLine, true, State);
 
-    // Exceeding column limit is bad.
-    if (State.Column > Style.ColumnLimit - (Line.InPPDirective ? 1 : 0))
-      return UINT_MAX;
+    // Exceeding column limit is bad, assign penalty.
+    if (State.Column > getColumnLimit()) {
+      unsigned ExcessCharacters = State.Column - getColumnLimit();
+      CurrentPenalty += Parameters.PenaltyExcessCharacter * ExcessCharacters;
+    }
 
     if (StopAt <= CurrentPenalty)
       return UINT_MAX;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=171964&r1=171963&r2=171964&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan  9 04:16:05 2013
@@ -1026,6 +1026,15 @@
 // Error recovery tests.
 //===----------------------------------------------------------------------===//
 
+TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
+  verifyFormat("int aaaaaaaa =\n"
+               "    // Overly long comment\n"
+               "    b;", getLLVMStyleWithColumns(20));
+  verifyFormat("function(\n"
+               "    ShortArgument,\n"
+               "    LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
+}
+
 TEST_F(FormatTest, IncorrectAccessSpecifier) {
   verifyFormat("public:");
   verifyFormat("class A {\n"





More information about the cfe-commits mailing list