[cfe-dev] Chaining ASTConsumers

Douglas Gregor dgregor at apple.com
Mon May 10 11:22:35 PDT 2010


On May 7, 2010, at 6:22 AM, Mohamed Yousef wrote:

> Hello ,
> 
> I'm writing a source to source tool, according to list prefred way is Rewriter class , but also i can modify list by a series of traversals
> 
> the problem is Rewriter class method is really primitive,, isn't suitable for big changes (esp. in structure )

Right.

> what i'm asking for is, can i chain ASTConsumer's ,,so i can progressively modify AST and pretty-print it finally
> but it looks like (from ParseAST) i would have to Parser -> ASTConsumer ->  pretty-print ->Parser ->ASTConsumer -> .. and so on
> which is very tedious

The AST pretty-print isn't guaranteed to produce output that can be parsed. It would be wonderful if we could get there, but it's going to take some serious work to get it right. 

> is the there an explicit AST data structure ? can i just parse once and modify resulting AST many times  (traverse,add/delete nodes it not re-parse source) ?

The Clang ASTs are explicit data structures representing the entire translation unit. Once constructed, ASTs are essentially immutable, so you won't be able to add/delete nodes easily. You might be able to write an AST -> AST transformation that performs the modifications on the source AST as it builds the resulting AST (perhaps even building up a chain of such transformations).

	- Doug



More information about the cfe-dev mailing list