[cfe-dev] Insertion one AST node into another at particular location

Gábor Márton via cfe-dev cfe-dev at lists.llvm.org
Mon Aug 24 23:58:13 PDT 2020

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.

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

Hope this helps,

On Tue, Aug 25, 2020 at 6:58 AM samins KAlex via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> 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:
> - add some statements to function compound stmt, and after match new AST
> again
> - perform another AST changing and so on and so on yet another.
> This way is more preferable, because I not need load source code many
> times.
> I have already found that it's possible:
> 1)
> https://stackoverflow.com/questions/41550891/adding-nodes-to-clangs-ast
> 2)
> http://clang-developers.42468.n3.nabble.com/Adding-nodes-to-Clang-s-AST-td4054800.html
> 3)
> https://stackoverflow.com/questions/30451485/how-to-clone-or-create-an-ast-stmt-node-of-clang/30459339
> The similar thing was realized in project Scout
> https://tu-dresden.de/zih/forschung/projekte/scout/
> or https://llvm.org/devmtg/2013-04/krzikalla-slides.pdf
> Unfortunately these projects and tips are outdated. Maybe there is some
> general solution in modern (9-11 versions) llvm/clang.
> In sum I need mechanism for development own source-to-source translator
> but changes must be in AST (not at textual) level
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200825/25342c7d/attachment.html>

More information about the cfe-dev mailing list