[cfe-dev] Tagging AST with metadata

Don Williamson don.williamson at yahoo.com
Wed Mar 14 03:06:08 PDT 2012


Hi,

I've been building a C++ reflection framework using clang for quite a while ( clReflect, https://bitbucket.org/dwilliamson/clreflect ) and I'm slowly going through the process of stripping functionality, making everything as easy to use and minimal as possible. After using it in anger for some months, I've identified the attribute system as a potential avenue for improvement.

One of the problems I've had to solve is how to tag specific C++ primitives with flexible attributes that can be inspected in the AST. Currently, it's achieved as follows:

clcpp_attr(flag_attribute, symbol_attribute = symbol, text_attribute = "text", number_attribute = 3)
struct SomeType
{
clcpp_attr(more_attributes)
int blah;
};

clcpp_attr is a macro, defined as:

#ifdef __clang__
#define clcpp_attr(...) __attribute__((annotate("attr": # __VA_ARGS__)))
#else
#define clcpp_attr(...)

The user runs an app called clscan, which embeds clang. This can easily pair AST attributes to their primitives. The user then runs their own compiler which uses the empty implementation of clcpp_attr.

This works. However, some drawbacks of this approach are:

* All header files that use these need to pull in the clReflect header, which I would like to reserve purely for when you want to query reflection information.
* The attributes are pretty verbose (typing clcpp_attr is annoying).
* Where you apply attributes in clang is inconsistent and impossible for some primitives (e.g. namespaces and templates).

So I'm throwing around some alternatives at the moment. My requirements are:

* Does not require C++11 and can potentially be used in C at a later date.
* Does not require that your source be un-compilable without a pre-process step to convert it.

One option I've thought of is using comments, similar to doxygen markup:

// @: flag_attribute, symbol_attribute = symbol, text_attribute = "text", number_attribute = 3
struct SomeType
{
// @: more_attributes
int blah;
};

However, scanning the mailing list archives I found reference to this commit, which strips the ability for comments to exist in the AST:

http://llvm.org/viewvc/llvm-project?view=rev&revision=99007

Are there any plans to bringing this back? Has it already been brought back? (I left the trunk after 3.0 became stable) Alternatively, can anybody think of anything within clang that could make this a little easier? I don't mind scheduling some time over the next few months to add this back, if I can make use of it.

Thanks for reading this lengthy mail.

Cheers,
- Don




More information about the cfe-dev mailing list