r290094 - clang-format: Slightly tweak the behavior of <<-wrapping.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 19 03:14:24 PST 2016


Author: djasper
Date: Mon Dec 19 05:14:23 2016
New Revision: 290094

URL: http://llvm.org/viewvc/llvm-project?rev=290094&view=rev
Log:
clang-format: Slightly tweak the behavior of <<-wrapping.

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.

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/TokenAnnotator.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=290094&r1=290093&r2=290094&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Mon Dec 19 05:14:23 2016
@@ -460,7 +460,7 @@ unsigned ContinuationIndenter::addTokenO
   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 &&

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=290094&r1=290093&r2=290094&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Dec 19 05:14:23 2016
@@ -2007,8 +2007,13 @@ unsigned TokenAnnotator::splitPenalty(co
   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();

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=290094&r1=290093&r2=290094&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Dec 19 05:14:23 2016
@@ -5092,6 +5092,9 @@ TEST_F(FormatTest, AlignsPipes) {
       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   verifyFormat(
+      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
+      "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
+  verifyFormat(
       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");




More information about the cfe-commits mailing list