[cfe-dev] Attributes on statements and expressions

Sean Hunt rideau3 at gmail.com
Fri Apr 23 22:17:27 PDT 2010

On 04/23/2010 04:59 PM, Trevor Harmon wrote:
> Hello Clangers,
> Some time ago, there was talk of extending Clang to allow attributes
> on statements and expressions [1]. By relaxing the restrictions on
> where attributes can be placed, users would be able to attach
> arbitrary info to the AST, which would then be transferred to the LLVM
> IR (presumably as LLVM 2.7 metadata [2]). Clients of the AST or the
> LLVM IR could then use this information to do whatever they want.
> The clients would typically be static analysis tools such as:
> * Design-by-contract checkers (to declare pre-, post-, and loop
> invariants)
> * Defect finders like Clang's own Static Analyzer (to eliminate false
> positives)
> * WCET analyzers (to define loop bounds)
> I would like to begin work on a patch that implements this feature in
> Clang. The first order of business is to decide how the attributes
> would look in the user source code. One approach is to retain the
> existing GCC __attribute__ syntax and simply allow the __attribute__
> token to appear on statements and expressions. (Currently it is only
> allowed on variable, type, and function declarations.) For example:
>     void myFunc(...) {
>         __attribute__((loopbound(50))
>         while (...) {
>             ...
>         }
>     }
> Any comments or questions on this? I'd also appreciate any tips on how
> to get started, as well as admonitions of obstacles I might encounter.
> Thanks,
> Trevor
> [1] http://lists.cs.uiuc.edu/pipermail/cfe-dev/2007-November/000473.html
> [2] http://blog.llvm.org/2010/04/extensible-metadata-in-llvm-ir.html

My Summer of Code project is to overhaul the way attributes are handled 
in clang. Adding attributes to statements is one of the parts of this 
project, in order to add support for the forthcoming C++0x standard.

Once attributes are placed onto the Stmt nodes, it would then be the 
responsibility of the CodeGen module to encode attribute information 
into the LLVM IR. Ideally, getting them there would require only a small 
amount of effort - ideally only a few lines added to a definition file. 
 From there, you would have to add the CG code to forward it into LLVM 
(and, of course, code to actually do something with the metadata).


More information about the cfe-dev mailing list