[cfe-dev] Exporting Clang AST

Manuel Klimek via cfe-dev cfe-dev at lists.llvm.org
Mon Sep 28 05:53:32 PDT 2015

On Sun, Sep 27, 2015 at 6:11 PM Bhargava Shastry via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hi,
> I am trying to export clang AST to a file, recreating the tree structure
> in a suitable format e.g., xml/json. I am using the RecursiveASTVisitor
> for this.

You can already save the AST in a way that you can read it back into clang,
but not in a way that is readable by a different tool.

So far the consensus has been that this would be a very hard project; there
has been an XML output once, but the problem is that it was never
up-to-date / complete enough to do anything useful with it.
Generally, if you want to have a full XML/JSON/whatever output, somebody
would need to implement it, and then make sure it doesn't get out of date
when the implementation changes.

So far we have build mostly clang-based tools that work directly on the C++

> Question: What is the invariant for establishing parent-child
> relationship between AST nodes? I see that one potential invariant is
> ``If two AST nodes have the same DeclContext pointer, then they are in
> the same scope i.e., siblings or parent-children.`` Is there a stronger
> invariant that establishes parent-child relationship? Sadly, the Decl
> class doesn't have a getParent() that returns its parent AST node.

ASTContext has a getParents() method. Note that you can get multiple
parents for a node (for example, non-type-dependent expressions often exist
only once if they are part of multiple template instantiations).

> Also, is there a generic way I could ask the same question for nodes of
> Stmt and Type classes?
> P.S. I tried to infer the invariant by skimming through ASTDumper code.
> But, ASTDumper invents custom traversal different from what RAV
> provides, so it's non-trivial to map logic from/to two different models.

Generally, AST traversal is non-trivial.

> Thanks,
> Bhargava
> --
> Bhargava Shastry <bshastry at sec.t-labs.tu-berlin.de>
> Security in Telecommunications
> TU Berlin / Telekom Innovation Laboratories
> Ernst-Reuter-Platz 7, Sekr TEL 17 / D - 10587 Berlin, Germany
> phone: +49 30 8353 58235
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://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/20150928/b1b7faaf/attachment.html>

More information about the cfe-dev mailing list