Getting past error: member access into incomplete type

Hi,

I'm working on a small AST-manipulation tool built upon clang [1]. I
had an implementation which worked but didn't deal well with included
headings. I've since re-written this using the Tooling interface,
however using the example code from [2] I run into the following "member
access into incomplete type" error on an ASTContext [3].

It looks like I could solve this error by stepping back to previous
versions of clang and llvm [4], but I'd prefer to fix this so that it
works with the current code and moving forward. The broken file is at,
https://github.com/eschulte/clang-mutate/blob/master/mutate-tool.cpp

Any suggestions would be greatly appreciated.

Thanks,

Footnotes:
[1] https://github.com/eschulte/clang-mutate

[2] http://clang.llvm.org/docs/RAVFrontendAction.html

[3] $ make mutate-tool
     clang++ -I/usr/local/include -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -g -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -fPIC -Woverloaded-virtual -Wcast-qual -fno-rtti -c -o mutate-tool.o mutate-tool.cpp
     mutate-tool.cpp:176:33: error: member access into incomplete type
           'clang::ASTContext'
         Visitor.TraverseDecl(Context.getTranslationUnitDecl());
                                     ^
     /usr/local/include/clang/Basic/Builtins.h:29:9: note: forward
           declaration of 'clang::ASTContext'
       class ASTContext;
             ^
     1 error generated.
     make: *** [mutate-tool.o] Error 1

[4] http://permalink.gmane.org/gmane.comp.debugging.lldb.devel/982

If I'm reading this correctly, your code just needs to #include the
definition of the ASTContext class (from
include/clang/AST/ASTContext.h) in order to refer to a member of
ASTContext (e.g., "Context.getTranslationUnitDecl()").

-- James

I agree with James.

There has been refactoring on the headers in the AST and Sema (I think) to move from direct inclusion to forward declaration which probably broke a few clients that relied on the inclusions being done for them. Normally if you would just make the inclusion yourself now, it should work as before.

– Matthieu

Matthieu Monrocq <matthieu.monrocq@gmail.com> writes:

> Hi,
>
> I'm working on a small AST-manipulation tool built upon clang [1]. I
> had an implementation which worked but didn't deal well with included
> headings. I've since re-written this using the Tooling interface,
> however using the example code from [2] I run into the following "member
> access into incomplete type" error on an ASTContext [3].
>
> It looks like I could solve this error by stepping back to previous
> versions of clang and llvm [4], but I'd prefer to fix this so that it
> works with the current code and moving forward. The broken file is at,
> https://github.com/eschulte/clang-mutate/blob/master/mutate-tool.cpp
>
> Any suggestions would be greatly appreciated.

If I'm reading this correctly, your code just needs to #include the
definition of the ASTContext class (from
include/clang/AST/ASTContext.h) in order to refer to a member of
ASTContext (e.g., "Context.getTranslationUnitDecl()").

-- James

I agree with James.

There has been refactoring on the headers in the AST and Sema (I think) to
move from direct inclusion to forward declaration which probably broke a
few clients that relied on the inclusions being done for them. Normally if
you would just make the inclusion yourself now, it should work as before.

-- Matthieu

Thanks,

Having added the ASTContext include directive (and copied some linking
behavior from tools distributed with clang) I'm now compiling
successfully.

However it seems that some more work will be required to inform clang's
search for included files. When running on a simple hello world example
file which includes stdio.h, clang tools run without problem, e.g.,

    $ clang hello.c
    $ clang-check hello.c
    Processing: /home/eschulte/src/clang-mutate/hello.c.

however my tool (using the Tooling interface) results in the following.

    $ ./clang-mutate n hello.c
    numbering
    Processing: /home/eschulte/src/clang-mutate/hello.c.
    In file included from /home/eschulte/src/clang-mutate/hello.c:1:
    /usr/include/stdio.h:33:11: fatal error: 'stddef.h' file not found
    # include <stddef.h>
              ^
    Stack dump:
    0. <eof> parser at end of file
    zsh: segmentation fault ./clang-mutate n hello.c

Any ideas what could be going wrong here? FWIW all of this code is
available at https://github.com/eschulte/clang-mutate, and the above
error can be demonstrated by running "make check".

Thanks,

Matthieu Monrocq <matthieu.monrocq@gmail.com> writes:

> Hi,
>
> I'm working on a small AST-manipulation tool built upon clang [1]. I
> had an implementation which worked but didn't deal well with included
> headings. I've since re-written this using the Tooling interface,
> however using the example code from [2] I run into the following "member
> access into incomplete type" error on an ASTContext [3].
>
> It looks like I could solve this error by stepping back to previous
> versions of clang and llvm [4], but I'd prefer to fix this so that it
> works with the current code and moving forward. The broken file is at,
> https://github.com/eschulte/clang-mutate/blob/master/mutate-tool.cpp
>
> Any suggestions would be greatly appreciated.

If I'm reading this correctly, your code just needs to #include the
definition of the ASTContext class (from
include/clang/AST/ASTContext.h) in order to refer to a member of
ASTContext (e.g., "Context.getTranslationUnitDecl()").

-- James

I agree with James.

There has been refactoring on the headers in the AST and Sema (I think) to
move from direct inclusion to forward declaration which probably broke a
few clients that relied on the inclusions being done for them. Normally if
you would just make the inclusion yourself now, it should work as before.

-- Matthieu

Thanks,

Having added the ASTContext include directive (and copied some linking
behavior from tools distributed with clang) I'm now compiling
successfully.

However it seems that some more work will be required to inform clang's
search for included files. When running on a simple hello world example
file which includes stdio.h, clang tools run without problem, e.g.,

    $ clang hello.c
    $ clang-check hello.c
    Processing: /home/eschulte/src/clang-mutate/hello.c.

however my tool (using the Tooling interface) results in the following.

    $ ./clang-mutate n hello.c
    numbering
    Processing: /home/eschulte/src/clang-mutate/hello.c.
    In file included from /home/eschulte/src/clang-mutate/hello.c:1:
    /usr/include/stdio.h:33:11: fatal error: 'stddef.h' file not found
    # include <stddef.h>
              ^
    Stack dump:
    0. <eof> parser at end of file
    zsh: segmentation fault ./clang-mutate n hello.c

Any ideas what could be going wrong here? FWIW all of this code is
available at https://github.com/eschulte/clang-mutate, and the above
error can be demonstrated by running "make check".

The tool needs to find the builtin include directory.

See http://clang.llvm.org/docs/LibTooling.html#builtin

Cheers,
/Manuel

Manuel Klimek <klimek@google.com> writes: