[cfe-dev] About AST rewriting / manipulation

Douglas Gregor dgregor at apple.com
Mon Feb 2 09:04:51 PST 2009


On Feb 2, 2009, at 8:40 AM, Simone Pellegrini wrote:
> Thanks for the response,
> I've chosen the simpler way, I just create clang nodes from scratch  
> and replace / insert these nodes with the old one in the syntax  
> tree!  At the end it's not so complex as I thought... but now I have  
> another problem! I want to write back the modified syntax tree but  
> no matter which kind of changes I made to the syntax tree... when I  
> use the TokenRewriter:
>
> const LangOptions &LangOpts = Ctx.getLangOptions();
> TokenRewriter Rewriter(Ctx.getSourceManager().getMainFileID(),  
> Ctx.getSourceManager(), LangOpts);
>
> // Print out the output.
> for (TokenRewriter::token_iterator I = Rewriter.token_begin(), E =  
> Rewriter.token_end(); I != E; ++I)
>       out << pp.getSpelling(*I);
>
> I still get the original code!

As I understand it, the TokenRewriter is only meant to be used to  
insert tokens into the token stream as it's being rewritten. It's not  
designed for AST-level rewrites.

> How can I rewrite a syntax tree to Source file? Is there something  
> in the API that I am missing? Should I use the Rewriter? If yes, why  
> the rewriter doesn't provide an InsertStmt method? With the current  
> API I can add text... and replace statements... what about inserting  
> statements? and... remove statements?

I believe the Rewriter can do what you want, but it will take some  
effort. The rewriter allows you to insert/remove/replace text in the  
source file with any other text, using SourceLocations to specify  
where in the source file the modifications will be made. That's one  
approach to AST-based rewriting, since one can turn a statement/ 
expression/declaration into a string and then insert it at the  
appropriate place in the source.

Clang has some functionality for pretty-printing statements and  
expressions as source code, but we haven't put a lot of effort into  
making sure that the result is well-formed C++. Depending on how  
complicated your statements and expressions are, this might take no  
work or it might take a lot of work.

	- Doug



More information about the cfe-dev mailing list