johannes updated this revision to Diff 112671.
johannes retitled this revision from "[AST] Make 
TraverseTemplateParameterListHelper public" to "[AST] Traverse templates in 
LexicallyOrderedRecursiveASTVisitor".
johannes edited the summary of this revision.
johannes added a reviewer: arphaman.
johannes added a comment.
Herald added a subscriber: klimek.

use LexicallyOrderedRecursiveASTVisitor


https://reviews.llvm.org/D36998

Files:
  include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h
  include/clang/AST/RecursiveASTVisitor.h
  unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp


Index: unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
===================================================================
--- unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
+++ unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
@@ -64,7 +64,7 @@
       OS << ND->getNameAsString();
     else
       OS << "???";
-    if (isa<DeclContext>(D))
+    if (isa<DeclContext>(D) or isa<TemplateDecl>(D))
       OS << "/";
   }
   Matcher.match(OS.str(), D);
@@ -138,4 +138,14 @@
   EXPECT_TRUE(Visitor.runOver(Source, DummyMatchVisitor::Lang_OBJC));
 }
 
+TEST(LexicallyOrderedRecursiveASTVisitor, VisitTemplateDecl) {
+  StringRef Source = R"(
+template <class T> T f();
+)";
+  DummyMatchVisitor Visitor;
+  Visitor.ExpectMatch("/f/T", 2, 11);
+  Visitor.ExpectMatch("/f/f/", 2, 20);
+  EXPECT_TRUE(Visitor.runOver(Source));
+}
+
 } // end anonymous namespace
Index: include/clang/AST/RecursiveASTVisitor.h
===================================================================
--- include/clang/AST/RecursiveASTVisitor.h
+++ include/clang/AST/RecursiveASTVisitor.h
@@ -499,10 +499,10 @@
 
   bool canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child);
 
-private:
   // These are helper methods used by more than one Traverse* method.
   bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
 
+private:
   // Traverses template parameter lists of either a DeclaratorDecl or TagDecl.
   template <typename T>
   bool TraverseDeclTemplateParameterLists(T *D);
Index: include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h
===================================================================
--- include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h
+++ include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h
@@ -111,6 +111,21 @@
     return true;
   }
 
+#define DEF_TRAVERSE_TEMPLATE_DECL(TMPLDECLKIND)                               
\
+  bool Traverse##TMPLDECLKIND##TemplateDecl(TMPLDECLKIND##TemplateDecl *TD) {  
\
+    if (!BaseType::TraverseTemplateParameterListHelper(                        
\
+            TD->getTemplateParameters()))                                      
\
+      return false;                                                            
\
+    assert(!BaseType::getDerived().shouldVisitTemplateInstantiations() &&      
\
+           "It does not make sense for most clients to visit template "        
\
+           "instantiations here.");                                            
\
+    return BaseType::getDerived().TraverseDecl(TD->getTemplatedDecl());        
\
+  }
+  DEF_TRAVERSE_TEMPLATE_DECL(Class)
+  DEF_TRAVERSE_TEMPLATE_DECL(Var)
+  DEF_TRAVERSE_TEMPLATE_DECL(Function)
+#undef DEF_TRAVERSE_TEMPLATE_DECL
+
 private:
   bool TraverseAdditionalLexicallyNestedDeclarations() {
     // FIXME: Ideally the gathered declarations and the declarations in the


Index: unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
===================================================================
--- unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
+++ unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
@@ -64,7 +64,7 @@
       OS << ND->getNameAsString();
     else
       OS << "???";
-    if (isa<DeclContext>(D))
+    if (isa<DeclContext>(D) or isa<TemplateDecl>(D))
       OS << "/";
   }
   Matcher.match(OS.str(), D);
@@ -138,4 +138,14 @@
   EXPECT_TRUE(Visitor.runOver(Source, DummyMatchVisitor::Lang_OBJC));
 }
 
+TEST(LexicallyOrderedRecursiveASTVisitor, VisitTemplateDecl) {
+  StringRef Source = R"(
+template <class T> T f();
+)";
+  DummyMatchVisitor Visitor;
+  Visitor.ExpectMatch("/f/T", 2, 11);
+  Visitor.ExpectMatch("/f/f/", 2, 20);
+  EXPECT_TRUE(Visitor.runOver(Source));
+}
+
 } // end anonymous namespace
Index: include/clang/AST/RecursiveASTVisitor.h
===================================================================
--- include/clang/AST/RecursiveASTVisitor.h
+++ include/clang/AST/RecursiveASTVisitor.h
@@ -499,10 +499,10 @@
 
   bool canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child);
 
-private:
   // These are helper methods used by more than one Traverse* method.
   bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
 
+private:
   // Traverses template parameter lists of either a DeclaratorDecl or TagDecl.
   template <typename T>
   bool TraverseDeclTemplateParameterLists(T *D);
Index: include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h
===================================================================
--- include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h
+++ include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h
@@ -111,6 +111,21 @@
     return true;
   }
 
+#define DEF_TRAVERSE_TEMPLATE_DECL(TMPLDECLKIND)                               \
+  bool Traverse##TMPLDECLKIND##TemplateDecl(TMPLDECLKIND##TemplateDecl *TD) {  \
+    if (!BaseType::TraverseTemplateParameterListHelper(                        \
+            TD->getTemplateParameters()))                                      \
+      return false;                                                            \
+    assert(!BaseType::getDerived().shouldVisitTemplateInstantiations() &&      \
+           "It does not make sense for most clients to visit template "        \
+           "instantiations here.");                                            \
+    return BaseType::getDerived().TraverseDecl(TD->getTemplatedDecl());        \
+  }
+  DEF_TRAVERSE_TEMPLATE_DECL(Class)
+  DEF_TRAVERSE_TEMPLATE_DECL(Var)
+  DEF_TRAVERSE_TEMPLATE_DECL(Function)
+#undef DEF_TRAVERSE_TEMPLATE_DECL
+
 private:
   bool TraverseAdditionalLexicallyNestedDeclarations() {
     // FIXME: Ideally the gathered declarations and the declarations in the
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to