C code instrumentation

Hi,

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

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?

Regards,
Steffen

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

Just FYI: we're doing 2) pretty extensively. Macros are tricky, but by
staring at a problem long enough you can usually find a solution. We also
have some ideas to build higher-level abstractions for things like "here's
an AST node, please insert a local variable before this point at the
appropriate scope", but that's still early stages.

Cheers,
/Manuel