[cfe-dev] how to use AST Consumer to modify loops in source code?

Douglas Gregor dgregor at apple.com
Mon Jan 17 09:50:16 PST 2011


On Jan 15, 2011, at 9:52 AM, jignesh vasoya wrote:

> Hi,
> i want to change all the loops in the source code.
> how can i do it with ASTConsumer class?
> 
> i want to visit each loop and after making some changes to its body and declaration i want to write it back to the AST.

The AST can't be safely modified in this way. It's effectively immutable once it has been created.

> if writing back to AST is not possible using plugin method for clang than at least i want to extract each loop in the source code.
> 
> i have gone through the plugin code given in the  clang/examples.........


You can write an ASTConsumer that identifies each of the loops, then perform source-to-source translation on them (as in, e.g., the Objective-C rewriter in RewriteObjC.cpp) . More involved loop transforms at the AST level would require more invasive changes to Clang.

What exactly are you trying to do?

	- Doug



More information about the cfe-dev mailing list