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

Douglas Gregor dgregor at apple.com
Mon Jun 21 14:48:27 PDT 2010


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



More information about the cfe-dev mailing list