[cfe-dev] Getting function body from CallExpr (function definition in .h, implementation in .c)
Matthias Gehre via cfe-dev
cfe-dev at lists.llvm.org
Wed Jul 13 00:04:10 PDT 2016
Hi Himanshu,
I guess that you are looking at the AST of main.c. The definition/body of
foo is not available in main.c (after expanding all includes),
thus you see the nullptr. The AST is per translation unit, not for the
whole program.
When compiling, this is only resolved later by the linker.
I don't know if there is a way to merge the ASTs of different translation
units yet, I remember that I saw some
discussions on the mailing list.
Best wishes,
Matthias
2016-07-13 3:45 GMT+02:00 Himanshu via cfe-dev <cfe-dev at lists.llvm.org>:
> 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
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160713/63ccd82c/attachment.html>
More information about the cfe-dev
mailing list