[cfe-dev] adding custom attributes

Mattias Holm holm at liacs.nl
Fri Aug 21 06:17:58 PDT 2009


> I'm new to clang and LLVM. I want to extend clang to handle
> non-standard variable attributes. What would be the best approach to
> that? Any help would be appreciated.

Depending on what you want to do, for now I assume that you are trying 
to do something like:

int x __attribute__((foo));

And then have the foo attribute apply to the declaration.

The interesting files for this are:

include/Parser/AttributeList.h: Define an enum entry AT_foo
lib/Parser/AttributeList.cpp: Add a memcmp call and return in order to 
allow the decoding of the attribute at the appropriate point; in the 
case of foo that would be under case 3.

If you want your attribute to apply to the declaration, check the 
function ProcessDeclAttribute in lib/Sema/SemaDeclAttr.cpp, most of the 
handler called by that function do 2 things, they check the context of 
the attribute and the number of argument, and they insert the attribute 
in the declaration's attribute list, the declaration attributes need to 
inherit from the Attr class, you can find this in include/AST/Attr.h 
where a number of standard attribute classes are defined with the 
DEF_SIMPLE_ATTR macro, that macro will generate an attribute class that 
is good enough for simple boolean attributes.

Your AST consumer can then read the attributes associated with the 
declaration in a later stage (check the clang Doxygen docs for how to do 
this).

If you are trying to add the attributes to types and not the actual 
declarations, then it is a bit more tricky and you should check the 
thread that I started called "adding attribute bits to types".


Regards,
Mattias




More information about the cfe-dev mailing list