How to get argument names from clang::CallExpr?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get argument names from clang::CallExpr?

Kristof Beyls via cfe-dev

Hi!

 

How can I retrieve the argument names from a clang::CallExpr? I was trying something along the following lines, but the dyn_cast returns null.

 

const auto *dr = clang::dyn_cast<clang::DeclRefExpr>(expr->getArg(0));

auto name = dr->getDecl()->getName().str();

 

Thanks,

Kenth

 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to get argument names from clang::CallExpr?

Kristof Beyls via cfe-dev
On 22/10/2019 08:06, Kenth Eriksson via cfe-dev wrote:
> Hi!
>
> How can I retrieve the argument names from a clang::CallExpr? I was
> trying something along the following lines, but the dyn_cast returns null.

What if you don't try to cast it to a `DeclRefExpr`, but you cast it to
a `Expr` and `->dump()` it?

Note that some args are not represented by `DeclRefExpr` if they are
literals such as IntegerLiteral or StringLiteral.

Also - are you sure you want argument names? In the code below, clang
calls a1 and a2 arguments and p1 and p2 parameters:

void foo(int p1, int p2);

void bar()
{
    int a1 = 5;
    int a2 = 7;
    foo(a1, a2);
}

if you want parameters, you need to get the FunctionDecl from the
callExpr and query its parameters to get their names.

Thanks,

Stephen.

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to get argument names from clang::CallExpr?

Kristof Beyls via cfe-dev
In reply to this post by Kristof Beyls via cfe-dev
> What if you don't try to cast it to a `DeclRefExpr`, but you cast it
> to
> a `Expr` and `->dump()` it?
>

If I dump the CallExpr, I can see the following;

CallExpr 0x5640de2c08c8 'char *'
|-ImplicitCastExpr 0x5640de2c08b0 'char *(*)(char *, const char *)' <FunctionToPointerDecay>
| `-DeclRefExpr 0x5640de2c07f0 'char *(char *, const char *)' Function 0x5640de2916a8 'strcpy' 'char *(char *, const char *)'
|-ImplicitCastExpr 0x5640de2c08f8 'char *' <LValueToRValue>
| `-UnaryOperator 0x5640de2c0848 'char *' lvalue prefix '*' cannot overflow
|   `-ImplicitCastExpr 0x5640de2c0830 'char **' <LValueToRValue>
|     `-DeclRefExpr 0x5640de2c0810 'char **' lvalue ParmVar 0x5640de366998 'valueString' 'char **'
`-ImplicitCastExpr 0x5640de2c0928 'const char *' <NoOp>
  `-ImplicitCastExpr 0x5640de2c0910 'char *' <LValueToRValue>
    `-DeclRefExpr 0x5640de2c0860 'char *' lvalue Var 0x5640de366d88 'ptr1' 'char *'

I want to get the name of the first argument supplied to the call of
strcpy.

Note that some args are not represented by `DeclRefExpr` if they are
> literals such as IntegerLiteral or StringLiteral.
>  


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to get argument names from clang::CallExpr?

Kristof Beyls via cfe-dev
Can you show use the code corresponding to the CallExpr?

Also, when you say the "name of the first argument supplied..." that presumes that the first argument is always a variable. That's not generally the case -- any (well typed) argument will work. For example, your first argument in the dumped CallExpr above is pointer-dereference (I think `*valueString`?).  So, you would, at the least, want your code to be

if (const auto *dr = clang::dyn_cast<clang::DeclRefExpr>(expr->getArg(0))) {

  auto name = dr->getDecl()->getName().str();

  ...

}


That said, you probably want expr->IgnoringImplicit(), since lvalues (e.g. variable names) are always implicitly cast to rvalues.

 


On Thu, Oct 24, 2019 at 3:29 AM Kenth Eriksson via cfe-dev <[hidden email]> wrote:
> What if you don't try to cast it to a `DeclRefExpr`, but you cast it
> to
> a `Expr` and `->dump()` it?
>

If I dump the CallExpr, I can see the following;

CallExpr 0x5640de2c08c8 'char *'
|-ImplicitCastExpr 0x5640de2c08b0 'char *(*)(char *, const char *)' <FunctionToPointerDecay>
| `-DeclRefExpr 0x5640de2c07f0 'char *(char *, const char *)' Function 0x5640de2916a8 'strcpy' 'char *(char *, const char *)'
|-ImplicitCastExpr 0x5640de2c08f8 'char *' <LValueToRValue>
| `-UnaryOperator 0x5640de2c0848 'char *' lvalue prefix '*' cannot overflow
|   `-ImplicitCastExpr 0x5640de2c0830 'char **' <LValueToRValue>
|     `-DeclRefExpr 0x5640de2c0810 'char **' lvalue ParmVar 0x5640de366998 'valueString' 'char **'
`-ImplicitCastExpr 0x5640de2c0928 'const char *' <NoOp>
  `-ImplicitCastExpr 0x5640de2c0910 'char *' <LValueToRValue>
    `-DeclRefExpr 0x5640de2c0860 'char *' lvalue Var 0x5640de366d88 'ptr1' 'char *'

I want to get the name of the first argument supplied to the call of
strcpy.

Note that some args are not represented by `DeclRefExpr` if they are
> literals such as IntegerLiteral or StringLiteral.



_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

smime.p7s (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to get argument names from clang::CallExpr?

Kristof Beyls via cfe-dev
On Thu, 2019-10-24 at 09:19 -0400, Yitzhak Mandelbaum wrote:
>
> Can you show use the code corresponding to the CallExpr?

*valueString = (char*)malloc(len+1);
strcpy(*valueString, ptr1);
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev