[cfe-dev] C code instrumentation

Steffen Hanikel hanikesn at studi.informatik.uni-stuttgart.de
Mon Nov 26 04:39:58 PST 2012


for my project I need to instrument C code. I.e. inserting statements
before instructions, branches and conditions. There seem to be 3

1. Instrument the LLVM IR. This won't work because I don't have
control over the compiler being used.
2. Use the Rewriter API to rewrite the source code.
3. Manipulate the AST using SEMA and dump it afterwards.

At first I tried option 2. This worked quite well, if the code doesn't
use the preprocessor. The Rewriter doesn't work well if the code is
"hidden" in a macro expansion. I tried expanding the macros while
visiting the AST but this didn't work out so well. I can't expand the
macros beforehand, because I need the original context information.
I didn't try option 3, yet. Because it was suggested that it isn't a
good option. And I can't find documentation.
> Direct AST manipulations will be rather tricky, since the Clang AST is designed to be immutable once created [...] [0]

Do you have any advice regarding the best option? Maybe there's
another possibility that I overlooked?


[0] http://lists.cs.uiuc.edu/pipermail/cfe-dev/2011-January/012898.html

More information about the cfe-dev mailing list