[cfe-dev] cfe-dev Digest, Vol 59, Issue 11

Robert Ankeney rrankene at gmail.com
Thu May 3 09:10:24 PDT 2012

On Thu, May 3, 2012 at 7:38 AM,  <fabeirojorge at gmail.com> wrote:
> Hi!
> I'm working on a tool that do some source-to-source transformations of an
> OpenCL kernel mainly using the Rewriter class of the Clang C++ API.
> However, now I want to do some more complex transformations, and in order
> to do them I'd need to clone a ForStmt node of an AST and add its copy at
> the same level of the tree (i.e., I want to create a copy of the node and
> just after add it to the tree as a sibling node of the original ForStmt).
> I know that I can do it very simply using the Rewriter class, but the
> cloning is just the first step of a more complex transformation that can't
> be done properly if I just rewrite the loop: I want to do some auxiliary
> transformations in the cloned loop and I need to access to its different
> parts using the ForStmt functions (GetBody(), GetInc(), GetCond()...).
> In summary, and returning to the subject of this mail, is it possible to
> clone a node in an AST using the Clang C++ API?
> Thank you so much in advance,
> Jorge.

Can you not use Rewriter.InsertText() to copy the full For statement
over the SourceRange of the For statement?  Then since you are at the
start of the For, you still have access to the body to perform your
transformations on the original body?
It seems easier than trying to clone the AST node.

For an example of using the Rewriter class, have a look at my tutorial
CIrewriter.cpp at:

Another option is to have a look at the new LibTooling library as part
of Clang 3.1.

Best regards,
Robert Ankeney

More information about the cfe-dev mailing list