From b8914dd4715130124f1c51a31a05db1833dcba19 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 20 Mar 2013 09:53:18 +0000 Subject: [PATCH] Improve formatting of function types in template parameters. Before: A; After: A; llvm-svn: 177505 --- clang/lib/Format/TokenAnnotator.cpp | 12 +++++++++--- clang/unittests/Format/FormatTest.cpp | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 4ac4c9b..51fd4e6 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -681,10 +681,15 @@ private: if (PrevToken->FormatTok.Tok.isLiteral() || PrevToken->isOneOf(tok::r_paren, tok::r_square) || - NextToken->FormatTok.Tok.isLiteral() || isUnaryOperator(*NextToken) || - NextToken->isOneOf(tok::l_paren, tok::l_square)) + NextToken->FormatTok.Tok.isLiteral() || isUnaryOperator(*NextToken)) return TT_BinaryOperator; + // "*(" is probably part of a function type if within template parameters. + // Otherwise, it is probably a binary operator. + if (NextToken->is(tok::l_paren)) + return Contexts.back().ContextKind == tok::less ? TT_PointerOrReference + : TT_BinaryOperator; + // It is very unlikely that we are going to find a pointer or reference type // definition on the RHS of an assignment. if (IsExpression) @@ -989,7 +994,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, !Style.PointerBindsToType); if (Left.Type == TT_PointerOrReference) return Right.FormatTok.Tok.isLiteral() || - ((Right.Type != TT_PointerOrReference) && Style.PointerBindsToType); + ((Right.Type != TT_PointerOrReference) && + Right.isNot(tok::l_paren) && Style.PointerBindsToType); if (Right.is(tok::star) && Left.is(tok::l_paren)) return false; if (Left.is(tok::l_square)) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index a940f70..18ba143 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2265,10 +2265,13 @@ TEST_F(FormatTest, FormatsFunctionTypes) { verifyFormat("A a;"); verifyFormat("A a;"); verifyFormat("A a;"); + verifyFormat("A;"); // FIXME: Inconsistent. verifyFormat("int (*func)(void *);"); verifyFormat("void f() { int(*func)(void *); }"); + + verifyGoogleFormat("A;"); } TEST_F(FormatTest, BreaksLongDeclarations) { -- 2.7.4