[cfe-dev] AST modifications that apply to the binary
Ori Zaig via cfe-dev
cfe-dev at lists.llvm.org
Sun Nov 20 08:49:08 PST 2016
Hi all
Is there any possible making AST modification that finally will be reflected in the binary?
I tried doing that via plugin, for instance changing a method name (let's say turn foo() to my_foo() ) such:
class FuncnameChangeVisitor : public RecursiveASTVisitor< FuncnameChangeVisitor > {
private:
ASTContext *m_context;
public:
/* some code */
bool VisitFunctionDecl(FunctionDecl *f) {
DeclarationNameInfo newNameInfo(f->getNameInfo());
DeclarationName origName(f->getDeclName());
std::string newName("my_");
newName += origName.getAsString();
IdentifierInfo& newNameIdInfo(m_context->Idents.get(StringRef(newName.c_str())));
f->setDeclName(DeclarationName(&newNameIdInfo));
return true;
}
};
class FuncnameChangeConsumer: public ASTConsumer {
private:
FuncnameChangeVisitor m_visitor;
public:
/* some code*/
virtual void HandleTranslationUnit(ASTContext &Cntx) {
m_visitor.TraverseDecl(Cntx.getTranslationUnitDecl());
}
};
class FuncnameChangeAction: public PluginASTAction {
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {
return llvm::make_unique< FuncnameChangeConsumer >(CI, &CI.getSourceManager());
}
bool ParseArgs(const CompilerInstance &CI, const std::vector<std::string> &args) override {
return true;
}
};
static FrontendPluginRegistry::Add< FuncnameChangeAction > reg("modify-func", "add my_ prefix to funtions");
But in vain... I understood that the IR and hence the binary are generated regardless the plugins
And if there isn't a way to do so, is there another way via Clang to make code-modifications that affect the binary, which are not source-to-source compilation?
Tnx
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20161120/72b70b55/attachment.html>
More information about the cfe-dev
mailing list