<div dir="ltr">Thanks Matthias and David! <div><br></div><div>I will try to look into ASTMergeAction. </div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature">--<div>Himanshu</div></div></div>
<br><div class="gmail_quote">On Tue, Jul 12, 2016 at 11:25 PM, David Come <span dir="ltr"><<a href="mailto:David.Come@onera.fr" target="_blank">David.Come@onera.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="font-size:10pt;font-family:Verdana,Geneva,sans-serif">
<p>Hi,</p>
<p>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.</p>
<p>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.</p>
<p>I know the API has some utilities for merging ASTs together (ASTMergeAction).</p>
<p> </p>
<p>David.</p><div><div class="h5">
<p> </p>
<p>Le 2016-07-13 03:45, Himanshu via cfe-dev a écrit :</p>
</div></div><blockquote type="cite" style="padding:0 0.4em;border-left:#1010ff 2px solid;margin:0"><div><div class="h5">
<div dir="ltr">
<div>
<div>Hi All, </div>
</div>
<div class="gmail_quote">
<div dir="ltr">
<div> </div>
<div>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. </div>
<div> </div>
<div>I would like to get the body of a called function when visiting a CallExpr. </div>
<div>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: </div>
<div> </div>
<div>def.h: </div>
<div>   void foo();</div>
<div> </div>
<div>impl.c: </div>
<div>   #include "def.h"</div>
<div>   </div>
<div>   void foo(){</div>
<div>          printf("in func foo\n");</div>
<div>   }</div>
<div> </div>
<div>main.c: </div>
<div>  #include "def.h"</div>
<div> </div>
<div>  int main(){</div>
<div>     foo();</div>
<div>  }</div>
<div> </div>
<div>Running my Visitor code here is able to parse the call expression, but returns nullptr on </div>
<div>callexpr->getDirectCallee()->getBody(). Similarly, callexpr->getDirectCallee()->hasBody() </div>
<div>returns false. </div>
<div>Could anyone kindly suggest what should I do? </div>
<div> </div>
<div>Here is the simplified code for my visitor: </div>
class FVisitor : public RecursiveASTVisitor<FVisitor> {<br>   public:<br>          bool VisitCallExpr(CallExpr *C){<br>                const FunctionDecl *FD = C->getDirectCallee();<br>                // try both variants of getBody<br>                if(FD->getBody())<br>                            cout << "found body"<<endl;<br>                else if(FD->getBody(FD))<br>                           cout << "found body"<<endl;<br>                return true;<br>          }<br>};</div>
<div dir="ltr"><br>
<div>Thanks,</div>
<div>
<div>
<div>--
<div>Himanshu</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
</div></div><span class=""><div style="margin:0;padding:0;font-family:monospace">_______________________________________________<br> cfe-dev mailing list<br><a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a></div>
</span></blockquote><span class="HOEnZb"><font color="#888888">
<p> </p>
<div>-- <br>
<div style="margin:0;padding:0;font-family:monospace">David Come</div>
</div>
</font></span></div>
</blockquote></div><br></div>