[cfe-dev] Parsing VCC annotations with clang

Frits van Bommel fvbommel at gmail.com
Tue May 31 05:33:58 PDT 2011


On 31 May 2011 13:48, Florian Merz <florian.merz at kit.edu> wrote:
> If I used a macro such as
>
>  #define _(x) // x
>
> so that x is not removed but turned into a comment instead. Can this comment
> be accessed in clang and can I determine the corresponding
> function/struct/loop with this? It seems like clang::CommentHandler is able to
> handle comments and determine the range of the comment in the source file. From
> there I'd expect to be able to determine the surrounding object easily. Am I
> on the right track with this?

IIRC the comment gets removed before the #define is processed, so the
macro definition won't even see the comment and this won't work.


I was /going/ to suggest
    #define _(x) __attribute__((annotate(#x)))
because if it would work then you could just look for functions in the
AST with an AnnotateAttr attribute whose getAnnotation() starts with
"requires " or "ensures ". (You'd have to parse the expressions
yourself though, as the AST would consider them to just be strings)

Unfortunately it seems the place your _() appears isn't currently a
valid attribute location though. The GCC manual does say "An attribute
specifier list may, in future, be permitted to appear after the
declarator in a function definition (before any old-style parameter
declarations or the function body)."[1], but both GCC and Clang don't
currently seem to like attributes there (and __attribute((annotate))
isn't recognized by GCC anyway).

Maybe a patch to Clang to allow them there would be accepted though?


[1]: http://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html




More information about the cfe-dev mailing list