[PATCH] D43567: [ASTMatchers] isTemplateInstantiation: also match explicit instantiation declaration.

Eric Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 21 04:52:49 PST 2018


ioeric created this revision.
ioeric added a reviewer: bkramer.
Herald added subscribers: cfe-commits, klimek.

Example:
template <typename T> class X {}; class A {}; 
// Explicit instantiation declaration.
extern template class X<A>;


Repository:
  rC Clang

https://reviews.llvm.org/D43567

Files:
  include/clang/ASTMatchers/ASTMatchers.h
  unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp


Index: unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -1623,6 +1623,14 @@
       "template class X<A>;",
     cxxRecordDecl(isTemplateInstantiation(), hasDescendant(
       fieldDecl(hasType(recordDecl(hasName("A"))))))));
+
+  // Make sure that we match the instantiation instead of the template
+  // definition by checking whether the member function is present.
+  EXPECT_TRUE(
+      matches("template <typename T> class X { void f() { T t; } };"
+              "extern template class X<int>;",
+              cxxRecordDecl(isTemplateInstantiation(),
+                            unless(hasDescendant(varDecl(hasName("t")))))));
 }
 
 TEST(IsTemplateInstantiation,
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -4649,6 +4649,10 @@
 /// \code
 ///   template <typename T> class X {}; class A {}; template class X<A>;
 /// \endcode
+/// or
+/// \code
+///   template <typename T> class X {}; class A {}; extern template class X<A>;
+/// \endcode
 /// cxxRecordDecl(hasName("::X"), isTemplateInstantiation())
 ///   matches the template instantiation of X<A>.
 ///
@@ -4666,7 +4670,9 @@
                                                         CXXRecordDecl)) {
   return (Node.getTemplateSpecializationKind() == TSK_ImplicitInstantiation ||
           Node.getTemplateSpecializationKind() ==
-          TSK_ExplicitInstantiationDefinition);
+              TSK_ExplicitInstantiationDefinition ||
+          Node.getTemplateSpecializationKind() ==
+              TSK_ExplicitInstantiationDeclaration);
 }
 
 /// \brief Matches declarations that are template instantiations or are inside


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43567.135231.patch
Type: text/x-patch
Size: 1933 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180221/c5381b77/attachment.bin>


More information about the cfe-commits mailing list