]> granicus.if.org Git - clang/commitdiff
clang-format: Avoid line-breaks that increase the current column.
authorDaniel Jasper <djasper@google.com>
Thu, 27 Mar 2014 16:14:13 +0000 (16:14 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 27 Mar 2014 16:14:13 +0000 (16:14 +0000)
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)

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204937 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/ContinuationIndenter.cpp
unittests/Format/FormatTest.cpp

index 55d700fd80537faf8ba5511d240fc034a2b13ebd..a8ab72c4ca560a571039df251bb48f2fdf22620d 100644 (file)
@@ -144,7 +144,13 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
        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(const LineState &State) {
   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::addTokenOnNewLine(LineState &State,
 }
 
 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::getNewLineColumn(const LineState &State) {
     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.
index 949167c8f566970817632d8c9281884d444fb5ef..b5a70760a418785bc5876237cb490ab520f626c8 100644 (file)
@@ -6552,11 +6552,10 @@ TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
                    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"