manipulating AST with ASTConsumer

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

manipulating AST with ASTConsumer

Ilya Mirsky
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.

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.

I'll be grateful if someone could give me a concrete example how to do the above operation.


Thanks in advance,
Ilya
 
--
Best Regards,
Ilya Mirsky
www.cs.bgu.ac.il/~mirskyil


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: manipulating AST with ASTConsumer

Douglas Gregor

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

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev