如何从Clang中的CallExpr获取函数指针的参数? [英] How to get the arguments of a function pointer from a CallExpr in Clang?
问题描述
我正在尝试使用函数调用来分析C源代码.我可以使用下面的源代码来分析正常的函数调用以获取其参数而不会出现问题,其中ce是CallExpr
对象:
I am trying to analyse C source code with function calls within them. I am able to analyse normal function calls to get their arguments without problem using the source code below where ce is a CallExpr
object:
1. if(ce != NULL) {
2. QualType q = ce->getType();
3. const Type *t = q.getTypePtrOrNull();
4.
5. if (t != NULL) {
6. llvm::errs() << "TYPE: " << t->isFunctionPointerType() << " " << q.getAsString() << " " << t->isPointerType() << "\n";
7. } else {
8. llvm::errs() << "FUNCTION CE HAS NO TYPE?\n";
9. }
10.
11.
12. const Decl* D = ce ->getCalleeDecl();
13. while(D->getPreviousDecl() != NULL)
14. D = D->getPreviousDecl();
15.
16. llvm::errs() << "Kind: " << D->getDeclKindName() << "\n";
17.
18. FunctionDecl* fd = (FunctionDecl*) llvm::dyn_cast<FunctionDecl>(D);
19. for(int x = 0; x< fd ->getNumParams(); x++) {
20. if(fd ->getParamDecl(x)->getType()->isAnyPointerType()) {
21. // Do Stuff Here
22. }
23. }
24. }
上述源代码的问题出现在第18行,当我尝试将Decl从CallExpr
转换为FunctionDecl
时,如果CallExpr
来自某个函数,则导致fd变为NULL
指针调用.
The problem with the above source code comes on line 18, when I try to typecast the Decl from the CallExpr
to a FunctionDecl
, this results in fd becoming NULL
if the CallExpr
is from a function pointer call.
我尝试通过尝试在第16行上打印种类来进行调试.对于函数指针,它指定Decl on 12
是VarDecl
,而不是像正常函数调用那样的FunctionDecl.
I tried to debug by trying to print the kind on line 16. For function pointers, it specifies the Decl on 12
is a VarDecl
, not a FunctionDecl like normal function calls.
我也尝试使用isFunctionPointerType()
,但这返回的是false.
I also tried using the isFunctionPointerType()
, but this is returning false.
这是一段导致段错误的源代码:
Here is a piece of source code that results in a segfault:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
void* (*mp)(size_t size);
void *mpp;
mp = &malloc;
mpp = mp(30);
free(mpp);
return (0);
}
有没有一种方法可以使用clang来检测CallExpr
是否是函数指针调用?如果可以,如何获取参数列表?
Is there a way using clang to detect whether a CallExpr
is a function pointer call? and if so, how to get a list of the arguments?
我正在使用clang 3.1
谢谢
推荐答案
您应该从指针声明中获取函数原型,之后您将能够获取有关返回类型和参数类型的信息:
You should get function prototype from pointer declaration, after that you will be able to get information about return type and parameters types:
clang::CallExpr* expr;
...
auto decl = expr->getCalleeDecl();
if (decl != nullptr) {
if (decl->getKind() == clang::Decl::Var) {
clang::VarDecl *varDecl = clang::dyn_cast<clang::VarDecl>(decl);
if(varDecl->getType()->isFunctionPointerType() == true) {
const clang::PointerType *pt = varDecl->getType()->getAs<clang::PointerType>();
const clang::FunctionProtoType *ft = pt->getPointeeType()->getAs<clang::FunctionProtoType>();
if (ft != nullptr) {
std::string retTypeName = ft->getReturnType().getAsString();
...
auto paramsCount = funcType->getNumParams();
for (size_t i = 0; i < paramsCount; ++i) {
clang::QualType paramType = funcType->getParamType(i);
std::string paramTypeName = paramType.getAsString();
...
}
}
}
}
}
这篇关于如何从Clang中的CallExpr获取函数指针的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!