<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 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        text-align:right;
        direction:rtl;
        unicode-bidi:embed;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 90.0pt 72.0pt 90.0pt;}
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"><div>Hi Tehila,</div><div><br></div><div>I recently added new function attributes to clang/llvm with arguments.</div><div><br></div><div>What I did is the following:</div><div><br></div><div>1. Add your attributes to tools/clang/include/clang/Basic/Attr.td</div><div><br></div><div><font face="monospace">def MYATTR : InheritableAttr {</font></div><div><font face="monospace">  let Spellings = [GNU<"myattr">, CXX11<"gnu", "myattr">];</font></div><div><font face="monospace">  let Args = [DefaultIntArgument<"myargument", 5>];</font></div><div><font face="monospace">  let Subjects = SubjectList<[Function]>;</font></div><div><font face="monospace">  let Documentation = [Undocumented];</font></div><div><font face="monospace">}</font></div><div><br></div><div>2. Manage it in tools/clang/lib/CodeGen/CGCall.cpp...</div><div><br></div><div><font face="monospace">void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,</font></div><div><font face="monospace">                                           const Decl *TargetDecl,</font></div><div><font face="monospace">                                           AttributeListType &PAL,</font></div><div><font face="monospace">                                           unsigned &CallingConv,</font></div><div><font face="monospace">                                           bool AttrOnCallSite) {</font></div><div><font face="monospace">//other code...</font></div><div><font face="monospace">if (TargetDecl) {</font></div><div><font face="monospace">if (TargetDecl->hasAttr<MYEXAMPLEAttr>())</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">       </span>{</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">    </span>auto myexample = TargetDecl->getAttr<MYEXAMPLEAttr>();</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">  </span><span class="Apple-tab-span" style="white-space:pre">    </span>FuncAttrs.addAttribute("myexample");</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">   </span><span class="Apple-tab-span" style="white-space:pre">    </span>unsigned myargument = myexample->getMyargument();</font></div><div><font face="monospace"><br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">      </span><span class="Apple-tab-span" style="white-space:pre">    </span>FuncAttrs.addAttribute("myargument", llvm::utostr(myargument));</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div><font face="monospace">//other code...</font></div><div><font face="monospace">}</font></div><div><font face="monospace">//other code...</font></div><div><font face="monospace">}</font></div><div><br></div><div><br></div><div>3. ...and in tools/clang/lib/Sema/SemaDeclAttr.cpp</div><div><br></div><div><font face="monospace">static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,</font></div><div><font face="monospace">                                 const AttributeList &Attr,</font></div><div><font face="monospace">                                 bool IncludeCXX11Attributes) {</font></div><div><font face="monospace">//other code...</font></div><div><font face="monospace">switch (Attr.getKind()) {</font></div><div><font face="monospace">case AttributeList::AT_MYEXAMPLE:</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre"> </span>handleMYEXAMPLEAttr(S, D, Attr);</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre"> </span>break;</font></div><div><font face="monospace">//other code...</font></div><div><font face="monospace">}</font></div><div><font face="monospace"><br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">static void handleMYEXAMPLEAttr(Sema &S, Decl *D, const AttributeList &Attr) {</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">     </span>uint32_t myargument = AVMAttr::DefaultMyargument;</font></div><div><font face="monospace"><br></font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">   </span>if (Attr.getNumArgs() && !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), myargument))</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">   </span><span class="Apple-tab-span" style="white-space:pre">    </span>return;</font></div><div><font face="monospace"><br></font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">     </span>D->addAttr(::new (S.Context)</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">  </span><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span>AVMAttr(Attr.getRange(), S.Context, myargument,</font></div><div><font face="monospace"><span class="Apple-tab-span" style="white-space:pre">     </span><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span><span class="Apple-tab-span" style="white-space:pre">    </span>Attr.getAttributeSpellingListIndex()));</font></div><div><font face="monospace">}</font></div><div><br></div><div>Then you should have access to it in LLVM IR.</div><div><br></div><div>I may have forgotten some stuff but it should help you get started.</div><div><br></div><div>Regards,</div><div>Gael</div><div><br></div><div>Le jeudi 08 octobre 2015 à 10:02 +0300, Tehila Mayzels via llvm-dev a écrit :</div><blockquote type="cite"><div class="WordSection1"><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed">Hi,<o:p></o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed"><o:p> </o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed">I'm trying to add a function attribute to clang/llvm.<o:p></o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed">I need an attribute that will have an additional information of a variable name.<o:p></o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed">e.g. I would like to add support for something like:<o:p></o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed"><o:p> </o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed">void foo() __attribute__ ((xyz(v)) {…}<o:p></o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed"><o:p> </o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed">such that on the IR stage I can realize that the function foo has the attribute xyz and an argument v that  marks variable v (which is a variable inside foo) for my needs.<o:p></o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed"><o:p> </o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed">I was able to add a function attribute without an argument, but I don't understand how to parse/save/analyze the argument.<o:p></o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed"><o:p> </o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed"><span style="font-size:11.5pt;background:#FCFAF3">I would appreciate any ideas</span>/examples/references.<o:p></o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed"><o:p> </o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed">Thanks a lot,<o:p></o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed">Tehila. <o:p></o:p></p><p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed"><o:p> </o:p></p><p class="MsoNormal" dir="RTL"><span dir="LTR"><o:p> </o:p></span></p></div><pre>_______________________________________________
LLVM Developers mailing list
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre></blockquote><div><br></div></body></html>