<div dir="ltr">Perhaps, you could do this with the RecursiveASTVisitor, by overriding the VisitCompoundStmt method. In the method you should be able to add the desired new AST nodes. But keep in mind that the Clang AST is designed to be immutable, so this classifies this approach as a hack, you have to be very careful. When you create a new node, you have to make sure that the node is wired in the existing tree properly. And that can have many corner cases and that is why ASTImporter and ASTReader are so complex.<div><br></div><div>Note, the ASTImporter is based on visitors as well, but it does not modify the visited AST, it copies the nodes from the source AST to the destination AST.</div><div><br></div><div>Hope this helps,</div><div>Gabor</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 25, 2020 at 6:58 AM samins KAlex via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>ASTImporter was a starting point. I realized rewritings at text level at particular position after some node was matched. Now I need to modify AST at tree (node) level:</div><div>- add some statements to function compound stmt, and after match new AST again</div><div>- perform another AST changing and so on and so on yet another.</div><div> </div><div>This way is more preferable, because I not need load source code many times.</div><div> </div><div>I have already found that it's possible:</div><div>1)   <a href="https://stackoverflow.com/questions/41550891/adding-nodes-to-clangs-ast" target="_blank">https://stackoverflow.com/questions/41550891/adding-nodes-to-clangs-ast</a></div><div>2)  <a href="http://clang-developers.42468.n3.nabble.com/Adding-nodes-to-Clang-s-AST-td4054800.html" target="_blank"> http://clang-developers.42468.n3.nabble.com/Adding-nodes-to-Clang-s-AST-td4054800.html </a></div><div>3) <a href="https://stackoverflow.com/questions/30451485/how-to-clone-or-create-an-ast-stmt-node-of-clang/30459339" target="_blank">https://stackoverflow.com/questions/30451485/how-to-clone-or-create-an-ast-stmt-node-of-clang/30459339</a></div><div><br>The similar thing was realized in project Scout <a href="https://tu-dresden.de/zih/forschung/projekte/scout/" target="_blank">https://tu-dresden.de/zih/forschung/projekte/scout/</a></div><div>or <a href="https://llvm.org/devmtg/2013-04/krzikalla-slides.pdf" target="_blank">https://llvm.org/devmtg/2013-04/krzikalla-slides.pdf</a></div><div> </div><div>Unfortunately these projects and tips are outdated. Maybe there is some general solution in modern (9-11 versions) llvm/clang.</div><div>In sum I need mechanism for development own source-to-source translator but changes must be in AST (not at textual) level<table><tbody><tr><td> </td></tr></tbody></table></div><div> </div>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>