[RFC] New attribute `annotate_type` (iteration 2)

“Contributing Extensions to Clang” criteria

A reviewer on ⚙ D111548 [Clang] Add the `annotate_type` attribute requested input on the criteria set up in “Contributing Extensions to Clang”. It appears that it is no longer possible to edit the original post, so I will provide that material here.

A specific need to reside within the Clang tree

While it is possible for Clang-based tools to add custom declaration attributes using Clang plugins, the same is not true for type attributes.

We investigated the feasibility of extending the plugin mechanism to type attributes (see this abandoned patch). However, the conclusion from this discussion was that adding type attributes through a plugin is significantly more difficult than adding declaration attributes because one would want to be able to specify the effects that the pluggable type attribute has on the type system. A quote from @AaronBallman on the review: “I love the idea of plugin type attributes in theory, but I don’t think we’re architecturally in a place where we can do that very easily.”

Based on this, we decided to proceed with the alternative of adding a general-purpose type annotation attribute to Clang.

A specification

D111548 adds documentation that describes both the syntax and semantics of the annotate_type attribute.

Representation within the appropriate governing organization

At this time, we do not intend to propose the annotate_type attribute as an extension to the C or C++ standards. Like the existing annotate declaration attribute, we are proposing to make annotate_type a vendor extension and, hence, to place the attribute in the clang namespace.

A long-term support plan

The change is small and self-contained, and we do not expect it to require significant support. To the extent that the feature does require support in the face of changes to Clang or the language itself, we can commit to providing this support as part of Google’s broader commitment to the Clang/LLVM project.

Note that a significant part of D111548 is code that is being moved around. The non-test, non-documentation code changes are less than 100 lines:

  • A small change in Parser::ParseDeclarationSpecifiers enables adding C++11 attributes to a decl-specifier-seq.
  • The rest of the changes consist of an attribute definition and attribute handlers. These are well-contained within switch cases for the new attribute kind. They do not make any broader architectural or behavioral changes to Clang.

A high-quality implementation

D111548 provides an implementation that follows LLVM’s coding conventions and meets Clang’s quality standards. The primary reviewer has stated “I’m going to add another reviewer just to make sure I’ve not missed something, but I think this is about ready to go”.

A test suite

D111548 adds a comprehensive test suite for the annotate_type attribute, covering semantic analysis, codegen (or rather lack of influence on codegen), and representation of the attribute in the AST.