Prevent only breaking before "?" in conditional expressions.
This is almost always more readable.
Before:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
? aaaaaaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaaaaaaaa;
After:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
? aaaaaaaaaaaaaaaaaaaaaaaaaaa
: aaaaaaaaaaaaaaaaaaaaaaaaaaa;
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175262 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 92c138a..4b63aa9 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -290,7 +290,8 @@
: Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
BreakBeforeClosingBrace(false), QuestionColumn(0),
AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
- HasMultiParameterLine(HasMultiParameterLine), ColonPos(0) {
+ HasMultiParameterLine(HasMultiParameterLine), ColonPos(0),
+ BreakBeforeThirdOperand(false) {
}
/// \brief The position to which a specific parenthesis level needs to be
@@ -333,6 +334,9 @@
/// \brief The position of the colon in an ObjC method declaration/call.
unsigned ColonPos;
+
+ /// \brief Break before third operand in ternary expression.
+ bool BreakBeforeThirdOperand;
bool operator<(const ParenState &Other) const {
if (Indent != Other.Indent)
@@ -353,6 +357,8 @@
return HasMultiParameterLine;
if (ColonPos != Other.ColonPos)
return ColonPos < Other.ColonPos;
+ if (BreakBeforeThirdOperand != Other.BreakBeforeThirdOperand)
+ return BreakBeforeThirdOperand;
return false;
}
};
@@ -465,6 +471,8 @@
State.Column = State.Stack.back().Indent;
}
+ if (Current.is(tok::question))
+ State.Stack.back().BreakBeforeThirdOperand = true;
if (Previous.is(tok::comma) && !State.Stack.back().AvoidBinPacking)
State.Stack.back().BreakBeforeParameter = false;
@@ -794,6 +802,9 @@
(State.NextToken->Parent->ClosesTemplateDeclaration &&
State.ParenLevel == 0)))
return true;
+ if (State.NextToken->is(tok::colon) &&
+ State.Stack.back().BreakBeforeThirdOperand)
+ return true;
return false;
}