0

I want to print the arguments passed in a function. But clang returns Parameter names used in the function definition. Here is my code:

if (CallExpr *call = dyn_cast<CallExpr>(st)) 
{ 
    LangOptions LangOpts;
    LangOpts.CPlusPlus = true;
    PrintingPolicy Policy(LangOpts);

    for(int i=0, j=call->getNumArgs(); i<j; i++)
    {
        std::string TypeS;
        raw_string_ostream raw(TypeS);
        call->getArg(i)->printPretty(raw, 0, Policy);
        errs() << raw.str() << "\n";
    }
}

Please tell what might be the problem.

1 Answers1

3

when it comes to arguments, there are many possibilities. But I assume you are dealing with the simplest case, like:

int iArgument = 123;
char cArgument = 'c';
foo(iArgument, cArgument);

Several castings are necessary to get arguments' info

for (unsigned i = 0; i < call->getNumArgs(); ++i) {
    if (auto cast = dyn_cast<ImplicitCastExpr>(call->getArg(i))) {
        if (auto arg = dyn_cast<DeclRefExpr>(cast->getSubExpr())) {
            arg->getNameInfo().getName().dump();
        }
    }
}
Jason L.
  • 741
  • 6
  • 11
  • Sorry for replying late. I want to get iArgument and cArgument, i.e., the arguments' names. That's all. [This](http://stackoverflow.com/questions/9607852/print-arguments-of-a-function-using-clang-ast) says to use `printPretty()`. – Dhriti Khanna Aug 07 '16 at 12:39
  • Also, I am able to get the arguments' name when I am using a simple C++ code. But when I start using another library's API (for that I am specifying compile_commands.json file), this presents problems. – Dhriti Khanna Aug 07 '16 at 12:42