使用 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
i
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() << "
";
}
return true;
}
输出:
argType: char *
argType: int
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屋!