[PATCH] ASTMatchers: Add a matcher to detect whether a decl or stmt is inside a template instantiation.

Manuel Klimek klimek at google.com
Thu Aug 28 02:53:46 PDT 2014


================
Comment at: include/clang/ASTMatchers/ASTMatchers.h:2970-2982
@@ -2969,1 +2969,15 @@
 
+/// \brief Matches declarations or statements that are contained in a function
+/// or class template instantiation.
+///
+/// Given
+/// \code
+///   template<typename T> void A(T t) { T i; }
+///   A(0);
+///   A(0U);
+/// \endcode
+/// stmt(isInTemplateInstantiation())
+///   matches 'int i;' and 'unsigned i;'.
+///
+/// Usable as: Matcher<Decl>, Matcher<Stmt>
+AST_POLYMORPHIC_MATCHER(isInTemplateInstantiation,
----------------
Perhaps add comment that this will not match if the node itself is a template instantiation (so functionDecl(isInTemplateInstantiation()) will not match anything here).

I'd also add a comment about node sharing; for example, given
int j;
template<typename T> void A(T t) { T i; j += 42; int x; }
  stmt(unless(isInTemplateInstnatiation()))
will not match j += 42 (because the template independent node is shared between the definition and the instantiations), but will match 'T i;' (with the dependent type), because the dependent nodes cannot be shared.
Funnily enough 'int x' *will* match (as will any further statement referencing x); I have no idea why the node cannot be shared, which is why node sharing always makes me feel cautious.

Of course:
  stmt(isInTemplateInstnatiation())
will match all.


================
Comment at: include/clang/ASTMatchers/ASTMatchers.h:2980
@@ +2979,3 @@
+/// stmt(isInTemplateInstantiation())
+///   matches 'int i;' and 'unsigned i;'.
+///
----------------
Also matches the block '{ T i; }'.

http://reviews.llvm.org/D5085






More information about the cfe-commits mailing list