r175262 - Prevent only breaking before "?" in conditional expressions.
Daniel Jasper
djasper at google.com
Fri Feb 15 03:07:25 PST 2013
Author: djasper
Date: Fri Feb 15 05:07:25 2013
New Revision: 175262
URL: http://llvm.org/viewvc/llvm-project?rev=175262&view=rev
Log:
Prevent only breaking before "?" in conditional expressions.
This is almost always more readable.
Before:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
? aaaaaaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaaaaaaaa;
After:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
? aaaaaaaaaaaaaaaaaaaaaaaaaaa
: aaaaaaaaaaaaaaaaaaaaaaaaaaa;
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=175262&r1=175261&r2=175262&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Feb 15 05:07:25 2013
@@ -290,7 +290,8 @@ private:
: 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 @@ private:
/// \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 @@ private:
return HasMultiParameterLine;
if (ColonPos != Other.ColonPos)
return ColonPos < Other.ColonPos;
+ if (BreakBeforeThirdOperand != Other.BreakBeforeThirdOperand)
+ return BreakBeforeThirdOperand;
return false;
}
};
@@ -465,6 +471,8 @@ private:
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 @@ private:
(State.NextToken->Parent->ClosesTemplateDeclaration &&
State.ParenLevel == 0)))
return true;
+ if (State.NextToken->is(tok::colon) &&
+ State.Stack.back().BreakBeforeThirdOperand)
+ return true;
return false;
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=175262&r1=175261&r2=175262&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Feb 15 05:07:25 2013
@@ -1323,11 +1323,20 @@ TEST_F(FormatTest, BreaksConditionalExpr
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+
+ verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
+
+ // FIXME: The trailing third parameter here is kind of hidden. Prefer putting
+ // it on the next line.
verifyFormat(
- "unsigned Indent = formatFirstToken(\n"
- " TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
- " ? IndentForLevel[TheLine.Level] : TheLine * 2,\n"
- " TheLine.InPPDirective, PreviousEndOfLineColumn);");
+ "unsigned Indent =\n"
+ " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
+ " ? IndentForLevel[TheLine.Level]\n"
+ " : TheLine * 2, TheLine.InPPDirective,\n"
+ " PreviousEndOfLineColumn);", getLLVMStyleWithColumns(70));
+
}
TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
More information about the cfe-commits
mailing list