<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=utf-8">
<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:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        color:black;}
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
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;
        color:black;}
code
        {mso-style-priority:99;
        font-family:"Courier New";}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";
        color:black;}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:"Courier New";}
span.EmailStyle21
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
span.EmailStyle22
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
span.apple-converted-space
        {mso-style-name:apple-converted-space;}
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 bgcolor="white" lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><span style="color:windowtext">Hi Aleksei, Tnx for the fast response<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:windowtext"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:windowtext">Another thing I can’t figure is how to integrate it with the AST-traversal Clang allows?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:windowtext">The way I use to modify AST-nodes is such:<o:p></o:p></span></p>
<pre style="mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt;background:#EFF0F1"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1">class MyASTConsumer : public ASTConsumer {<o:p></o:p></span></code></pre>
<pre style="mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt;background:#EFF0F1"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1">public:<o:p></o:p></span></code></pre>
<pre style="mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt;background:#EFF0F1"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1">  bool HandleTopLevelDecl(DeclGroupRef DG) override {<o:p></o:p></span></code></pre>
<pre style="mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt;background:#EFF0F1"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1">    for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) { … }<o:p></o:p></span></code></pre>
<pre style="mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt;background:#EFF0F1"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1">  }<o:p></o:p></span></code></pre>
<pre style="mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt;background:#EFF0F1"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1">}<o:p></o:p></span></code></pre>
<p class="MsoNormal"><span style="color:windowtext">AFAIU, I need to tell the new namespace to hold the FunctionDecl of foo(), and make the object that held foo()’s FunctionDecl to release it. How can I do it?<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><span style="color:windowtext">From:</span></b><span style="color:windowtext"> Aleksei Sidorin [mailto:a.sidorin@samsung.com]
<br>
<b>Sent:</b> Monday, January 09, 2017 6:55 PM<br>
<b>To:</b> Ori Zaig <oriz@mellanox.com>; cfe-dev@lists.llvm.org<br>
<b>Cc:</b> Eran Jakoby <eranj@mellanox.com><br>
<b>Subject:</b> Re: [cfe-dev] Adding nodes to Clang's AST<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">Hello Ori,<br>
<br>
You can create different kinds of nodes directly. For example, to create a new namespace, you will need to use code like this:<br>
<br>
IdentifierInfo *IdInfo = Context.Idents.get("bar");<br>
NamespaceDecl *NewNS = NamespaceDecl::Create(Context, Context.getTranslationUnitDecl(), false, StartLoc, IdLoc, IdInfo, OldDecl);<br>
<br>
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.<br>
To move a function to this namespace, you will need to set a new DeclContext for it:<br>
<br>
FD->setDeclContext(NewNS);<br>
FD->setLexicalDeclContext(NewNS);<br>
<br>
<br>
<br>
09.01.2017 18:59, Ori Zaig via cfe-dev пишет:<span style="font-size:12.0pt"><o:p></o:p></span></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white">
<span style="font-size:11.5pt;font-family:"Arial",sans-serif;color:#242729">I need to insert new nodes to AST. For instance, adding a namespace to a function: Turning this -</span><o:p></o:p></p>
<pre style="background:#EFF0F1;max-height: 600px;word-wrap: normal;font-variant-ligatures: normal;font-variant-caps: normal;orphans: 2;widows: 2;-webkit-text-stroke-width: 0px;overflow:auto;word-spacing:0px"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1">void foo();</span></code><o:p></o:p></pre>
<pre style="background:#EFF0F1"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1"> </span></code><o:p></o:p></pre>
<p style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;orphans: 2;widows:
          2;-webkit-text-stroke-width: 0px;word-spacing:0px">
<span style="font-size:11.5pt;font-family:"Arial",sans-serif;color:#242729">into this -</span><o:p></o:p></p>
<pre style="background:#EFF0F1;max-height: 600px;word-wrap: normal;font-variant-ligatures: normal;font-variant-caps: normal;orphans: 2;widows: 2;-webkit-text-stroke-width: 0px;overflow:auto;word-spacing:0px"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1">namespace bar {</span></code><o:p></o:p></pre>
<pre style="background:#EFF0F1"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1">    void foo();</span></code><o:p></o:p></pre>
<pre style="background:#EFF0F1"><code><span style="font-family:Consolas;color:#242729;border:none windowtext 1.0pt;padding:0in;background:#EFF0F1">}</span></code><o:p></o:p></pre>
<p style="margin:0in;margin-bottom:.0001pt;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;orphans: 2;widows:
          2;-webkit-text-stroke-width: 0px;word-spacing:0px">
<span style="font-size:11.5pt;font-family:"Arial",sans-serif;color:#242729"> </span><o:p></o:p></p>
<p style="margin:0in;margin-bottom:.0001pt;background:white"><span style="font-size:11.5pt;font-family:"Arial",sans-serif;color:#242729">I want to edit the AST directly - I prefer not using source-to-source compilation</span><o:p></o:p></p>
<p style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;orphans: 2;widows:
          2;-webkit-text-stroke-width: 0px;word-spacing:0px">
<span style="font-size:11.5pt;font-family:"Arial",sans-serif;color:#242729">Tnx</span><o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"><br>
<br>
<br>
<o:p></o:p></span></p>
<pre>_______________________________________________<o:p></o:p></pre>
<pre>cfe-dev mailing list<o:p></o:p></pre>
<pre><a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><o:p></o:p></pre>
<pre><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><o:p></o:p></pre>
</blockquote>
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"><o:p> </o:p></span></p>
<p><o:p> </o:p></p>
<pre>-- <o:p></o:p></pre>
<pre>Best regards,<o:p></o:p></pre>
<pre>Aleksei Sidorin<o:p></o:p></pre>
<pre>Software Engineer, <o:p></o:p></pre>
<pre>IMSWL-IMCG, SRR, Samsung Electronics<o:p></o:p></pre>
</div>
</body>
</html>