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

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

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