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
This commit is contained in:
Daniel Jasper 2015-05-10 21:15:07 +00:00
parent 117339a234
commit 3eb01df09c
3 changed files with 14 additions and 9 deletions

View 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.

View 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,

View 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(