<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body 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>
<p> </p>
<p>Le 2016-07-13 03:45, Himanshu via cfe-dev a écrit :</p>
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0"><!-- html ignored --><!-- head ignored --><!-- meta ignored -->
<div dir="ltr">
<div>
<div class="gmail_signature">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 class="pre" style="margin: 0; padding: 0; font-family: monospace">_______________________________________________<br /> cfe-dev mailing list<br /><a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br /><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a></div>
</blockquote>
<p> </p>
<div>-- <br />
<div class="pre" style="margin: 0; padding: 0; font-family: monospace">David Come</div>
</div>
</body></html>