[cfe-dev] Clang function call expressions

Georgiou, Andreas via cfe-dev cfe-dev at lists.llvm.org
Fri Jul 8 07:48:44 PDT 2016


Hello I have implemented an AST visitor which is working quite good and it can print me on the console the information I want from the AST such as variable declarations, function declarations and function calls. Today while I was experimenting I came across a function call which is not recognized as a function call. syntacticaly is the same as a function call. Here is the code:


void  //this is a part of the tiff library for linux. its in the source file tif_error.c in libtiff directory
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
}


My code of the ASTvisitor is this:


 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;
    }


The expression if(call->getDirectCallee()) is not true for those calls.


How can I extract the "function name" and its arguments like I am doing with the "normal" function calls?


Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160708/270551ee/attachment.html>


More information about the cfe-dev mailing list