manipulating @llvm.var.annotation in AST?

Is there anyway to manipulate @llvm.var.annotation at the AST level?

In the source file you could do something like

Is it possible todo something like that with a Clang::DeclRefExpr? Could something be done to @llvm.var.annotation “by hand”? How do you access this?

This becomes an AnnotateAttr on the VarDecl. There's nothing stopping you from adding this to an AST, as long as you obey the syntactic constraints on the operand.


I do not see any kind of AnnotateAttr object in VarDecl or anything it inherits, and I also dont see where AnnotateAttr inherits something for it to be inserted into the AST.

How exactly do you add a AnnotateAttr? What is it added to?

Thank you for the reply

Attr objects represent attributes. You can add them to arbitrary Decls with Decl::addAttr().


Now that they are added, how is this saved for later use?

The PrintFunctionNames example is ran by
clang -cc1 -load ~/llvm/Release+Asserts/lib/ -plugin print-fns input_file.c

But how does this get stored/saved for the Pass phase? How do the new contents of llvm.var.annotation get stored/saved? “-emit-llvm -S” does not seem to help

Thanks again

Probably IR generation is running before you make your changes to the AST. I don't know the plugin system well enough to know if/how this is fixable.


My plugin now adds AnnotateAttr objects directly to VarDecls, and writes out the AST file. This AST file is compiled to clang, with the -S -emit-llvm options, and there is nothing about these AnnotateAttr in there. I can tell they are being add because they print to the screen before hand, but there are no traces in the IR file.

I was expecting to see my string or atleast a reference to @llvm.var.annotation.

Am I missing something?

Thank you

That should work. It's possible that the attributes are not being
serialized or deserialized properly; I would check whether it works
when you don't round-trip through an AST file.


Theres a direct way todo this? How can I avoid writing a AST file? When I run

clang -cc1 -load ~/llvm/Release+Asserts/lib/ -plugin print-fns test_file.c
clang -cc1 -load ~/llvm/Release+Asserts/lib/ -plugin print-fns test_file.c -S -emit-llvm

I don’t get any output, just my own printouts.

It might entail changing clang; again, I am not an expert in the limitations of the current plugin hack.


Try s/-plugin/-plugin-add/.

error: unknown argument: ‘-plugin-add’

Full use was: clang -cc1 -load ~/llvm/Release+Asserts/lib/ -plugin-add print-fns double_assigns.c

-add-plugin? :slight_smile:

Thanks, that does run without error.

Still no difference in the IR file though

Ah, didn't read the start of the thread. The AST is effectively
immutable once built: If you change it, you might break invariants
that modules further down the road make. That's why -add-plugin
plugins run after codegen.


Ouch, this is painful. So even Annotations/Attributes are not allowed? Both seem particular useful and trivial, along with safe.

I’m asking this mostly because I’m saving the AST I modified myself and then later calling clang on it. So it seems like it doesn’t matter if CodeGen already ran the first time, its the second that counts.

Sorry if I’m still beating a dead horse about this, I’m a bit confused and cant imagine why something like this wouldn’t be much easier and a basic feature. Keep thinking I’m missing something simple.

Thanks everyone