<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
{mso-style-priority:99;
mso-style-link:"Balloon Text Char";
margin:0in;
margin-bottom:.0001pt;
font-size:8.0pt;
font-family:"Tahoma",sans-serif;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{mso-style-priority:34;
margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.BalloonTextChar
{mso-style-name:"Balloon Text Char";
mso-style-priority:99;
mso-style-link:"Balloon Text";
font-family:"Tahoma",sans-serif;}
span.EmailStyle20
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.EmailStyle21
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:#1F497D;}
span.EmailStyle22
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:#1F497D;}
span.EmailStyle23
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:#1F497D;}
span.EmailStyle24
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><span style="color:#1F497D">Working like a charm…<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">Thanks a lot!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> John Brawn [mailto:John.Brawn@arm.com] <br>
<b>Sent:</b> Thursday, December 01, 2016 8:07 PM<br>
<b>To:</b> Ori Zaig <oriz@mellanox.com><br>
<b>Cc:</b> Eran Jakoby <eranj@mellanox.com>; nd <nd@arm.com>; cfe-dev@lists.llvm.org<br>
<b>Subject:</b> RE: AST modifications that apply to the binary<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">What’s happening here is some slightly non-obvious behaviour in how ASTConsumers are used.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">In brief:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">* Adding a PluginASTAction means we get a MultiplexConsumer whose list of consumers<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"> is FuncnameChangeAction then the main action<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">* This means that for each of the HandleXXX functions it calls the HandleXXX function on the<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"> FuncnameChangeAction then the main action<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">* clang::parseAST is where it ultimately ends up using the MultiplexConsumer and there it<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"> calls HandleTopLevelDecl on each DeclGroup
<i>then</i> calls HandleTranslationUnit<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">* You’ve done your transformation in HandleTranslationUnit, but CodeGenAction generates code<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"> in HandleTopLevelDecl, so though your HandleTranslationUnit is called before the CodeGenAction’s<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"> HandleTranslationUnit it doesn’t matter because the code has already been generated.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">* Doing the transformation in HandleTopLevelDecl works<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">(I myself didn’t realise that this would happen, I only figured it out by running clang in a debugger,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">setting breakpoints at various places and trying to figure out what was going on.)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">John<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma",sans-serif">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma",sans-serif"> Ori Zaig [<a href="mailto:oriz@mellanox.com">mailto:oriz@mellanox.com</a>]
<br>
<b>Sent:</b> 28 November 2016 12:58<br>
<b>To:</b> John Brawn<br>
<b>Cc:</b> Eran Jakoby; nd; <a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<b>Subject:</b> RE: AST modifications that apply to the binary<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">Unfortunately it doesn’t work - maybe I’m missing something…<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">I added the </span><span lang="EN-GB" style="color:#1F497D">getActionType method:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal">class FuncnameChangeAction: public PluginASTAction {<o:p></o:p></p>
<p class="MsoNormal"> std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {<o:p></o:p></p>
<p class="MsoNormal"> return llvm::make_unique< FuncnameChangeConsumer >(CI, &CI.getSourceManager());<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> bool ParseArgs(const CompilerInstance &CI, const std::vector<std::string> &args) override {<o:p></o:p></p>
<p class="MsoNormal"> return true;<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><b>virtual ActionType getActionType() override { return AddBeforeMainAction; }<o:p></o:p></b></p>
<p class="MsoNormal">};<o:p></o:p></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">And used the below command line:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">clang++ -Xclang -load -Xclang ModifyFunc.so -Xclang -add-plugin -Xclang modify-func -c some_code.cpp -o some_code.o<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">And yet, changes were not reflected in the binary (some_code.o) or even in IR<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">Any other ideas please?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> John Brawn [<a href="mailto:John.Brawn@arm.com">mailto:John.Brawn@arm.com</a>]
<br>
<b>Sent:</b> Wednesday, November 23, 2016 7:14 PM<br>
<b>To:</b> Ori Zaig <<a href="mailto:oriz@mellanox.com">oriz@mellanox.com</a>><br>
<b>Cc:</b> Eran Jakoby <<a href="mailto:eranj@mellanox.com">eranj@mellanox.com</a>>; nd <<a href="mailto:nd@arm.com">nd@arm.com</a>>;
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<b>Subject:</b> RE: AST modifications that apply to the binary<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">By default a PluginASTAction will replace the main AST action (if you use -plugin pluginname on the<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">cc1 command line) or go after the main AST action (if you use -add-plugin pluginname on the cc1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">command line), where the “main AST action” is “generate an object file” if using -c, “generate<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">assembly” is using -S, etc.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">If you want the PluginASTAction to go before the main AST action, i.e. you want it to change the<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">AST in a way that will affect the compiled output, then define a getActionType method in the<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">PluginASTAction which returns AddBeforeMainAction.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D">John<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="color:#1F497D"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma",sans-serif">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma",sans-serif"> cfe-dev [</span><a href="mailto:cfe-dev-bounces@lists.llvm.org"><span style="font-size:10.0pt;font-family:"Tahoma",sans-serif">mailto:cfe-dev-bounces@lists.llvm.org</span></a><span style="font-size:10.0pt;font-family:"Tahoma",sans-serif">]
<b>On Behalf Of </b>Ori Zaig via cfe-dev<br>
<b>Sent:</b> 20 November 2016 16:49<br>
<b>To:</b> </span><a href="mailto:cfe-dev@lists.llvm.org"><span style="font-size:10.0pt;font-family:"Tahoma",sans-serif">cfe-dev@lists.llvm.org</span></a><span style="font-size:10.0pt;font-family:"Tahoma",sans-serif"><br>
<b>Cc:</b> Eran Jakoby<br>
<b>Subject:</b> [cfe-dev] AST modifications that apply to the binary<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal">Hi all<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Is there any possible making AST modification that finally will be reflected in the binary?<o:p></o:p></p>
<p class="MsoNormal">I tried doing that via plugin, for instance changing a method name (let’s say turn foo() to my_foo() ) such:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">class FuncnameChangeVisitor : public RecursiveASTVisitor< FuncnameChangeVisitor > {<o:p></o:p></p>
<p class="MsoNormal">private:<o:p></o:p></p>
<p class="MsoNormal"> ASTContext *m_context;<o:p></o:p></p>
<p class="MsoNormal">public:<o:p></o:p></p>
<p class="MsoNormal"> /* some code */<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> bool VisitFunctionDecl(FunctionDecl *f) {<o:p></o:p></p>
<p class="MsoNormal"> DeclarationNameInfo newNameInfo(f->getNameInfo());<o:p></o:p></p>
<p class="MsoNormal"> DeclarationName origName(f->getDeclName());<o:p></o:p></p>
<p class="MsoNormal"> std::string newName("my_");<o:p></o:p></p>
<p class="MsoNormal"> newName += origName.getAsString();<o:p></o:p></p>
<p class="MsoNormal"> IdentifierInfo& newNameIdInfo(m_context->Idents.get(StringRef(newName.c_str())));<o:p></o:p></p>
<p class="MsoNormal"> f->setDeclName(DeclarationName(&newNameIdInfo));<o:p></o:p></p>
<p class="MsoNormal"> return true;<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal">};<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">class FuncnameChangeConsumer: public ASTConsumer {<o:p></o:p></p>
<p class="MsoNormal">private:<o:p></o:p></p>
<p class="MsoNormal"> FuncnameChangeVisitor m_visitor;<o:p></o:p></p>
<p class="MsoNormal">public:<o:p></o:p></p>
<p class="MsoNormal"> /* some code*/<o:p></o:p></p>
<p class="MsoNormal"> virtual void HandleTranslationUnit(ASTContext &Cntx) {<o:p></o:p></p>
<p class="MsoNormal"> m_visitor.TraverseDecl(Cntx.getTranslationUnitDecl());<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal">};<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">class FuncnameChangeAction: public PluginASTAction {<o:p></o:p></p>
<p class="MsoNormal"> std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {<o:p></o:p></p>
<p class="MsoNormal"> return llvm::make_unique< FuncnameChangeConsumer >(CI, &CI.getSourceManager());<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> bool ParseArgs(const CompilerInstance &CI, const std::vector<std::string> &args) override {<o:p></o:p></p>
<p class="MsoNormal"> return true;<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal">};<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">static FrontendPluginRegistry::Add< FuncnameChangeAction > reg("modify-func", "add my_ prefix to funtions");<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">But in vain… I understood that the IR and hence the binary are generated regardless the plugins<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Tnx<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</body>
</html>