]> granicus.if.org Git - clang/commitdiff
Improve recognition of template parameters.
authorDaniel Jasper <djasper@google.com>
Sat, 1 Jun 2013 18:56:00 +0000 (18:56 +0000)
committerDaniel Jasper <djasper@google.com>
Sat, 1 Jun 2013 18:56:00 +0000 (18:56 +0000)
Before: return a<b &&c> d;
After:  return a < b && c > d;

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183077 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 5b228f8acea30ed5f0a846d0d441fd8e75fa4092..61f43a02010092937b3d91f0d3e959a15323cb8a 100644 (file)
@@ -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);
index 0b35abeb9b000043032e8d60788bf3e6164c4d46..24e4769274fff8b2a9e374536b3f09f4fb759e7d 100644 (file)
@@ -2893,6 +2893,13 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
 
   verifyFormat("f<int>();");
   verifyFormat("template <typename T> 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) {