r236960 - clang-format: Improve wrapping of << operators.

Daniel Jasper djasper at google.com
Sun May 10 14:15:08 PDT 2015


Author: djasper
Date: Sun May 10 16:15:07 2015
New Revision: 236960

URL: http://llvm.org/viewvc/llvm-project?rev=236960&view=rev
Log:
clang-format: Improve wrapping of << operators.

Before:
  llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
                      aaaaaaaaaaaaaaaa) << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;

After:
  llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
                      aaaaaaaaaaaaaaaa)
               << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;

Also, cleanup and simplify the operator wrapping logic.

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/FormatToken.h
    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=236960&r1=236959&r2=236960&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Sun May 10 16:15:07 2015
@@ -172,7 +172,11 @@ bool ContinuationIndenter::mustBreak(con
 
   if (State.Column < getNewLineColumn(State))
     return false;
-  if (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None) {
+
+  // Using CanBreakBefore here and below takes care of the decision whether the
+  // current style uses wrapping before or after operators for the given
+  // operator.
+  if (Previous.is(TT_BinaryOperator) && Current.CanBreakBefore) {
     // If we need to break somewhere inside the LHS of a binary expression, we
     // should also break after the operator. Otherwise, the formatting would
     // hide the operator precedence, e.g. in:
@@ -188,16 +192,13 @@ bool ContinuationIndenter::mustBreak(con
                         Previous.Previous->isNot(TT_BinaryOperator); // For >>.
     bool LHSIsBinaryExpr =
         Previous.Previous && Previous.Previous->EndsBinaryExpression;
-    if (Previous.is(TT_BinaryOperator) && (!IsComparison || LHSIsBinaryExpr) &&
-        Current.isNot(TT_BinaryOperator) && // For >>.
-        !Current.isTrailingComment() && !Previous.is(tok::lessless) &&
+    if ((!IsComparison || LHSIsBinaryExpr) && !Current.isTrailingComment() &&
         Previous.getPrecedence() != prec::Assignment &&
         State.Stack.back().BreakBeforeParameter)
       return true;
-  } else {
-    if (Current.is(TT_BinaryOperator) && Previous.EndsBinaryExpression &&
-        State.Stack.back().BreakBeforeParameter)
-      return true;
+  } else if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore &&
+             State.Stack.back().BreakBeforeParameter) {
+    return true;
   }
 
   // Same as above, but for the first "<<" operator.

Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=236960&r1=236959&r2=236960&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Sun May 10 16:15:07 2015
@@ -47,8 +47,8 @@ enum TokenType {
   TT_FunctionTypeLParen,
   TT_ImplicitStringLiteral,
   TT_InheritanceColon,
-  TT_InlineASMColon,
   TT_InlineASMBrace,
+  TT_InlineASMColon,
   TT_JavaAnnotation,
   TT_JsTypeColon,
   TT_JsTypeOptionalQuestion,

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=236960&r1=236959&r2=236960&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun May 10 16:15:07 2015
@@ -4876,6 +4876,10 @@ TEST_F(FormatTest, AlignsPipes) {
       "}");
   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
+  verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+               "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+               "                    aaaaaaaaaaaaaaaaaaaaa)\n"
+               "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
 
   // Breaking before the first "<<" is generally not desirable.
   verifyFormat(





More information about the cfe-commits mailing list