clang-format: Don't break empty 2nd operand of ternary expr.

Before:
  some_quite_long_variable_name_ptr
      ?
      : argv[9] ? ptr : argv[8] ? : argv[7] ? ptr : argv[6];
After:
  some_quite_long_variable_name_ptr
      ?: argv[9] ? ptr : argv[8] ?: argv[7] ? ptr : argv[6];

Patch by Adam Strzelecki, thank you!!

This fixed llvm.org/PR16758.

llvm-svn: 187622
This commit is contained in:
Daniel Jasper 2013-08-01 22:05:00 +00:00
parent 975abffee7
commit b1ae734ffc
3 changed files with 25 additions and 5 deletions

View File

@ -1237,7 +1237,8 @@ private:
return true;
}
if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
Current.Type == TT_ConditionalExpr) &&
(Current.Type == TT_ConditionalExpr &&
!(Current.is(tok::colon) && Previous.is(tok::question)))) &&
State.Stack.back().BreakBeforeParameter &&
!Current.isTrailingComment() &&
!Current.isOneOf(tok::r_paren, tok::r_brace))

View File

@ -1158,7 +1158,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if (Left.is(tok::colon))
return Left.Type != TT_ObjCMethodExpr;
if (Right.is(tok::colon))
return Right.Type != TT_ObjCMethodExpr;
return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question);
if (Left.is(tok::l_paren))
return false;
if (Right.is(tok::l_paren)) {
@ -1220,7 +1220,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
return false;
if (Tok.is(tok::colon))
return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr;
Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
!Tok.Previous->is(tok::question);
if (Tok.Previous->Type == TT_UnaryOperator ||
Tok.Previous->Type == TT_CastRParen)
return false;
@ -1260,7 +1261,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
return true;
if (Left.ClosesTemplateDeclaration)
return true;
if (Right.Type == TT_ConditionalExpr || Right.is(tok::question))
if ((Right.Type == TT_ConditionalExpr &&
!(Right.is(tok::colon) && Left.is(tok::question))) ||
Right.is(tok::question))
return true;
if (Right.Type == TT_RangeBasedForLoopColon ||
Right.Type == TT_OverloadedOperatorLParen)

View File

@ -2921,6 +2921,10 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
" aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaa);");
verifyFormat(
"aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaa);");
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
@ -2933,7 +2937,11 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
@ -2978,6 +2986,14 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
" : aaaaaaaaaaaaaaa);\n"
"}",
NoBinPacking);
verifyFormat(
"void f() {\n"
" g(aaa,\n"
" aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" ?: aaaaaaaaaaaaaaa);\n"
"}",
NoBinPacking);
}
TEST_F(FormatTest, DeclarationsOfMultipleVariables) {