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: 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 bool TraverseDeclTemplateParameterLists(T *D); Index: unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp =================================================================== --- unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp +++ unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp @@ -64,7 +64,7 @@ OS << ND->getNameAsString(); else OS << "???"; - if (isa(D)) + if (isa(D) or isa(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 T f(); +)"; + DummyMatchVisitor Visitor; + Visitor.ExpectMatch("/f/T", 2, 11); + Visitor.ExpectMatch("/f/f/", 2, 20); + EXPECT_TRUE(Visitor.runOver(Source)); +} + } // end anonymous namespace