You'd use the Rewriter to modify the source buffers, and then use that information to overlay the files and re-parse the translation unit.<br><br><div class="gmail_quote">On Mon Dec 22 2014 at 7:27:26 PM Xiaohui Chen <<a href="mailto:xchen422@uwo.ca">xchen422@uwo.ca</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Citing from the link below:<br><br>" It is pretty much the same as editing the AST directly. You can do all the
source editing in memory in a single tool, *and* do the compile step on the
changed source from the same tool; no need to write anything to disk (in
fact, the rewriting tools all have the source in memory before they edit
the files on disk).
"<br><br>i think this is what i want. but i can not find any related APIs to do this job. Could any one point this out for me?<br><br><br>To present my question again:<br>i already have the AST for the following code:<br>{<br> ....... // A<br> ....... // B<br>}<br><br>For instance, i intend to insert Decl "int var"(say C) between A and B, i want to rebuild the AST, so <br>i can get a new AST presenting the following code:<br><br>{<br> ....... // A<br> int var; // C<br> ....... // B<br>}<br><br>i will do other transformation based on this new AST.<br><br>Sincerely<br>hui<br><br><span>On 12/22/14, <b>mobi phil </b> <<a href="mailto:mobi@mobiphil.com" target="_blank">mobi@mobiphil.com</a>> wrote:</span><blockquote style="border-left:1px solid #00f;padding-left:13px;margin-left:0" type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi,<br>thanks for your reply.<br>since the AST is immutable, what did you mean by saying "rebuild the AST with the rewritten code "?<br><br>do you mean i should save the modified code to a ".c" file and re-parse it again?<br><br>or there are some APIs i can use to insert a node into the AST without re-parse the file?<div><div><br></div></div></blockquote><div><br></div><div>did not try it myself but according to Manuel (link below), you do not need to save the file to disk, you can re-compile (thus re-build the AST) from the buffer where you applied the rewrites</div><div><br></div><div>check this one <a href="http://lists.cs.uiuc.edu/pipermail/cfe-dev/2014-December/040616.html" target="_blank">http://lists.cs.uiuc.edu/pipermail/cfe-dev/2014-December/040616.html</a></div><div><br></div><div><br></div><div><br></div></div>
</div></div>
</div></blockquote>
</blockquote></div>