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

Eli Friedman eli.friedman at gmail.com
Tue Jul 16 15:55:43 PDT 2013


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



More information about the cfe-dev mailing list