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

Alexander Kornienko alexfh at google.com
Thu Aug 28 03:02:03 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,
----------------
klimek wrote:
> 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.
> 
> Perhaps add comment that this will not match if the node itself is a template instantiation

Maybe add a separate version for decl, which will handle this? Something along the lines of:

    auto IsInstantiation = decl(anyOf(recordDecl(isTemplateInstantiation()),
                                      functionDecl(isTemplateInstantiation())));
    auto InnerMatcher = decl(anyOf(isInstantiation, hasAncestor(isInstantiation));

http://reviews.llvm.org/D5085






More information about the cfe-commits mailing list