Adding nodes to Clang's AST

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

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 пишет:

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:

Hi

It worked as a charm! Tnx

I use it with a plugin: