[cfe-dev] Using clang as a meta-data generator.

chris nuernberger cnuernber at gmail.com
Tue Jun 22 09:16:06 PDT 2010


OK, cool.  I have it producing an AST.  I don't need it to compile or
even produce a perfect AST, but I do need it to attempt to produce as
much of the AST as it can.  It appears to do that; can I always count
on that behavior?

This is on a large, established code base (PhysX) so I can't mess with
the code base much nor do I want to.

So, just some notes: unless I use cc1, I get a bizarre error message
stating that clang itself can't execute "clang".  It appears the
driver wraps the clang invocation with many more command line
arguments and relaunches.  This doesn't appear to work on windows; at
least not right now.

__int64 isn't an intrinsic type so the windows c headers won't
compile.  Judging by the compiler output, it looks like you would want
a truly portable std library implementation or something like that.

For this stage of the project, I just need to know variable names.

For the next stage, when I need to mangle binary data, I will need to
know the exact binary offsets of member variables.  How do you
recommend I go about getting this information?  It seems much later in
the pipeline but something must annotate the AST graph with such
information somewhere...

Chris

On Mon, Jun 21, 2010 at 3:48 PM, Douglas Gregor <dgregor at apple.com> wrote:
>
> On Jun 21, 2010, at 2:16 PM, chris nuernberger wrote:
>
>> Hey,
>>
>> I need to expose the binary layout of several c++ classes to a third
>> party system.  I would like to use clang to parse the header files of
>> these classes and output a meta data table describing the count and
>> types of all member variables of a given class.
>>
>> I have never looked at Clang and this is a very prototype project but
>> it could help a lot of people when they need to write, read, and
>> manipulate their c++ classes.  An example usage, for instance, would
>> be to automatically produce boost::serialization bindings for your
>> classes.  Or to a c++->XML or god forbid OODB mapping without needing
>> to change your original classes at all.
>>
>> Has anyone tried to do this before using Clang?
>
>> Is it an appropriate usage?  It seems like it would be a great test or
>> example usage of Clang as a pure front end.
>
> Yes, absolutely. Clang was designed for this kind of thing.
>
>> If I should start, where should I start?  My initial plan is to get
>> Clang, figure out which libs I need to produce the AST in memory (not
>> output to a file) and figure out how to manipulate this AST to find
>> all the target types and output their binary layout.
>
>
> That's about it! One easy way to play with this is to check out the BoostConAction in the source tree, by looking through everything referred to via "boostcon". That will show you how to get to parsing an AST into memory and then walking it relatively quickly. The magic incantation to try out the BoostCon action is:
>
>        clang -cc1 -boostcon source-file.cpp
>
> The BoostCon action will eventually go away, once someone has time to write a decent tutorial on creating a new action.
>
>        - Doug



-- 
A foolish consistency is the hobgoblin of little minds - Emerson




More information about the cfe-dev mailing list