[cfe-dev] How to annotate an arbitrary source line

James Stark mrjamesstark at gmail.com
Sun Jan 19 00:56:58 PST 2014


Hi,

I'm trying to push LLVM annotations from source code into IR. I use
the following syntax to achieve that.

__attribute__((annotate(STRING)))

For example:

__attribute__((annotate(“my message")))
int a = 1;

However, this mechanism is applicable to only certain types of
statements. For example, I can annotate a function declaration, but
not a function call:

 __attribute__((annotate("my message"))) memcpy(buffer, "hello", 6);
// Compile-time error!

I thought I could get around this problem by using an empty statement such as:

 __attribute__((annotate("my message"))); //Notice a semicolon here.
But when I see this annotation "my message", I apply it to the next
source line below.
 memcpy(buffer, "hello", 6);

Unfortunately, this hack didn't work out because the compiler didn’t
push the annotation into the bitcode.

Instead, the following is a hack that fixes the problem:

 __attribute__((annotate("my message"))) int a; //Notice a dummy
statement “int a"
 memcpy(buffer, "hello", 6);

It uses a dummy statement “int a” but I use the annotation "my
message" for the memcpy call in the next line. It compiles clean and
its IR does have the annotation in it as well. However, I think this
is an ugly solution, not to mention extra IR statements for the local
variable a and memory overhead.

I'd appreciate any ideas.

Thanks!
James




More information about the cfe-dev mailing list