clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
After:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT)
.StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH)
.Default(ORDER_TEXT);
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189353 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp
index ed5098b..199fc02 100644
--- a/lib/Format/ContinuationIndenter.cpp
+++ b/lib/Format/ContinuationIndenter.cpp
@@ -47,6 +47,12 @@
return false;
}
+// Returns \c true if \c Tok is the "." or "->" of a call and starts the next
+// segment of a builder type call.
+static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
+ return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
+}
+
ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
SourceManager &SourceMgr,
const AnnotatedLine &Line,
@@ -99,6 +105,8 @@
// As they hide "DoSomething" and are generally bad for readability.
if (Previous.opensScope() && State.LowestLevelOnLine < State.StartOfLineLevel)
return false;
+ if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
+ return false;
return !State.Stack.back().NoLineBreak;
}
@@ -178,6 +186,9 @@
Line.MightBeFunctionDecl && State.Stack.back().BreakBeforeParameter &&
State.ParenLevel == 0)
return true;
+ if (startsSegmentOfBuilderTypeCall(Current) &&
+ State.Stack.back().CallContinuation != 0)
+ return true;
return false;
}
@@ -232,8 +243,7 @@
} else if (Current.is(tok::lessless) &&
State.Stack.back().FirstLessLess != 0) {
State.Column = State.Stack.back().FirstLessLess;
- } else if (Current.isOneOf(tok::period, tok::arrow) &&
- Current.Type != TT_DesignatedInitializerPeriod) {
+ } else if (Current.isMemberAccess()) {
if (State.Stack.back().CallContinuation == 0) {
State.Column = ContinuationIndent;
State.Stack.back().CallContinuation = State.Column;
@@ -299,8 +309,7 @@
if (!Current.isTrailingComment())
State.Stack.back().LastSpace = State.Column;
- if (Current.isOneOf(tok::arrow, tok::period) &&
- Current.Type != TT_DesignatedInitializerPeriod)
+ if (Current.isMemberAccess())
State.Stack.back().LastSpace += Current.CodePointCount;
State.StartOfLineLevel = State.ParenLevel;
State.LowestLevelOnLine = State.ParenLevel;
@@ -369,6 +378,8 @@
if (Previous.is(tok::comma) && !Current.isTrailingComment() &&
State.Stack.back().AvoidBinPacking)
State.Stack.back().NoLineBreak = true;
+ if (startsSegmentOfBuilderTypeCall(Current))
+ State.Stack.back().ContainsUnwrappedBuilder = true;
State.Column += Spaces;
if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
@@ -401,8 +412,7 @@
bool HasTrailingCall = false;
if (Previous.MatchingParen) {
const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
- if (Next && Next->isOneOf(tok::period, tok::arrow))
- HasTrailingCall = true;
+ HasTrailingCall = Next && Next->isMemberAccess();
}
if (HasMultipleParameters ||
(HasTrailingCall &&
@@ -431,7 +441,7 @@
if (!Current.opensScope() && !Current.closesScope())
State.LowestLevelOnLine =
std::min(State.LowestLevelOnLine, State.ParenLevel);
- if (Current.isOneOf(tok::period, tok::arrow))
+ if (Current.isMemberAccess())
State.Stack.back().StartOfFunctionCall =
Current.LastInChainOfCalls ? 0 : State.Column + Current.CodePointCount;
if (Current.Type == TT_CtorInitializerColon) {