[cfe-dev] Adding nodes to Clang's AST

Ori Zaig via cfe-dev cfe-dev at lists.llvm.org
Mon Jan 9 09:30:13 PST 2017


Hi Aleksei, Tnx for the fast response

Another thing I can’t figure is how to integrate it with the AST-traversal Clang allows?
The way I use to modify AST-nodes is such:

class MyASTConsumer : public ASTConsumer {

public:

  bool HandleTopLevelDecl(DeclGroupRef DG) override {

    for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) { … }

  }

}
AFAIU, I need to tell the new namespace to hold the FunctionDecl of foo(), and make the object that held foo()’s FunctionDecl to release it. How can I do it?

From: Aleksei Sidorin [mailto:a.sidorin at samsung.com]
Sent: Monday, January 09, 2017 6:55 PM
To: Ori Zaig <oriz at mellanox.com>; cfe-dev at lists.llvm.org
Cc: Eran Jakoby <eranj at mellanox.com>
Subject: Re: [cfe-dev] Adding nodes to Clang's AST

Hello Ori,

You can create different kinds of nodes directly. For example, to create a new namespace, you will need to use code like this:

IdentifierInfo *IdInfo = Context.Idents.get("bar");
NamespaceDecl *NewNS = NamespaceDecl::Create(Context, Context.getTranslationUnitDecl(), false, StartLoc, IdLoc, IdInfo, OldDecl);

You can take a look at constructors of different Decl kinds (and static ::Create() methods) in Decl.h and DeclCXX.h (and other Decl*.h) for more information.
To move a function to this namespace, you will need to set a new DeclContext for it:

FD->setDeclContext(NewNS);
FD->setLexicalDeclContext(NewNS);



09.01.2017 18:59, Ori Zaig via cfe-dev пишет:

I need to insert new nodes to AST. For instance, adding a namespace to a function: Turning this -

void foo();



into this -

namespace bar {

    void foo();

}



I want to edit the AST directly - I prefer not using source-to-source compilation

Tnx





_______________________________________________

cfe-dev mailing list

cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>

http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev




--

Best regards,

Aleksei Sidorin

Software Engineer,

IMSWL-IMCG, SRR, Samsung Electronics
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170109/71e0dcb0/attachment.html>


More information about the cfe-dev mailing list