]> granicus.if.org Git - clang/commitdiff
Prevent line breaks that make stuff hard to read.
authorDaniel Jasper <djasper@google.com>
Mon, 18 Feb 2013 11:05:07 +0000 (11:05 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 18 Feb 2013 11:05:07 +0000 (11:05 +0000)
Before:
aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

After:
aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)
    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)
    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,
                         aaaaaaaaaaaaaaaaaaa,
                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

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

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

index 939211cf65d20a2f972ce0ac7dd504f0a1b003f1..ee3516af0c8ed66b7df9fe903526e773cdf305df 100644 (file)
@@ -251,6 +251,7 @@ public:
     State.VariablePos = 0;
     State.LineContainsContinuedForLoopSection = false;
     State.ParenLevel = 0;
+    State.StartOfLineLevel = State.ParenLevel;
 
     DEBUG({
       DebugTokenState(*State.NextToken);
@@ -384,6 +385,9 @@ private:
     /// \brief The level of nesting inside (), [], <> and {}.
     unsigned ParenLevel;
 
+    /// \brief The \c ParenLevel at the start of this line.
+    unsigned StartOfLineLevel;
+
     /// \brief A stack keeping track of properties applying to parenthesis
     /// levels.
     std::vector<ParenState> Stack;
@@ -401,6 +405,8 @@ private:
         return LineContainsContinuedForLoopSection;
       if (Other.ParenLevel != ParenLevel)
         return Other.ParenLevel < ParenLevel;
+      if (Other.StartOfLineLevel < StartOfLineLevel)
+        return Other.StartOfLineLevel < StartOfLineLevel;
       return Other.Stack < Stack;
     }
   };
@@ -489,6 +495,7 @@ private:
       }
 
       State.Stack.back().LastSpace = State.Column;
+      State.StartOfLineLevel = State.ParenLevel;
       if (Current.is(tok::colon) && Current.Type != TT_ConditionalExpr)
         State.Stack.back().Indent += 2;
     } else {
@@ -772,6 +779,14 @@ private:
         !(State.NextToken->is(tok::r_brace) &&
           State.Stack.back().BreakBeforeClosingBrace))
       return false;
+    // This prevents breaks like:
+    //   ...
+    //   SomeParameter, OtherParameter).DoSomething(
+    //   ...
+    // As they hide "DoSomething" and generally bad for readability.
+    if (State.NextToken->Parent->is(tok::l_paren) &&
+        State.ParenLevel <= State.StartOfLineLevel)
+      return false;
     // Trying to insert a parameter on a new line if there are already more than
     // one parameter on the current line is bin packing.
     if (State.Stack.back().HasMultiParameterLine &&
index a41c713ac51377f6728528cf605055eb3bae0798..fad7998b2cbd2df616d1093669c3bba6b71059fb 100644 (file)
@@ -1457,6 +1457,13 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
                "    .insert(ccccccccccccccccccccccc);");
 
+  verifyGoogleFormat(
+      "aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
+      "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
+      "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
+      "                         aaaaaaaaaaaaaaaaaaa,\n"
+      "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+
   // Here, it is not necessary to wrap at "." or "->".
   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");