clang-format: More eagerly wrap trailing return types.

Before:
  template <typename T>
  auto aaaaaaaaaaaaaaaaaaaaaa(T t) -> decltype(eaaaaaaaaaaaaaaa<T>(t.a)
                                                   .aaaaaaaa());

After:
  template <typename T>
  auto aaaaaaaaaaaaaaaaaaaaaa(T t)
      -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());

Also add a test case for a difficult template parsing case I stumbled accross.
Needs fixing.

llvm-svn: 239149
This commit is contained in:
Daniel Jasper 2015-06-05 13:18:09 +00:00
parent f6a1312f1b
commit 6f2b88a398
4 changed files with 13 additions and 8 deletions

View File

@ -329,7 +329,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
State.Column > getNewLineColumn(State)) State.Column > getNewLineColumn(State))
State.Stack.back().ContainsUnwrappedBuilder = true; State.Stack.back().ContainsUnwrappedBuilder = true;
if (Current.is(TT_LambdaArrow)) if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java)
State.Stack.back().NoLineBreak = true; State.Stack.back().NoLineBreak = true;
if (Current.isMemberAccess() && Previous.is(tok::r_paren) && if (Current.isMemberAccess() && Previous.is(tok::r_paren) &&
(Previous.MatchingParen && (Previous.MatchingParen &&

View File

@ -754,8 +754,8 @@ private:
// recovered from an error (e.g. failure to find the matching >). // recovered from an error (e.g. failure to find the matching >).
if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro, if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro,
TT_FunctionLBrace, TT_ImplicitStringLiteral, TT_FunctionLBrace, TT_ImplicitStringLiteral,
TT_InlineASMBrace, TT_JsFatArrow, TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow,
TT_RegexLiteral, TT_TrailingReturnArrow)) TT_RegexLiteral))
CurrentToken->Type = TT_Unknown; CurrentToken->Type = TT_Unknown;
CurrentToken->Role.reset(); CurrentToken->Role.reset();
CurrentToken->MatchingParen = nullptr; CurrentToken->MatchingParen = nullptr;
@ -844,6 +844,8 @@ private:
Contexts.back().IsExpression = true; Contexts.back().IsExpression = true;
} else if (Current.is(TT_TrailingReturnArrow)) { } else if (Current.is(TT_TrailingReturnArrow)) {
Contexts.back().IsExpression = false; Contexts.back().IsExpression = false;
} else if (Current.is(TT_LambdaArrow)) {
Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
} else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration && } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
!Line.InPPDirective && !Line.InPPDirective &&
(!Current.Previous || (!Current.Previous ||
@ -1646,7 +1648,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
} }
if (Right.is(TT_PointerOrReference)) if (Right.is(TT_PointerOrReference))
return 190; return 190;
if (Right.is(TT_TrailingReturnArrow)) if (Right.is(TT_LambdaArrow))
return 110; return 110;
if (Left.is(tok::equal) && Right.is(tok::l_brace)) if (Left.is(tok::equal) && Right.is(tok::l_brace))
return 150; return 150;
@ -1901,8 +1903,6 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
} else if (Style.Language == FormatStyle::LK_Java) { } else if (Style.Language == FormatStyle::LK_Java) {
if (Left.is(tok::r_square) && Right.is(tok::l_brace)) if (Left.is(tok::r_square) && Right.is(tok::l_brace))
return true; return true;
if (Left.is(TT_LambdaArrow) || Right.is(TT_LambdaArrow))
return true;
if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
return Style.SpaceBeforeParens != FormatStyle::SBPO_Never; return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private, if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
@ -1927,7 +1927,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
(Right.is(tok::equal) || Left.is(tok::equal))) (Right.is(tok::equal) || Left.is(tok::equal)))
return false; return false;
if (Right.is(TT_TrailingReturnArrow) || Left.is(TT_TrailingReturnArrow)) if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow))
return true; return true;
if (Left.is(tok::comma)) if (Left.is(tok::comma))
return true; return true;

View File

@ -953,7 +953,7 @@ bool UnwrappedLineParser::tryToParseLambda() {
nextToken(); nextToken();
break; break;
case tok::arrow: case tok::arrow:
FormatTok->Type = TT_TrailingReturnArrow; FormatTok->Type = TT_LambdaArrow;
nextToken(); nextToken();
break; break;
default: default:

View File

@ -3956,6 +3956,9 @@ TEST_F(FormatTest, TrailingReturnType) {
verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
" -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
verifyFormat("template <typename T>\n"
"auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
" -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
// Not trailing return types. // Not trailing return types.
verifyFormat("void f() { auto a = b->c(); }"); verifyFormat("void f() { auto a = b->c(); }");
@ -6606,6 +6609,7 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
// FIXME: This is still incorrectly handled at the formatter side. // FIXME: This is still incorrectly handled at the formatter side.
verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
verifyFormat("int i = SomeFunction(a<b, a> b);");
// FIXME: // FIXME:
// This now gets parsed incorrectly as class definition. // This now gets parsed incorrectly as class definition.