[cfe-dev] Question about ASTMatchers
Manuel Klimek
klimek at google.com
Sun Feb 23 09:31:09 PST 2014
On Thu, Feb 20, 2014 at 8:08 PM, Christian Schafmeister <
chris.schaf at verizon.net> wrote:
> I’m creating AST Matchers to match recordDecls and varDecls and match the
> types for the varDecls to recordDecls.
> In many cases the varDecls are instantiating template classes and some of
> the template classes are specialized.
>
> If I use this matcher:
> recordDecl(isTemplateInstantiation(),
> isSameOrDerivedFrom(recordDecl(hasName("BAR")))).bind(“WHOLE”) —> FOO<A>
> node
> or
> recordDecl(isExplicitTemplateSpecialization(),
> isSameOrDerivedFrom(recordDecl(hasName("BAR")))).bind(“WHOLE”) —>
> FOO<int> node
>
> How do I get the full name of the nodes that match and are bound to
> “WHOLE”?
>
> node.getName() —> “FOO” not “FOO<A>” or “FOO<int>"
>
I don't have a good answer. Generally, getName() (or getNameAsString())
will print the full declaration for anything that's a declaration context.
So if you match a member m in FOO, you'd get FOO<int>::m. This will be
expanded for everything but the last part.
I don't know whether there's a good way to get the expansion for
everything. I've never needed it, though; out of curiosity, what do you
need it for?
>
>
>
> Here is the sample code:
>
> #include <stdio.h>
> #include <core/foundation.h>
> #include <core/object.h>
> #include <core/holder.h>
>
> namespace asttooling {
>
> class BAR {};
>
> template<typename T> class FOO : public BAR {};
> class A {};
>
> template<> class FOO<int> : public BAR {};
>
> void tinyFunc()
> {
> FOO<A> xa;
> FOO<int> xi;
> printf("Hi there, this is tinyFunc @%p %p\n", &xa, &xi);
> }
>
> };
>
>
> Christian Schafmeister
> chris.schaf at verizon.net
>
>
> On Feb 20, 2014, at 6:56 AM, Manuel Klimek <klimek at google.com> wrote:
>
>
> On Thu, Feb 20, 2014 at 3:49 PM, Christian Schafmeister <
> chris.schaf at verizon.net> wrote:
>
>> Manuel,
>>
>> I completely missed those predicates - thank you!
>>
>> How do I get the full name of an instantiated template recordDecl?
>>
>> As in:
>> template <class T> class X;
>> class A;
>>
>> X<A> xa;
>>
>> If a varDecl(…) matches on xa - how do I get the "X<A>” name?
>>
>
> Do you want to "get" it or "match" it. Assuming you want to "get" it,
> you'd say something like varDecl(hasType(qualType().bind("t"))) (untested
> ;), and then in the callback extract the QualType and call getAsString() on
> it.
>
>
>>
>> Best,
>>
>> .Chris.
>> Christian Schafmeister
>> chris.schaf at verizon.net
>>
>>
>> On Feb 20, 2014, at 12:53 AM, Manuel Klimek <klimek at google.com> wrote:
>>
>> hasAncestor(decl(anyOf(recordDecl(
>> isTemplateInstantiation()),
>> functionDecl(isTemplateInstantiation()))
>>
>> should do what you want.
>>
>>
>> On Wed, Feb 19, 2014 at 11:01 PM, Christian Schafmeister <
>> chris.schaf at verizon.net> wrote:
>>
>>>
>>> If I match a varDecl() that is in a method of a template class I get a
>>> match for the template and instantiated templates
>>> - is there a way to tell the difference by matching some difference in
>>> the ancestors of the varDecl node?
>>>
>>> I get a match for the translate::from_object<T> a0(*args); varDecl in
>>> the method activate (see below) that has this type:
>>> from_object<type-parameter-0-0, struct
>>> std::__1::integral_constant<_Bool, true> >
>>>
>>> I also get a match that I think is from the instantiation
>>> TestFunctoid<int> j(“test”) in tinyFunc - it has this type: "struct
>>> translate::from_object<int, std::true_type>"
>>>
>>> There is a clear difference in the name - but I was looking for
>>> something that didn’t feel like a hack.
>>>
>>> Are there differences in the AST class node for TestFunctoid when it is
>>> a template class vs when it is an instantiated template class?
>>>
>>>
>>>
>>> #include <stdio.h>
>>> #include <core/foundation.h>
>>> #include <core/object.h>
>>> #include <core/holder.h>
>>>
>>> namespace asttooling {
>>>
>>>
>>> template<typename T>
>>> class TestFunctoid : public core::Functoid {
>>> public:
>>>
>>> TestFunctoid(const string& name) : core::Functoid(name) {};
>>> core::T_mv activate( core::const_ActivationFrame_spREF
>>> closedOverFrame, int numArgs, ArgArray args )
>>> {
>>> translate::from_object<T> a0(*args);
>>> printf( "Address of a0= %p\n", &a0);
>>> return Values0<core::T_O>();
>>> }
>>> };
>>>
>>>
>>>
>>> void tinyFunc()
>>> {
>>> TestFunctoid<int> j("test");
>>> TinyStruct x(10);
>>> tinyFunc(x);
>>> printf("Hi there, this is tinyFunc\n");
>>> }
>>>
>>> };
>>>
>>> Christian Schafmeister
>>> Associate Professor
>>> Chemistry Department
>>> Temple University
>>>
>>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140223/3171259c/attachment.html>
More information about the cfe-dev
mailing list