[cfe-dev] Extracting the include tree from a source file

Nico Weber thakis at chromium.org
Mon Nov 15 08:49:55 PST 2010


Hi,

Call InitializePreprocessor to add defines. To fix the crash, add
calls to BeginSourceFile()/EndSourceFile() (see
https://github.com/nico/clangtut/blob/master/tut03_pp.cpp ).

Take a look at include/clang/Lex/PPCallbacks.h – you probably want to
derive a class from that and set it on the preprocessor to be notified
of includes and defines in the code.

Nico

On Mon, Nov 15, 2010 at 5:17 PM, Bertjan Broeksema <broeksema at kde.org> wrote:
> Hi all,
>
> For a small program [1] I'm working on to analyze physical properties of a
> C++ project, I'm interested in integrating clang. The first (and I think
> relative simple) step I'd like to take is using clang to extract the
> included headers.
>
> My program already has an internal project graph containing source files,
> object files, executables and (external) libraries. This graph I want to
> complete with the headers.
>
> So my question is, what is the easiest way, using clang (programmatically)
> to extract the include tree given a path to a source file, a list of include
> directories (i.e. the one past to the compiler, not internal paths) and a
> list of defines (again, only the defines past to the compiler).
>
> I attached some initial code I'm playing with (not integrated with the rest
> of my program, just to get a feeling on how to use clang). Currently it
> somewhat works, but it doesn't find system includes (i.e. the headers that
> the compiler normally internally resolves) and if something goes wrong, e.g.
> a header is not found, it just plain crashes. Also, its not clear to me how
> to set defines and finally most important, how to actually get the include
> tree after (or during?) preprocessing.
>
> Any help is appreciated.
>
> Cheers,
>
> Bertjan
>
> [1] http://gitorious.org/cpp-dependency-analyzer
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>




More information about the cfe-dev mailing list