]> granicus.if.org Git - clang/commitdiff
clang-format: Slightly tweak the behavior of <<-wrapping.
authorDaniel Jasper <djasper@google.com>
Mon, 19 Dec 2016 11:14:23 +0000 (11:14 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 19 Dec 2016 11:14:23 +0000 (11:14 +0000)
Before:
  SomeLongLoggingStatementOrMacro() << "Some long text "
                                    << some_variable << "\n";

Before:
  SomeLongLoggingStatementOrMacro()
      << "Some long text " << some_variable << "\n";

Short logging statements are already special cased in a different part
of the code.

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

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

index aa6f37bd199de4d494e7e7309b538cf67b62c80b..bf075ab6d53eabfdbb61e88162cb8dc59770f07b 100644 (file)
@@ -460,7 +460,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
   Penalty += State.NextToken->SplitPenalty;
 
   // Breaking before the first "<<" is generally not desirable if the LHS is
-  // short. Also always add the penalty if the LHS is split over mutliple lines
+  // short. Also always add the penalty if the LHS is split over multiple lines
   // to avoid unnecessary line breaks that just work around this penalty.
   if (NextNonComment->is(tok::lessless) &&
       State.Stack.back().FirstLessLess == 0 &&
index 77b6a5d4d26bba42213a0cde4c93242b6512bbd1..5dcaf2651ff8e03b3b72d157f4732db8681d4595 100644 (file)
@@ -2007,8 +2007,13 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
   if (Right.isOneOf(tok::lessless, tok::plus) && Left.isLabelString() &&
       (Right.NextOperator || Right.OperatorIndex != 1))
     return 25;
-  if (Right.is(tok::lessless))
-    return 1; // Breaking at a << is really cheap.
+  if (Right.is(tok::lessless)) {
+    // Breaking at a << is really cheap.
+    if (!Left.is(tok::r_paren) || Right.OperatorIndex > 0)
+      // Slightly prefer to break before the first one in log-like statements.
+      return 2;
+    return 1;
+  }
   if (Left.is(TT_ConditionalExpr))
     return prec::Conditional;
   prec::Level Level = Left.getPrecedence();
index 9c2c25d0910ae1dffb54079c19d3e8138af44e28..af784197062d3f302f52ef6d0aebe7235ae3d5c9 100644 (file)
@@ -5091,6 +5091,9 @@ TEST_F(FormatTest, AlignsPipes) {
   verifyFormat(
       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
+  verifyFormat(
+      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
+      "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   verifyFormat(
       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"