使用Clang AST打印函数的参数 [英] Print arguments of a function using Clang AST
问题描述
我想得到传递给函数的参数。例如,如果我有呼叫
I want to get the arguments passed to a function. for example, if I have the call
printf(%d%d,i,j);
printf("%d%d", i, j);
输出应为
%d%d
ielves J
%d%d
i
j
我可以使用RecursiveASTVisitor中的VisitCallExpr()函数调用函数。也能够获得参数的数量和参数类型。但我不知道如何获得参数。
I am able to get to function calls using VisitCallExpr() in RecursiveASTVisitor. Also able to get the number of arguments and the argument types. But I don't know how to get the arguments.
bool MyRecursiveASTVisitor::VisitCallExpr (clang::CallExpr *E)
{
for(int i=0, j=E->getNumArgs(); i<j; i++)
{
llvm::errs() << "argType: " << E->getArg(i)->getType().getAsString() << "\n";
}
return true;
}
输出:
argType:char *
argType:intholar argType:int
argType: char *
argType: int
argType: int
推荐答案
您正在调用E-> getArg(i) - > getType()参数类型。使用E-> getArg(i)获取Expr *表示参数的值。然后使用 printPretty(...)方法将其漂亮打印为字符串,如果你需要字符串值。
You are calling E->getArg(i)->getType() - but that is type of argument. Use E->getArg(i) to get Expr* representing value of argument. Then use printPretty(...) method to pretty-print it to string, if you need string value.
这篇关于使用Clang AST打印函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!