From: Daniel Jasper Date: Sun, 10 May 2015 21:15:07 +0000 (+0000) Subject: clang-format: Improve wrapping of << operators. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3eb01df09cf988d630dc9ba5cde84d044bd25855;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236960 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index ee06bc6cb5..1c262c2be9 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -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. diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 54951abade..85e5eb2ceb 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -47,8 +47,8 @@ enum TokenType { TT_FunctionTypeLParen, TT_ImplicitStringLiteral, TT_InheritanceColon, - TT_InlineASMColon, TT_InlineASMBrace, + TT_InlineASMColon, TT_JavaAnnotation, TT_JsTypeColon, TT_JsTypeOptionalQuestion, diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index ce14165bc8..a70cab1096 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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(