使用 Clang AST 打印函数的参数 [英] Print arguments of a function using Clang AST

查看:45
本文介绍了使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆