Adding custom pragma to Clang


I am trying to add custom pragma to clang to annotate extra infromation to variables and loops.

I had a look at the OpenMP pragma handling a little bit, and found that adding custom pragma like OpenMP to clang is not easy, one need to modify the lexer, introduce dedicated node to AST, etc.

But on the other hand, I am thinking the pragma for variable and loop annotating may not as complicated as those OpenMP pragmas. Is there any simple pragma in clang that I can look at?



don’t know whether this helps (or even still works), but some time ago I had some fun with the attached patch: It stops compilation until user has accepted an EULA (please don’t kill me, this was no serious work…)

I mostly copied from PragmaOnce, but you may need to do some more work to finally annotate variables and loops.



PragmaEula.diff (1.48 KB)


It might be interesting to look at

They create new PragmaHandler that are called by clang when processing pragma (see The problem is that (if I remember correctly) it happens before semantic processing and it is therefore hard to match them to actual loops or objects, at that time you mostly have access to the line number.

Maybe using a secondary pass on the clang ast to add the attributes is all that is needed.