clang-format: Improve constructor initializer linewrapping.
Specifically make ConstructorInitializerAllOnOneLineOrOnePerLine work
nicely with BreakConstructorInitializersBeforeComma.
This fixes llvm.org/PR17395.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@192168 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp
index 6a5f876..ff1f1aa 100644
--- a/lib/Format/ContinuationIndenter.cpp
+++ b/lib/Format/ContinuationIndenter.cpp
@@ -44,6 +44,18 @@
return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
}
+// Returns \c true if \c Current starts a new parameter.
+static bool startsNextParameter(const FormatToken &Current,
+ const FormatStyle &Style) {
+ const FormatToken &Previous = *Current.Previous;
+ if (Current.Type == TT_CtorInitializerComma &&
+ Style.BreakConstructorInitializersBeforeComma)
+ return true;
+ return Previous.is(tok::comma) && !Current.isTrailingComment() &&
+ (Previous.Type != TT_CtorInitializerComma ||
+ !Style.BreakConstructorInitializersBeforeComma);
+}
+
ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
SourceManager &SourceMgr,
WhitespaceManager &Whitespaces,
@@ -113,15 +125,9 @@
return true;
if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
return true;
- if (Style.BreakConstructorInitializersBeforeComma) {
- if (Previous.Type == TT_CtorInitializerComma)
- return false;
- if (Current.Type == TT_CtorInitializerComma)
- return true;
- }
- if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
- (Current.Type == TT_ConditionalExpr &&
- !(Current.is(tok::colon) && Previous.is(tok::question)))) &&
+ if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
+ Current.is(tok::question) ||
+ (Current.Type == TT_ConditionalExpr && Previous.isNot(tok::question))) &&
State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
!Current.isOneOf(tok::r_paren, tok::r_brace))
return true;
@@ -250,8 +256,7 @@
if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
Current.Type != TT_LineComment)
State.Stack.back().Indent = State.Column + Spaces;
- if (Previous.is(tok::comma) && !Current.isTrailingComment() &&
- State.Stack.back().AvoidBinPacking)
+ if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
State.Stack.back().NoLineBreak = true;
if (startsSegmentOfBuilderTypeCall(Current))
State.Stack.back().ContainsUnwrappedBuilder = true;
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index 220da70..0e06d3b 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -1366,7 +1366,8 @@
// FIXME: Fix horrible hack of using BindingStrength to find top-level <>.
return true;
} else if (Right.Type == TT_CtorInitializerComma &&
- Style.BreakConstructorInitializersBeforeComma) {
+ Style.BreakConstructorInitializersBeforeComma &&
+ !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
return true;
} else if (Right.Previous->BlockKind == BK_Block &&
Right.Previous->isNot(tok::r_brace) &&
@@ -1450,6 +1451,9 @@
if (Left.Type == TT_CtorInitializerComma &&
Style.BreakConstructorInitializersBeforeComma)
return false;
+ if (Right.Type == TT_CtorInitializerComma &&
+ Style.BreakConstructorInitializersBeforeComma)
+ return true;
if (Right.isBinaryOperator() && Style.BreakBeforeBinaryOperators)
return true;
if (Left.is(tok::greater) && Right.is(tok::greater) &&