[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 03:15:17 PDT 2014


On Thu Aug 28 2014 at 12:02:11 PM Alexander Kornienko <alexfh at google.com>
wrote:

> ================
> 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));
>

That's what I have used before, too.


> http://reviews.llvm.org/D5085
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140828/cbf5730d/attachment.html>


More information about the cfe-commits mailing list