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

chris nuernberger cnuernber at gmail.com
Fri Jul 2 10:29:27 PDT 2010

OK, lets look at attributes for a second.

What are they?  A google search for clang attributes of course doesn't
turn up much.

Really, if I could embed arbitrary text in an attribute associated
with a member variable this would do it; I could design a small DSL to
do what I need.  Does this sound feasible, even if this text is
somewhat verbose?

If not, the changelist about the comments looks pretty involved; I
will want to discuss that further but I am really hoping that
attributes will do.


On Fri, Jul 2, 2010 at 11:22 AM, Douglas Gregor <dgregor at apple.com> wrote:
> 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.
> Great!
>> 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
>        http://llvm.org/viewvc/llvm-project?view=rev&revision=99007
> 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

A foolish consistency is the hobgoblin of little minds - Emerson

More information about the cfe-dev mailing list