[cfe-dev] About AST rewriting / manipulation

Ben Laurie benl at google.com
Tue Feb 3 10:37:43 PST 2009


On Fri, Jan 23, 2009 at 8:42 AM, Simone Pellegrini
<spellegrini at dps.uibk.ac.at> wrote:
> Hello everyone,
> I am trying to use Clang as a source-to-source compiler. Through the API
> I've found the way to rewrite back the syntax tree into source code, and
> that's not difficult. However, before writing back the syntax tree I
> would like to manipulate the syntax tree in order to apply some code
> transformations.
>
> For example I would like to rewrite something like f(a,b) into g(b, a, c)
>
> Now I guess I should create the AST nodes I need (building a new
> CallExpr... object and so on...) and then substitute the old f(...) with
> the new g(...). The Clang API for creating AST nodes is nevertheless
> quite complex to use, it's really too demanding.
>
> Now, I am wonder that It would be very nice if I could write the
> statement I want to substitute (or to add) as a string and then use the
> the Clang parser to create the syntax tree of the piece of code I have
> written in a way it can be easily plugged in the old main syntax tree
> (of course the new instance of the parser should be invoked considering
> the previous context...). Is it possible to have this kind of behavior?
> Or my imagination simply went too far? :)
>
> Any pointers / references related to this kind of topic are really welcome!

We do this in the Caja compiler
(http://code.google.com/p/google-caja/) using things we call
quasiliterals.

For the above you would match something like "f(@a, @b)" and output
something like "g(@b, @a, c)".

For some examples, see
http://code.google.com/p/google-caja/source/browse/trunk/src/com/google/caja/plugin/stages/CajaRuntimeDebuggingRewriter.java.

Unfortunately you can't reuse our code very easily because a) it is in
Java and b) it compiles Javascript, but I'm sure the ideas are very
similar for C/C++.



More information about the cfe-dev mailing list