From: Manuel Klimek Date: Thu, 27 Mar 2014 19:00:52 +0000 (+0000) Subject: Improve handling of bool expressions in template arguments. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d6597d8497c454545c2d6e3756be62b738884d70;p=clang Improve handling of bool expressions in template arguments. Now correctly formats: foo(); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204950 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 42d4a8dc24..b7262f396d 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -282,6 +282,10 @@ struct FormatToken { (!ColonRequired || (Next && Next->is(tok::colon))); } + bool isLiteral() const { + return Tok.isLiteral() || isOneOf(tok::kw_true, tok::kw_false); + } + /// \brief Determine whether the token is a simple-type-specifier. bool isSimpleTypeSpecifier() const; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index ee68afe7dc..c3312cc66b 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -67,7 +67,11 @@ private: // parameters. // FIXME: This is getting out of hand, write a decent parser. if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) && - (CurrentToken->Previous->Type == TT_BinaryOperator || + ((CurrentToken->Previous->Type == TT_BinaryOperator && + // Toplevel bool expressions do not make lots of sense; + // If we're on the top level, it contains only the base context and + // the context for the current opening angle bracket. + Contexts.size() > 2) || Contexts[Contexts.size() - 2].IsExpression) && Line.First->isNot(tok::kw_template)) return false; @@ -858,9 +862,9 @@ private: PrevToken->MatchingParen->Previous->is(tok::kw_typeof)) return TT_PointerOrReference; - if (PrevToken->Tok.isLiteral() || + if (PrevToken->isLiteral() || PrevToken->isOneOf(tok::r_paren, tok::r_square) || - NextToken->Tok.isLiteral() || NextToken->isUnaryOperator() || + NextToken->isLiteral() || NextToken->isUnaryOperator() || // If we know we're in a template argument, there are no named // declarations. Thus, having an identifier on the right-hand side // indicates a binary operator. diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index b5a70760a4..3d1e5a5d8e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -4537,6 +4537,12 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyFormat("vector v;"); verifyFormat("vector v;"); verifyFormat("vector v;"); + verifyFormat("foo();"); + verifyFormat("foo();"); + + // FIXME: We cannot handle this case yet; we might be able to figure out that + // foo d > v; doesn't make sense. + verifyFormat("foo d > v;"); } TEST_F(FormatTest, UnderstandsAttributes) {