[cfe-dev] About AST rewriting / manipulation

Simone Pellegrini spellegrini at dps.uibk.ac.at
Fri Jan 23 08:42:41 PST 2009


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!

regards, Simone





More information about the cfe-dev mailing list