[cfe-dev] manipulating AST with ASTConsumer

Douglas Gregor dgregor at apple.com
Wed Aug 11 07:45:00 PDT 2010


On Aug 11, 2010, at 7:17 AM, Ilya Mirsky wrote:

> Hi all,
> 
> Not sure if it's the right place for this kind of questions, if not- please refer me to the right place.
> I want to use clang to manipulate AST of a c++ source, and write back the modified source to an output file.
> I'm trying to get familiar with clang and llvm for 2 weeks now, but having problems since all tutorials I found are outdated,
> and so is the API.

The API documentation is generated nightly; it's only out-of-date if the comments in the source are out-of-date, and if that's the case... please tell us!

> I've managed to run some tutorial which changes function names thru ASTConsumer implementation,
> which calls the Visit method of DeclVisitor class, and the manipultion is done in the VisitFunctionDecl method.
> Basically I'm able retrieve pretty much all I need from the AST.
> 
> My problem is modifying the AST:
> 1. Alter function signutures (type and params), 
> 2. Adding a statement or a declaration to a block (both global and in function scope). 
> i.e. a function or variable declaration, a conditional statement.

The AST is not designed to be mutated. For source-to-source transformations, we recommend using the rewriting infrastructure to produce rewritten source for just those parts of the AST that are changing. See lib/Rewrite/RewriterObjc.cpp for an example of a non-trivial source-to-source translator.

	- Doug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20100811/8b347b59/attachment.html>


More information about the cfe-dev mailing list