<div style="font-family: arial, helvetica, sans-serif; font-size: 10pt">On Mon, Nov 26, 2012 at 1:39 PM, Steffen Hanikel <span dir="ltr"><<a href="mailto:hanikesn@studi.informatik.uni-stuttgart.de" target="_blank">hanikesn@studi.informatik.uni-stuttgart.de</a>></span> wrote:<br>
<div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
for my project I need to instrument C code. I.e. inserting statements<br>
before instructions, branches and conditions. There seem to be 3<br>
options:<br>
<br>
1. Instrument the LLVM IR. This won't work because I don't have<br>
control over the compiler being used.<br>
2. Use the Rewriter API to rewrite the source code.<br>
3. Manipulate the AST using SEMA and dump it afterwards.<br>
<br>
At first I tried option 2. This worked quite well, if the code doesn't<br>
use the preprocessor. The Rewriter doesn't work well if the code is<br>
"hidden" in a macro expansion. I tried expanding the macros while<br>
visiting the AST but this didn't work out so well. I can't expand the<br>
macros beforehand, because I need the original context information.<br>
I didn't try option 3, yet. Because it was suggested that it isn't a<br>
good option. And I can't find documentation.<br>
> Direct AST manipulations will be rather tricky, since the Clang AST is designed to be immutable once created [...] [0]<br>
<br>
Do you have any advice regarding the best option? Maybe there's<br>
another possibility that I overlooked?<br></blockquote><div><br></div><div>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.</div>
<div><br></div><div>Cheers,</div><div>/Manuel</div><div> </div></div></div></div>