【发布时间】:2023-03-18 04:24:02
【问题描述】:
您好,我已经实现了一个运行良好的 AST 访问器,它可以在控制台中打印我想要的 AST 信息,例如变量声明、函数声明和函数调用。今天,在我进行实验时,我遇到了一个未被识别为函数调用的函数调用。语法上与函数调用相同。代码如下:
void
TIFFError(const char* module, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt); <------------------------------ THIS IS THE FUNCTION CALL
if (_TIFFerrorHandler)
(*_TIFFerrorHandler)(module, fmt, ap);
if (_TIFFerrorHandlerExt)
(*_TIFFerrorHandlerExt)(0, module, fmt, ap);
va_end(ap); <--------------------------------AND THIS ONE
}
我的 ASTvisitor 代码是这样的:
bool VisitStmt(Stmt *st)
{
FullSourceLoc FullLocation = astContext->getFullLoc(st->getLocStart());
FileID fileID = FullLocation.getFileID();
unsigned int thisFileID = fileID.getHashValue();
if(thisFileID == 1) //checks if the node is in the main = input file.
{
if (CallExpr *call = dyn_cast<CallExpr>(st))
{
numFuncCalls++;
//call->dump(); //prints the corresponding line of the AST.
FunctionDecl *func_decl;
if(call->getDirectCallee())
{
func_decl = call ->getDirectCallee();
string funcCall = func_decl->getNameInfo().getName().getAsString();
cout << "Function call: " << funcCall << " with arguments ";
APIs << funcCall << ",";
for(int i=0, j = call->getNumArgs(); i<j; i++)
{
//For each argument it prints its type. The function must be declared otherwise it will return int-for unknown argument type.
APIs << call->getArg(i)->getType().getAsString()<< ",";
cout << call->getArg(i)->getType().getAsString() << ", ";
}
cout << "\n";
}
else
{
Expr *expr = call->getCallee();
string exprCall = expr->getStmtClassName();
cout << "Expression call: " << exprCall << " with arguments ";
APIs << exprCall << ",";
for(int i=0, j = call->getNumArgs(); i<j; i++)
{
//For each argument it prints its type. The function must be declared otherwise it will return int-for unknown argument type.
APIs << call->getArg(i)->getType().getAsString()<< ",";
cout << call->getArg(i)->getType().getAsString() << ", ";
}
cout << "\n";
}
}
}
return true;
}
表达式 if(call->getDirectCallee()) 不适用于这些调用。
我如何提取“函数名”及其参数,就像我对“普通”函数调用所做的那样? 甚至有人告诉我为什么这些调用不被 AST 递归访问者识别为正常的函数调用。
谢谢。
【问题讨论】:
标签: c++ abstract-syntax-tree llvm-clang