[cfe-dev] C code instrumentation

Manuel Klimek klimek at google.com
Tue Nov 27 01:17:40 PST 2012

On Mon, Nov 26, 2012 at 1:39 PM, Steffen Hanikel <
hanikesn at studi.informatik.uni-stuttgart.de> wrote:

> 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?

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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20121127/947fb674/attachment.html>

More information about the cfe-dev mailing list