[cfe-dev] Getting function body from CallExpr (function definition in .h, implementation in .c)

Himanshu via cfe-dev cfe-dev at lists.llvm.org
Tue Jul 12 18:45:55 PDT 2016


Hi All,

I have looked thru the previous posts on this topic in the mailing-list
archives, and still can't get things to work. Hence the repost --- for
which I apologize.

I would like to get the body of a called function when visiting a CallExpr.
I create a recursive AST visitor, and handle the translation units
correctly (to the best of my knowledge), and still am not able to get the
body. The function is declared in a header file and implemented in a .c
file. For brevity, I am recreating a toy example here:

def.h:
   void foo();

impl.c:
   #include "def.h"

   void foo(){
          printf("in func foo\n");
   }

main.c:
  #include "def.h"

  int main(){
     foo();
  }

Running my Visitor code here is able to parse the call expression, but
returns nullptr on
callexpr->getDirectCallee()->getBody(). Similarly,
callexpr->getDirectCallee()->hasBody()
returns false.
Could anyone kindly suggest what should I do?

Here is the simplified code for my visitor:
class FVisitor : public RecursiveASTVisitor<FVisitor> {
   public:
          bool VisitCallExpr(CallExpr *C){
                const FunctionDecl *FD = C->getDirectCallee();
                // try both variants of getBody
                if(FD->getBody())
                            cout << "found body"<<endl;
                else if(FD->getBody(FD))
                           cout << "found body"<<endl;
                return true;
          }
};

Thanks,
--
Himanshu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160712/747c648d/attachment.html>


More information about the cfe-dev mailing list