This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by bkramer on Aug 27 2014, 7:14 AM.

Details

Summary

This is hoisted from clang-tidy where it's used everywhere. The implementation
is not particularly efficient right now, but there is no easy fix for that.

Diff Detail

Event Timeline

bkramer updated this revision to Diff 12990.Aug 27 2014, 7:14 AM
bkramer retitled this revision from to ASTMatchers: Add a matcher to detect whether a decl or stmt is inside a template instantiation..
bkramer updated this object.
bkramer added reviewers: klimek, alexfh.
bkramer added a subscriber: Unknown Object (MLST).
klimek added inline comments.Aug 28 2014, 2:53 AM
include/clang/ASTMatchers/ASTMatchers.h
2970–2982

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.

2980

Also matches the block '{ T i; }'.

alexfh added inline comments.Aug 28 2014, 3:02 AM
include/clang/ASTMatchers/ASTMatchers.h
2970–2982

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));
bkramer updated this revision to Diff 13140.Sep 1 2014, 8:20 AM

Split into two matchers, one for Decl (also matches itself) and one for Stmt.
Update tests and document node sharing artifacts.

alexfh added inline comments.Sep 1 2014, 9:14 AM
include/clang/ASTMatchers/ASTMatchers.h
2970

I find this comment somewhat hard to understand. I'd say that this matches declarations that are template instantiations or are inside template instantiations.

2998

Did you mean isInTemplateInstantiation (here and below)?

lib/ASTMatchers/Dynamic/Registry.cpp
246

Why do you register only one matcher?

bkramer updated this revision to Diff 13141.Sep 1 2014, 9:22 AM

Address review comments.

klimek accepted this revision.Sep 3 2014, 5:15 AM
klimek edited edge metadata.

lg

This revision is now accepted and ready to land.Sep 3 2014, 5:15 AM
Diffusion closed this revision.Sep 3 2014, 5:17 AM
Diffusion updated this revision to Diff 13200.

Closed by commit rL217029 (authored by d0k).