[cfe-dev] How to get Function Definition from CallExpr

Kevin Boos kevinaboos at gmail.com
Tue Jul 16 16:06:46 PDT 2013


Thanks so much for your quick reply.

I have been using hasBody(), isThisDeclarationADefinition(), etc... to
determine if the Decl* i'm looking at is actually a definition -- those are
useful. I was merely testing things with the redecl chain just to see if I
could get the definition that way even if getDirectCallee() returned the
function prototype.

How would I determine if clang has parsed the definition? Sorry, I'm a bit
of a beginner here. I thought that Clang first parsed the source file,
built an AST, and then allowed you to Visit each type of AST node. All I
know is, at that point, Clang would not yet have *visited* the definition
of foo(), but I assume it would have parsed it...?

Is it wrong to try to access or jump to a later function definition before
visiting it first?

~Kevin



On Tue, Jul 16, 2013 at 5:55 PM, Eli Friedman <eli.friedman at gmail.com>wrote:

> On Tue, Jul 16, 2013 at 3:27 PM, Kevin Boos <kevinaboos at gmail.com> wrote:
> > Hello,
> >
> > I have a piece of C code like so:
> >
> > ==========================================
> > void foo(); // just a declaration (sometimes in a header file instead)
> >
> > void bar() {
> >     // definition here
> > }
> >
> > int main() {
> >     bar();  // this one is fine
> >     foo();  // this one doesn't work
> > }
> >
> > void foo() {
> >     // definition here
> > }
> > ============================================
> >
> > I'm using VisitStmt and looking at the CallExprs in main(). For the
> "bar()"
> > function above, I can get the FunctionDecl* using
> > CallExpr::getDirectCallee(). However, that doesn't work for the "foo()"
> > function -- calling getDirectCallee() gives me the function prototype,
> not
> > the definition of "foo()"...
> >
> > So I tried using the Redeclarable class's "redecl_iterator" to dump each
> > redeclaration starting from  getDirectCallee()->redecls_begin. However,
> it
> > only iterates once for the function prototype and doesn't include the
> actual
> > definition of "foo()."
>
> Note that you probably just want to use FunctionDecl::hasBody instead
> of iterating over the redeclaration chain.
>
> > How can I get the real definition of foo() at the bottom of the code
> above?
> > I know it's probably something easy, but I just can't seem to get there.
>
> Are your sure clang has actually parsed the definition at the point
> where you are dumping it?
>
> -Eli
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130716/af8f3aa2/attachment.html>


More information about the cfe-dev mailing list