你好,我已经实现了一个工作得很好的AST访问者,它可以在控制台中打印我想从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;
}
表达如果(呼叫 - > getDirectCallee())是不是为那些通话真。
如何提取“函数名”及其参数,如我正在做的“正常”函数调用? 甚至有人告诉我为什么这些调用不被AST递归访问者认为是正常的函数调用。
谢谢。
谢谢你的回答。这将澄清事情,并会让我继续前进。我转储相应的行,我可以看到__builtin_va_start(ap,param),但是我从来没有像之前那样(定义一个函数调用的宏)之类的东西。在你最后一点上,我已经实现了VisitVarDecl和VisitFuctionDecl。 VisitStmt将帮助我获得函数调用。如果有更好的方法让他们请让我知道。 –
您可以调用像visitCallExpr,visitCXXMemberCallExpr这样的函数来执行常规函数和成员函数调用。可能会有更多,但我不知道这一点。此外,如果您的原始问题已解决,我会很感激,如果您标记它。 –
感谢您的评论。我不知道visitCallExpr存在!我在doxygen中没有遇到过这个功能。我正在使用这个源http://clang.llvm.org/doxygen/classclang_1_1idx_1_1ASTVisitor.html 你是对的。 VisitCallExpr()也存在.. –