Source-to-Source transformations

Dear LLVM Members,

I have started using LLVM for my project two weeks ago and I would be grateful if you could give me some information for writing a source-to-source transformation pass. More specifically, I want to apply a source-level transformation is some functions of my input program in order to create work chunks. For example, given the following function:
       void foo() {

       /* some work */

I would like to modify it in the following:

    void foo() {

       int i = work_size();

       for ( int j = 0; j < i; j++ ) {

          /* some work */



So, the first thing I would like to know is how to create some local variables in a given function while the second is how to insert a for statement in the body of a function.

Thanks a lot in advance,

I'd look at clang's Rewriter class.


I'm writing a ASTProcessing lib with which you already could do a
transformation like yours in a hurry.
It is however not part of the clang project nor will it becomes one
since it depends on boost.
You should be able to check out the current stuff via anon SVN (look at
The interesting part is trunk/Scout/clangAddons (the other folders on
the Scout level are trash).
Note that this is still work in heavy progress. Also I'm of course eager
to get your remarks or even submissions.
(e.g. StmtCollector::tStmtTreeMirror::compareOneLevel is still broken
(probably by design)).

Best regards
Olaf Krzikalla