How to add an attribute¶
To add an attribute, you’ll have to add it to the list of attributes, add it tothe parsing phase, and look for it in the AST scan.r124217 has a good example of adding a warning attribute.
(Beware that this hasn’t been reviewed/fixed by the people who designed theattributes system yet.)
include/clang/Basic/Attr.td
First, add your attribute to the include/clang/Basic/Attr.td file.
Each attribute gets a def inheriting fromAttr or one of itssubclasses.InheritableAttr means that the attribute also applies tosubsequent declarations of the same name.
Spellings lists the strings that can appear in__attribute__((here)) or[[here]]. All such strings will be synonymous. If you want to allow the[[]] C++11 syntax, you have to define a list ofNamespaces, which willlet users write[[namespace::spelling]]. Using the empty string for anamespace will allow users to write just the spelling with no “::”.Attributes which g++-4.8 accepts should also have a CXX11<"gnu","spelling"> spelling.
Subjects restricts what kinds of AST node to which this attribute canappertain (roughly, attach).
Args names the arguments the attribute takes, in order. IfArgs is [StringArgument<"Arg1">, IntArgument<"Arg2">] then__attribute__((myattribute("Hello",3))) will be a valid use.
--- cfe/trunk/include/clang/Basic/Attr.td 2011/01/25 19:39:31 124216 +++ cfe/trunk/include/clang/Basic/Attr.td 2011/01/25 20:08:12 124217 @@ -232,6 +232,11 @@ let Spellings = []; } +def ForbidTemporaries : Attr { + let Spellings = ["forbid_temporaries"]; + let Subjects = [CXXRecord]; +} + def Format : InheritableAttr { let Spellings = ["format"]; let Args = [StringArgument<"Type">, IntArgument<"FormatIdx">,
Boilerplate
Write a new HandleYourAttr() function inlib/Sema/SemaDeclAttr.cpp,and add a case to the switch in ProcessNonInheritableDeclAttr() or ProcessInheritableDeclAttr() forwarding to it.
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp 2011/01/25 19:39:31 124216 +++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp 2011/01/25 20:08:12 124217 @@ -885,6 +885,24 @@ d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context)); } +static void HandleForbidTemporariesAttr(Decl *d, const AttributeList &Attr, + Sema &S) { + assert(Attr.isInvalid() == false); + + if (Attr.getNumArgs() != 0) { + S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + return; + } + + if (!isa<TypeDecl>(d)) { + S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) + << Attr.getName() << 9 /*class*/; + return; + } + + d->addAttr(::new (S.Context) ForbidTemporariesAttr(Attr.getLoc(), S.Context)); +} + static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) { if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) { S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) @@ -2674,6 +2692,8 @@ case AttributeList::AT_ext_vector_type: HandleExtVectorTypeAttr(scope, D, Attr, S); break; + case AttributeList::AT_forbid_temporaries: + HandleForbidTemporariesAttr(D, Attr, S); break; case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break; case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
The meat of your attribute
Find an appropriate place in Clang to do whatever your attribute needs to do.Check for the attribute’s presence usingDecl::getAttr<YourAttr>().
Update the Clang Language Extensions document to describe your new attribute.