]> granicus.if.org Git - clang/commitdiff
clang-format: Improve line breaks at function calls.
authorDaniel Jasper <djasper@google.com>
Fri, 12 Sep 2014 16:35:28 +0000 (16:35 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 12 Sep 2014 16:35:28 +0000 (16:35 +0000)
Before:
  EXPECT_CALL(SomeObject, SomeFunction(Parameter)).Times(2).WillRepeatedly(
      Return(SomeValue));

After:
  EXPECT_CALL(SomeObject, SomeFunction(Parameter))
      .Times(2)
      .WillRepeatedly(Return(SomeValue));

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

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

index afd20b22e3aab1bbc54bf2b5e79c60eab01573ec..0dc65ba28efe15bc9d4ed59aad1188f8d4a4a59b 100644 (file)
@@ -302,6 +302,18 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
   if (startsSegmentOfBuilderTypeCall(Current))
     State.Stack.back().ContainsUnwrappedBuilder = true;
 
+  if (Current.isMemberAccess() && Previous.is(tok::r_paren) &&
+      (Previous.MatchingParen &&
+       (Previous.TotalLength - Previous.MatchingParen->TotalLength > 10))) {
+    // If there is a function call with long parameters, break before trailing
+    // calls. This prevents things like:
+    //   EXPECT_CALL(SomeLongParameter).Times(
+    //       2);
+    // We don't want to do this for short parameters as they can just be
+    // indexes.
+    State.Stack.back().NoLineBreak = true;
+  }
+
   State.Column += Spaces;
   if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) &&
       Previous.Previous && Previous.Previous->isOneOf(tok::kw_if, tok::kw_for))
index f8e318365bff7b2aa39ddcb3334607e3f4299ce9..dfa31085a96718d2e38692140e9c51b3729617a5 100644 (file)
@@ -4452,6 +4452,11 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
 
   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
                "    .WillRepeatedly(Return(SomeValue));");
+  verifyFormat("void f() {\n"
+               "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
+               "      .Times(2)\n"
+               "      .WillRepeatedly(Return(SomeValue));\n"
+               "}");
   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
                "    ccccccccccccccccccccccc);");
   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"