]> granicus.if.org Git - clang/commitdiff
clang-format: Improve wrapping of << operators.
authorDaniel Jasper <djasper@google.com>
Sun, 10 May 2015 21:15:07 +0000 (21:15 +0000)
committerDaniel Jasper <djasper@google.com>
Sun, 10 May 2015 21:15:07 +0000 (21:15 +0000)
Before:
  llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
                      aaaaaaaaaaaaaaaa) << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;

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

Also, cleanup and simplify the operator wrapping logic.

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

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

index ee06bc6cb5b856b209cf0e227c63c8b1e4a05392..1c262c2be92b71903d361d54ec1f887caaf57a20 100644 (file)
@@ -172,7 +172,11 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
 
   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(const LineState &State) {
                         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.
index 54951abade07236f7bffff34b19b1c243f2130a1..85e5eb2ceb4ce8e94ea3bb6197cb97b69b75ecba 100644 (file)
@@ -47,8 +47,8 @@ enum TokenType {
   TT_FunctionTypeLParen,
   TT_ImplicitStringLiteral,
   TT_InheritanceColon,
-  TT_InlineASMColon,
   TT_InlineASMBrace,
+  TT_InlineASMColon,
   TT_JavaAnnotation,
   TT_JsTypeColon,
   TT_JsTypeOptionalQuestion,
index ce14165bc881e245e52e73092e4fa7c6313e6149..a70cab1096f4603ea03c0b2eda03760ee760062f 100644 (file)
@@ -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(