使用Clang进行函数调用分析
我正在使用 clang 进行某种源到源的转换。我想做以下事情:
我在 C 中有一些函数类,它们是 va_arg 函数,例如 printf() 。源文件中可能有多次对 printf()
的调用。我想解析源代码并找到所有这些对 printf()
的调用。此外,我想找到传递给 printf() 的参数类型。因此,如果我有类似的事情,
int a, b, c;
printf("%d%d%d", a, b, c);
我希望能够弄清楚对 printf 的特定调用是 printf(char*, int, int, int) 类型。我并不特别关心预选赛。
有人能告诉我应该如何在 clang 中做到这一点吗?任何与此类似的示例都会受到欢迎。如果您甚至可以告诉我我应该查看的所有课程并简要告诉我应该遵循的流程,我将非常感激。
I am using clang to do some kind of source to source transformation. I would like to do the following:
I have some class of functions in C which are va_arg
functions, e.g printf()
. There might be a number of calls to printf()
in the source file. I want to parse the source code and find all these calls to printf()
. Furthermore, I want to find the type of arguments that are passed to printf()
. So, if i have something like
int a, b, c;
printf("%d%d%d", a, b, c);
I want to be able to figure out that the particular call to printf
is of type printf(char*, int, int, int)
. I don't particularly care about qualifiers.
Could someone tell me how I should go about doing this in clang? Any example doing anything similar to this would be welcome. If you could even tell me what all classes I should be looking at and in brief tell me the flow that I should follow, I would be very grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该编写一个 ASTConsumer。首先要看的是 examples/PrintFunctionNames 这是一个非常简单的 ASTConsumer。
查找所有 printf 调用的一种方法是通过 RecursiveASTVisitor,查找 CallExpr 节点。这些节点具有 getNumArgs() 和 getArg(n),可让您检查参数。您可以对这些表达式调用 expr->getType() 来获取它们的类型。
You should write an ASTConsumer. The first thing to look at is the code in examples/PrintFunctionNames which is a very simple ASTConsumer.
One way to find all the calls to printf is through the RecursiveASTVisitor, looking for the CallExpr nodes. These nodes have getNumArgs() and getArg(n) which lets you examine the arguments. You can call expr->getType() on those expressions to get their types.