[cfe-dev] Question about ASTMatchers

Richard legalize at xmission.com
Wed Feb 19 16:38:19 PST 2014


In article <5C28B1F5-CDBF-4ABD-BF60-F70FB2AF6DFF at verizon.net>,
    Christian Schafmeister <chris.schaf at verizon.net> writes:

> 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?

Did you try using any of these:
- classTemplateDecl
- classTemplateSpecializationDecl
- unless(isTemplateInstantiation)

What does the ast dump say for your case?  (I'm away from my clang
tools right now.)

clang -Xclang -ast-dump 

> #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);
          ^^^^^^^^^^^^
tinyFunc wasn't declared as taking any arguments...

>         printf("Hi there, this is tinyFunc\n");
>     }
> 
> };

Skipping over everything from the include files, I got this from
ast-dump:

|-NamespaceDecl 0x2d24a90 </tmp/foo.cpp:6:1, line:30:1> asttooling
| |-ClassTemplateDecl 0x2d24c30 <line:8:5, line:20:5> TestFunctoid
| | |-TemplateTypeParmDecl 0x2d24af0 <line:8:14, col:23> typename T
| | |-CXXRecordDecl 0x2d24ba0 <line:9:5, line:20:5> class TestFunctoid definition invalid
| | | |-CXXRecordDecl 0x2d24eb0 <line:9:5, col:11> class TestFunctoid
| | | |-AccessSpecDecl 0x2d24f40 <line:10:5, col:11> public
| | | |-CXXConstructorDecl 0x2d25090 <line:12:9, col:66> TestFunctoid<T> 'void (const int &)'
| | | | |-ParmVarDecl 0x2d24fb0 <col:22, col:36> name 'const int &' invalid
| | | | `-CompoundStmt 0x2d26458 <col:65, col:66> invalid
| | | `-CXXMethodDecl 0x2d26370 <line:13:9, line:19:9> activate 'int (int, int, int)'
| | |   |-ParmVarDecl 0x2d25160 <line:13:30, line:14:1> closedOverFrame 'int' invalid
| | |   |-ParmVarDecl 0x2d26220 <col:18, col:22> numArgs 'int'
| | |   |-ParmVarDecl 0x2d26290 <col:31, col:40> args 'int' invalid
| | |   `-CompoundStmt 0x2d265d8 <line:15:9, line:19:9> invalid
| | `-ClassTemplateSpecializationDecl 0x2d266f0 <line:8:5, line:20:5> class TestFunctoid definition
| |   |-TemplateArgument type 'int' invalid
| |   |-CXXRecordDecl 0x2d26a40 prev 0x2d266f0 <line:9:5, col:11> class TestFunctoid
| |   `-AccessSpecDecl 0x2d26ad0 <line:10:5, col:11> public
| `-FunctionDecl 0x2d26640 <line:22:5, line:28:5> tinyFunc 'void (void)'
|   `-CompoundStmt 0x2d26e38 <line:23:5, line:28:5>
|     |-DeclStmt 0x2d26af8 <line:24:9, col:36>
|     | `-VarDecl 0x2d268a0 <col:9, col:27> j 'TestFunctoid<int>':'class asttooling::TestFunctoid<int>' invalid
|     |-DeclStmt 0x2d26bc8 <line:25:9, col:25>
|     | `-VarDecl 0x2d26b20 <col:9, col:20> x 'int' invalid
|     `-CallExpr 0x2d26df0 <line:27:9, col:46> 'int'
|       |-ImplicitCastExpr 0x2d26dd8 <col:9> 'int (*)(const char *restrict, ...)' <FunctionToPointerDecay>
|       | `-DeclRefExpr 0x2d26d50 <col:9> 'int (const char *restrict, ...)' lvalue Function 0x2d15500 'printf' 'int (const char *restrict, ...)'
|       `-ImplicitCastExpr 0x2d26e20 <col:16> 'const char *' <ArrayToPointerDecay>
|         `-StringLiteral 0x2d26d08 <col:16> 'const char [28]' lvalue "Hi there, this is tinyFunc\n"
`-EmptyDecl 0x2d26e70 <line:30:2>

-- 
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
         The Terminals Wiki <http://terminals.classiccmp.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>



More information about the cfe-dev mailing list