clang-format: Wrap complex binary expressions on the RHS of a comma.

Specifically, if the RHS of a comma is a complex binary expression and
spans multiple lines, insert a line break before it. This usually is
often more readable compared to producing a hanging indent. See changes
in FormatTest.cpp for examples.

llvm-svn: 288120
This commit is contained in:
Daniel Jasper 2016-11-29 09:40:32 +00:00
parent 35e4122f48
commit 22ed262c23
2 changed files with 30 additions and 26 deletions

View File

@ -866,7 +866,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
// Exclude relational operators, as there, it is always more desirable to
// have the LHS 'left' of the RHS.
if (Previous && Previous->getPrecedence() != prec::Assignment &&
Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr, tok::comma) &&
Previous->getPrecedence() != prec::Relational) {
bool BreakBeforeOperator =
Previous->is(tok::lessless) ||

View File

@ -4577,12 +4577,13 @@ TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
TEST_F(FormatTest, BreaksConditionalExpressions) {
verifyFormat(
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
"aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat(
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
"aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
" : aaaaaaaaaaaaa);");
@ -4632,11 +4633,12 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
" ? aaaa\n"
" : bbbb;");
verifyFormat("unsigned Indent =\n"
" format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
" ? IndentForLevel[TheLine.Level]\n"
" : TheLine * 2,\n"
" format(TheLine.First,\n"
" IndentForLevel[TheLine.Level] >= 0\n"
" ? IndentForLevel[TheLine.Level]\n"
" : TheLine * 2,\n"
" TheLine.InPPDirective, PreviousEndOfLineColumn);",
getLLVMStyleWithColumns(70));
getLLVMStyleWithColumns(60));
verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
" ? aaaaaaaaaaaaaaa\n"
" : bbbbbbbbbbbbbbb //\n"
@ -4711,13 +4713,14 @@ TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
Style.BreakBeforeTernaryOperators = false;
Style.ColumnLimit = 70;
verifyFormat(
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
"aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
Style);
verifyFormat(
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
"aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
Style);
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
@ -4773,13 +4776,13 @@ TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
" b :\n"
" c);",
Style);
verifyFormat(
"unsigned Indent =\n"
" format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
" IndentForLevel[TheLine.Level] :\n"
" TheLine * 2,\n"
" TheLine.InPPDirective, PreviousEndOfLineColumn);",
Style);
verifyFormat("unsigned Indent =\n"
" format(TheLine.First,\n"
" IndentForLevel[TheLine.Level] >= 0 ?\n"
" IndentForLevel[TheLine.Level] :\n"
" TheLine * 2,\n"
" TheLine.InPPDirective, PreviousEndOfLineColumn);",
Style);
verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
" aaaaaaaaaaaaaaa :\n"
" bbbbbbbbbbbbbbb ? //\n"
@ -5899,11 +5902,12 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyFormat("foo<b & 1>();");
verifyFormat("decltype(*::std::declval<const T &>()) void F();");
verifyFormat(
"template <class T, class = typename std::enable_if<\n"
" std::is_integral<T>::value &&\n"
" (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
"template <class T,\n"
" class = typename std::enable_if<\n"
" std::is_integral<T>::value &&\n"
" (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
"void F();",
getLLVMStyleWithColumns(76));
getLLVMStyleWithColumns(70));
verifyFormat(
"template <class T,\n"
" class = typename ::std::enable_if<\n"