[cfe-dev] Is it possible to clone a node in an AST?

Jorge Fernández Fabeiro fabeirojorge at gmail.com
Thu May 3 09:29:48 PDT 2012


First, thank you so much again for your answer. I know that I can use the
Rewriter class in order to do that kind of transformations (I'm using it
for other operations, indeed), but probably I didn't explained myself very
well what I really want to do, so I'll try to explain my problem from a
different point of view:

I need to transform a given piece of code (for example, unroll a loop body
rewriting adequate additional sentences just under the original -my tool
does this thing right now using the Rewrite class-), but, according to the
code optimization I want to implement, once this piece of code had been
transformed, I need to do a second transformation with it, and for this I'd
need to have an updated version of the AST in order to use the methods of
the different classes that inherit from Stmt to access the multiple parts
of any sentence.

Anyway, I'd like to know how clone nodes from an AST (o adding new ones) in
order to do in a future more complex transformations than those I'm
describing now.



2012/5/3 Robert Ankeney <rrankene at gmail.com>

> From your question:
> 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:
> https://github.com/loarabia/Clang-tutorial.
> Another option is to have a look at the new LibTooling library as part
> of Clang 3.1.
> Best regards,
> Robert Ankeney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120503/d2c04bcb/attachment.html>

More information about the cfe-dev mailing list