Hi!<div><br></div><div>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: </div>

<div><br></div><div>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. </div>

<div><br></div><div>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.</div><div><br></div><div>Thanks,</div>

<div><br></div><div>Jorge.</div><div><br><div class="gmail_quote">2012/5/3 Robert Ankeney <span dir="ltr"><<a href="mailto:rrankene@gmail.com" target="_blank">rrankene@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

>From your question:<br>
Hi!<br>
<br>
I'm working on a tool that do some source-to-source transformations of an<br>
OpenCL kernel mainly using the Rewriter class of the Clang C++ API.<br>
However, now I want to do some more complex transformations, and in order<br>
to do them I'd need to clone a ForStmt node of an AST and add its copy at<br>
the same level of the tree (i.e., I want to create a copy of the node and<br>
just after add it to the tree as a sibling node of the original ForStmt).<br>
<br>
I know that I can do it very simply using the Rewriter class, but the<br>
cloning is just the first step of a more complex transformation that can't<br>
be done properly if I just rewrite the loop: I want to do some auxiliary<br>
transformations in the cloned loop and I need to access to its different<br>
parts using the ForStmt functions (GetBody(), GetInc(), GetCond()...).<br>
<br>
In summary, and returning to the subject of this mail, is it possible to<br>
clone a node in an AST using the Clang C++ API?<br>
<br>
Thank you so much in advance,<br>
<br>
Jorge.<br>
<br>
------------------------------------------<br>
Can you not use Rewriter.InsertText() to copy the full For statement<br>
over the SourceRange of the For statement?  Then since you are at the<br>
start of the For, you still have access to the body to perform your<br>
transformations on the original body?<br>
It seems easier than trying to clone the AST node.<br>
<br>
For an example of using the Rewriter class, have a look at my tutorial<br>
CIrewriter.cpp at:<br>
<a href="https://github.com/loarabia/Clang-tutorial" target="_blank">https://github.com/loarabia/Clang-tutorial</a>.<br>
<br>
Another option is to have a look at the new LibTooling library as part<br>
of Clang 3.1.<br>
<br>
<br>
Best regards,<br>
Robert Ankeney<br>
</blockquote></div><br></div>