[cfe-dev] Matching all the DeclRefExpr referring to a certain VarDecl

Nikola Smiljanic via cfe-dev cfe-dev at lists.llvm.org
Tue Aug 25 20:06:40 PDT 2015


Note that 'a' is a VarDecl whereas 'i' is a ParamVarDecl (that derives from
VarDecl). Using isa<ParamVarDecl> sounds better to me if all you're trying
to do is differentiate between local variables and parameters.

On Tue, Aug 25, 2015 at 11:49 PM, Luca Atzori via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hi all,
>
> supposing I have this code:
>
> void foo(int i){
>         int a;
>         a = i;
> }
>
> int main(){
>         foo(0);
> }
>
> I would like to match all the DeclRefExpr related to the variable a,
> but not to the parameter i.
>
> This is the AST dump of the code
>
> |-FunctionDecl 0x6261720 <decltest.cu:1:1, line:4:1> line:1:6 used foo
> 'void (int)'
> | |-ParmVarDecl 0x6261698 <col:10, col:14> col:14 used i 'int'
> | `-CompoundStmt 0x62618e8 <col:16, line:4:1>
> |   |-DeclStmt 0x6261840 <line:2:2, col:7>
> |   | `-VarDecl 0x62617e0 <col:2, col:6> col:6 used a 'int'
> |   `-BinaryOperator 0x62618c0 <line:3:2, col:6> 'int' lvalue '='
> |     |-DeclRefExpr 0x6261858 <col:2> 'int' lvalue Var 0x62617e0 'a' 'int'
> |     `-ImplicitCastExpr 0x62618a8 <col:6> 'int' <LValueToRValue>
> |       `-DeclRefExpr 0x6261880 <col:6> 'int' lvalue ParmVar 0x6261698 'i'
> 'int'
> `-FunctionDecl 0x6261930 <line:6:1, line:8:1> line:6:5 main 'int (void)'
>   `-CompoundStmt 0x6261ac0 <col:11, line:8:1>
>     `-CallExpr 0x6261a90 <line:7:2, col:7> 'void'
>       |-ImplicitCastExpr 0x6261a78 <col:2> 'void (*)(int)'
> <FunctionToPointerDecay>
>       | `-DeclRefExpr 0x6261a50 <col:2> 'void (int)' lvalue Function
> 0x6261720 'foo' 'void (int)'
>       `-IntegerLiteral 0x6261a30 <col:6> 'int' 0
>
> It seems that I have to exploit the "address" (is it correct?) of the
> VarDecl, namely 0x62617e0, that guarantees me that I'm referring
> exactly that variable, because I'm not really sure the name will be
> enough for avoiding ambiguity.
>
> I'm using an ASTConsumer, and I have a Stmt* variable representing the
> body of the function foo (i'm not using matchers' DSL).
>
> Any idea on how can i get those addresses? (my guess is something
> related to VarDecl class' getASTContext method).
>
>
> Any help would be very much appreciated.
>
> Luca A.
>
>
> P.S.
>
> More, hopefully useful, details: my goal is to "vectorize" those
> variables, so the output must be:
>
> void foo(int i){
>         int a[N];
>         loop(id: 0..N-1){
>                 a[id] = i;
>         }
> }
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150826/2b337fbb/attachment.html>


More information about the cfe-dev mailing list