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

Ori Zaig via cfe-dev cfe-dev at lists.llvm.org
Tue Jan 10 10:27:17 PST 2017


Hi

It worked as a charm! Tnx

I use it with a plugin:

class EditAPIsConsumer : public ASTConsumer {

public:

  EditAPIsConsumer(CompilerInstance &ci) : m_ci(ci) {}



  bool HandleTopLevelDecl(DeclGroupRef DG) override {

           static ASTContext& ctx = m_ci.getASTContext();

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

                   if ((*I)->getKind() == Decl::Function) {

                            FunctionDecl* f = static_cast<FunctionDecl*>(*I);

                            DeclarationName origName(f->getDeclName());

                            if (origName.getAsString() != "main") {

                                     static IdentifierInfo& venusNamespaceIdInfo(ctx.Idents.get(StringRef("bar")));

                                     NamespaceDecl *newNamespace = NamespaceDecl::Create(ctx, ctx.getTranslationUnitDecl(), false, f->getLocStart(), f->getLocStart(), &venusNamespaceIdInfo, nullptr);

                                     f->setDeclContext(newNamespace);

                                     f->setLexicalDeclContext(newNamespace);

                            }

                   }

           }

           return true;

  }



private:

  CompilerInstance& m_ci;

};





class EditAPIsAction : public PluginASTAction {

protected:

  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {

           return llvm::make_unique<EditAPIsConsumer>(CI);

  }



  virtual ActionType getActionType() override { return AddBeforeMainAction; }



  bool ParseArgs(const CompilerInstance &CI, const std::vector<std::string> &args) override {

           return true;

  }

};



static FrontendPluginRegistry::Add<EditAPIsAction>

__reg("dump-apis", "creates declarative APIs");


And I compiled foo() with it:
some_code.h:


int foo();

some_code.cpp:


int foo() {return 42;}

And now foo()’s symbol is:
[oriz@ llvm-plugin git:master]$ nm some_code.o
0000000000000000 T _ZN3bar3fooEv


Now, I get another problem – I can’t the modified foo() in a test-
I got this test:


#include "some_code.h"

#include <iostream>



int main(int argc, char* argv[]) {

  std::cout << bar::foo() << std::endl;



  return 0;

}

And I build it with the same plugin above
I got this error:
./test/main.cpp:23:15: error: use of undeclared identifier 'bar'
        std::cout << bar::foo() << std::endl;

I debugged it and it seems that the error is invoked before the fronted executes the action

What can I do about 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/20170110/766de01d/attachment.html>


More information about the cfe-dev mailing list