[PATCH] D36998: [AST] Traverse templates in LexicallyOrderedRecursiveASTVisitor

Johannes Altmanninger via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 25 02:56:54 PDT 2017


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36998.112671.patch
Type: text/x-patch
Size: 2852 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170825/d78ffa97/attachment.bin>


More information about the cfe-commits mailing list