天天看點

LLVM每日談之十四 如何給Clang添加一個屬性

每次内容主要出自文檔: “Clang” CFE Internals Manual

位址: http://clang.llvm.org/docs/InternalsManual.html

在這個文檔中,以簡明的的一個例子說明了如何按照這個方式在Clang中添加一個屬性。講的很清楚,我在這裡不再累述,隻是将例子中的部分代碼和文檔結合起來,一起展現給大家。

of adding a warning attribute.

(Beware that this hasn’t been reviewed/fixed by the people who designed theattributes system yet.)

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.

add a case to the switch in ProcessNonInheritableDeclAttr() or

ProcessInheritableDeclAttr() forwarding to it.

If your attribute causes extra warnings to fire, define a

Spelling with “_”s replaced by “-“s. If you’reonly defining one diagnostic, you can skipDiagnosticGroups.td and useInGroup<DiagGroup<"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>().