r204937 - clang-format: Avoid line-breaks that increase the current column.

Daniel Jasper djasper at google.com
Thu Mar 27 09:14:13 PDT 2014


Author: djasper
Date: Thu Mar 27 11:14:13 2014
New Revision: 204937

URL: http://llvm.org/viewvc/llvm-project?rev=204937&view=rev
Log:
clang-format: Avoid line-breaks that increase the current column.

While these might make sense for some rule (e.g. break after multi-line
operand), they generally appear ugly and confusing.

Before:
  fffffffffff(R\"x(
  multiline raw string literal xxxxxxxxxxxxxx
  )x\" + bbbbbb)

After:
  fffffffffff(R\"x(
  multiline raw string literal xxxxxxxxxxxxxx
  )x\" +
              bbbbbb)

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

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=204937&r1=204936&r2=204937&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Thu Mar 27 11:14:13 2014
@@ -144,7 +144,13 @@ bool ContinuationIndenter::mustBreak(con
        Previous.Type == TT_ArrayInitializerLSquare) &&
       getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State))
     return true;
+  if (Current.Type == TT_CtorInitializerColon &&
+      (!Style.AllowShortFunctionsOnASingleLine ||
+       Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0))
+    return true;
 
+  if (State.Column < getNewLineColumn(State))
+    return false;
   if (!Style.BreakBeforeBinaryOperators) {
     // If we need to break somewhere inside the LHS of a binary expression, we
     // should also break after the operator. Otherwise, the formatting would
@@ -204,10 +210,6 @@ bool ContinuationIndenter::mustBreak(con
   if (Previous.BlockKind == BK_Block && Previous.is(tok::l_brace) &&
       !Current.isOneOf(tok::r_brace, tok::comment))
     return true;
-  if (Current.Type == TT_CtorInitializerColon &&
-      (!Style.AllowShortFunctionsOnASingleLine ||
-       Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0))
-    return true;
 
   return false;
 }
@@ -446,6 +448,8 @@ unsigned ContinuationIndenter::addTokenO
 }
 
 unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
+  if (!State.NextToken || !State.NextToken->Previous)
+    return 0;
   FormatToken &Current = *State.NextToken;
   const FormatToken &Previous = *State.NextToken->Previous;
   // If we are continuing an expression, we want to use the continuation indent.
@@ -528,7 +532,7 @@ unsigned ContinuationIndenter::getNewLin
     return State.FirstIndent + Style.ConstructorInitializerIndentWidth;
   if (NextNonComment->Type == TT_CtorInitializerComma)
     return State.Stack.back().Indent;
-  if (State.Stack.back().Indent == State.FirstIndent &&
+  if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment &&
       PreviousNonComment->isNot(tok::r_brace))
     // Ensure that we fall back to the continuation indent width instead of
     // just flushing continuations left.

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=204937&r1=204936&r2=204937&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Mar 27 11:14:13 2014
@@ -6552,11 +6552,10 @@ TEST_F(FormatTest, CountsCharactersInMul
                    getGoogleStyleWithColumns(20)));
   EXPECT_EQ("fffffffffff(R\"x(\n"
             "multiline raw string literal xxxxxxxxxxxxxx\n"
-            ")x\" +\n"
-            "            bbbbbb);",
+            ")x\" + bbbbbb);",
             format("fffffffffff(R\"x(\n"
                    "multiline raw string literal xxxxxxxxxxxxxx\n"
-                   ")x\" + bbbbbb);",
+                   ")x\" +   bbbbbb);",
                    getGoogleStyleWithColumns(20)));
   EXPECT_EQ("fffffffffff(\n"
             "    R\"x(\n"





More information about the cfe-commits mailing list