[cfe-dev] AST modifications that apply to the binary
John Brawn via cfe-dev
cfe-dev at lists.llvm.org
Wed Nov 23 09:14:11 PST 2016
By default a PluginASTAction will replace the main AST action (if you use -plugin pluginname on the
cc1 command line) or go after the main AST action (if you use -add-plugin pluginname on the cc1
command line), where the "main AST action" is "generate an object file" if using -c, "generate
assembly" is using -S, etc.
If you want the PluginASTAction to go before the main AST action, i.e. you want it to change the
AST in a way that will affect the compiled output, then define a getActionType method in the
PluginASTAction which returns AddBeforeMainAction.
John
From: cfe-dev [mailto:cfe-dev-bounces at lists.llvm.org] On Behalf Of Ori Zaig via cfe-dev
Sent: 20 November 2016 16:49
To: cfe-dev at lists.llvm.org
Cc: Eran Jakoby
Subject: [cfe-dev] AST modifications that apply to the binary
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/20161123/5633d794/attachment.html>
More information about the cfe-dev
mailing list