From: Daniel Jasper Date: Sat, 1 Jun 2013 18:56:00 +0000 (+0000) Subject: Improve recognition of template parameters. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0348be0c78781c5ddb8c271976812705410c731a;p=clang Improve recognition of template parameters. Before: return a d; After: return a < b && c > d; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183077 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 5b228f8ace..61f43a0201 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -53,8 +53,15 @@ private: if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace, tok::question, tok::colon)) return false; + // If a && or || is found and interpreted as a binary operator, this set + // of angles is like part of something like "a < b && c > d". If the + // angles are inside an expression, the ||/&& might also be a binary + // operator that was misinterpreted because we are parsing template + // parameters. + // FIXME: This is getting out of hand, write a decent parser. if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) && - CurrentToken->Previous->Type != TT_PointerOrReference && + (CurrentToken->Previous->Type == TT_BinaryOperator || + Contexts[Contexts.size() - 2].IsExpression) && Line.First->isNot(tok::kw_template)) return false; updateParameterCount(Left, CurrentToken); diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 0b35abeb9b..24e4769274 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -2893,6 +2893,13 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) { verifyFormat("f();"); verifyFormat("template void f() {}"); + + // Not template parameters. + verifyFormat("return a < b && c > d;"); + verifyFormat("void f() {\n" + " while (a < b && c > d) {\n" + " }\n" + "}"); } TEST_F(FormatTest, UnderstandsBinaryOperators) {