Attributes on statements and expressions

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).

Sean

Trevor,

Sean and me talked about the topic two weeks ago on the list. It is a good idea to have attributes as defined in the upcoming C++ standard in mind while implementing that feature in clang.

The clients would typically be static analysis tools such as:
  

IMHO one thing that will be solved using attributes in the future is a "parfor" construct.

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.

To be honest I don't like the GCC syntax and - given an proposal already accepted for standardization - I wouldn't bother implementing it but go straight for the new [[attribute]] syntax.

Best regards
Olaf Krzikalla

Actually I wasn't even aware of the new [[attribute]] syntax. (I've not been following the C++0x effort.) I only mentioned __attribute__ because that seemed to the planned approach in the original discussion I referenced.

The new [[attribute]] stuff looks cool; it's exactly what I need. In particular it allows attributes on loops, such as:

for [[ attr1 ]] (int i=0; i<num_elem; i++) {process (list_items[i]); }

The fact that it's going to be a standard makes it all the better.

For those interested in reading about it, the best material I've been able to find so far is this:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2418.pdf

It's a little old but provides a good overview.

There's one thing I'm confused about, though. I assume it's possible to define my own attributes, e.g. [[mycoolattr(42)]]. But how exactly do I do that? Is there some sample code somewhere?

Trevor

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.

Is your proposal available online somewhere?

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.

Would the attribute information be encoded as LLVM metadata (MDNode)?

Let me know if there's any way I can help in this effort.

Trevor

Is your proposal available online somewhere?

http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2010/llvm/t127230760878

Would the attribute information be encoded as LLVM metadata (MDNode)?

Let me know if there's any way I can help in this effort.

Trevor

Encoding it as a metadata node seems like a good idea, but that's something I'll probably leave to someone else. I don't know enough about LLVM's metadata to know if you'd want to just have an [[llvm::metadata(<something>)]] attribute, or individual attributes for different kinds of metadata.

Sean

Trevor Harmon schrieb:

For those interested in reading about it, the best material I've been able to find so far is this:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2418.pdf

It's a little old but provides a good overview.

Another source of interest is http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf esp. section 7.6.
This is the FCD, so we can expect this getting standardized with some minor changes only.

There's one thing I'm confused about, though. I assume it's possible to define my own attributes, e.g. [[mycoolattr(42)]]. But how exactly do I do that? Is there some sample code somewhere?

I don't know of examples. The FCD states that the behavior of attributes other than the predefined ones are implementation defined. That is you can define your own attributes at will in the compiler implementation. (hmm - that would prevent the standardization of other attributes in the future. I'm going to raise that issue in csc++...)

Best regards
Olaf Krzikalla

Have you considered the opportunity to store in the AST attribute also
the attribute arguments expressions?

This, together with the insertion of a generic attribute would permit to
add arbitrary annotations to source code to be used by analysis client
and other clients.

The reason to have a regular expression AST for argument has two benefits:

1) the possibility to know the original syntactic form

2) the resolution of identifiers in the exact scope where they appeared

I'm only familiar with the Java mechanism, which works like this:

// Definition of custom attribute
public @interface RequestForEnhancement {
     String synopsis();
     String engineer() default "[unassigned]";
     String date() default "[unimplemented]";
}

// Use of custom attribute
@RequestForEnhancement(
     synopsis = "Enable time-travel",
     engineer = "Mr. Peabody",
     date = "4/1/3007" )
public static void travelThroughTime(Date destination) { ... }

The nice thing about this is that the annotation data is (optionally) embedded into the Java class file's metadata, and it can then be extracted by tools by means of a well-defined spec.

Is there a way to do something similar to the above in C++0x?

Trevor