[cfe-dev] Getting meta-data into Clang's AST

Douglas Gregor dgregor at apple.com
Fri Jul 2 10:22:31 PDT 2010

On Jul 2, 2010, at 10:03 AM, chris nuernberger wrote:

> I completed (quite easily) the first part of my task which was to get
> clang to parse some c++ objects and write out some serialization
> mappings for them.


> Now I have seemingly much harder task which is to parse some c++
> objects and not only get their binary layout

ASTRecordLayout has that information.

> but also to be able to
> read some pretty arbitrary meta data about each member.
> Is there a recommended way to add in meta data to the Clang AST from
> the parser?  

Attributes, pragmas, and comments are the typical approaches. I would recommend *not* using pragmas, because associating them with specific declarations is a real pain. Attributes are okay for lightweight metadata, and in the future it'll be far easier to add your own attributes. Comments give the most flexibility, although you'll still need to solve the problem of associating a comment with the declaration(s) it applies to.

> I noticed comments seem to be completely stripped; is
> this the case or did I mis-read something?

Yes, this is the case, although it was actually a recent chang. See


where I ripped out our handling of comments because they were completely unused. I'm not opposed to bringing comments back if they're actually going to be used for something real *cough* Doxygen parsing *cough*.

> Comments would be ideal as the other compilers for these objects will
> then definitely ignore the data, but I could also declare a special
> objects with string constants or something like that; assuming it
> didn't change the binary layout of the object.
> Has anyone done something like this before?

Not in Clang, but it's fairly common to use comments for metadata.

	- Doug

More information about the cfe-dev mailing list