[cfe-dev] support for annotations in clang

Chris Lattner clattner at apple.com
Sun Nov 25 10:02:32 PST 2007


On Nov 25, 2007, at 8:57 AM, Zvonimir Rakamaric wrote:
> I am looking for a C front-end that has some kind of support for
> processing source code annotations in the form of pre- and
> post-conditions and loop invariants. So far, I haven't found anything
> suitable for the task (if you are aware of any C front-ends that
> support annotations, please let me know), and given that clang is a
> new project, it might be a good place to start. Exploiting comments,
> which was mentioned in a recent thread, would be for instance one of
> the ways to go.
>
> Please let me know if anybody is already working on that, or planning
> to in the near future...
>
> Also, would you even be interested in adding support for annotations
> to clang? I think that would be a very useful feature since many
> static analysis tools rely on some sort of user-provided annotations,
> and having a uniform support for those would be great.

Hi Zvonimir,

I would definitely like to see clang go in this direction.  At the  
very least, we will eventually need to grow support for the generic  
string annotation attribute that llvm-gcc supports.  Attributes are  
ugly syntactically, but they are recognized as a first class part of  
the grammar by the parser, so they are more predictable and robust  
than comments.   Also, in practice, the ugly syntax is normally hidden  
with macros in the user source.

This attribute is used like this (from the regression test):

/* Global variable with attribute */
int X __attribute__((annotate("GlobalValAnnotation")));

/* Function with attribute */
int foo(int y) __attribute__((annotate("GlobalValAnnotation")))
                __attribute__((noinline));

int foo(int y __attribute__((annotate("LocalValAnnotation")))) {
   int x __attribute__((annotate("LocalValAnnotation")));
   x = 34;
   return y + x;
}

The nice thing about this is that it means the user can attach  
arbitrary info to the AST (which is then transfered to the LLVM IR),  
and then clients of the AST or the LLVM IR can then use this  
information to do whatever they want.  Strings are nice because they  
are fully general, and you can put whatever you want in there.  For  
example, you could do:

int foo(int y) __attribute__((annotate("frequency:100,badness:42")))

etc.

llvm-gcc (and gcc in general) don't support putting attributes on  
statements and expressions, but I would like to extend clang to  
support this.  If you'd be interested in working on this, it would be  
a great contribution to clang!

-Chris




More information about the cfe-dev mailing list