[cfe-dev] How to get argument names from clang::CallExpr?

Yitzhak Mandelbaum via cfe-dev cfe-dev at lists.llvm.org
Thu Oct 24 06:19:14 PDT 2019


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 <
cfe-dev at lists.llvm.org> 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
> cfe-dev at lists.llvm.org
> https://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/20191024/fbed010a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4847 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20191024/fbed010a/attachment.bin>


More information about the cfe-dev mailing list