[cfe-dev] Getting function body from CallExpr (function definition in .h, implementation in .c)
Himanshu via cfe-dev
cfe-dev at lists.llvm.org
Wed Jul 13 08:46:11 PDT 2016
Thanks Matthias and David!
I will try to look into ASTMergeAction.
--
Himanshu
On Tue, Jul 12, 2016 at 11:25 PM, David Come <David.Come at onera.fr> wrote:
> Hi,
>
> The compiler only knows what it has parsed. So unless you parse impl.c and
> then merge the AST, you can't get the function's body C.
>
> Because it is enough for any compiler in main.c to know that foo is a
> function with no argument returning nothing. Its body is irrelevant for
> compiling main.c to an object file. You could have its source code (like
> you do in your example) or it could be in a library, it only matters when
> linking.
>
> I know the API has some utilities for merging ASTs together
> (ASTMergeAction).
>
>
>
> David.
>
>
>
> Le 2016-07-13 03:45, Himanshu via cfe-dev a écrit :
>
> 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
>
>
> --
> David Come
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160713/85a3f60f/attachment.html>
More information about the cfe-dev
mailing list