<div dir="ltr"><div class="gmail-post-text">
<p>I'm novice with clang libtooling, I try to learn with easy c++ code.
I try to parse/print typedef expression like below lines:</p>
<pre class="gmail-lang-cpp gmail-prettyprint gmail-prettyprinted"><code><span class="gmail-kwd">namespace</span><span class="gmail-pln"> DEBUG </span><span class="gmail-pun">{</span><span class="gmail-pln">
</span><span class="gmail-kwd">typedef</span><span class="gmail-pln"> </span><span class="gmail-kwd">void</span><span class="gmail-pun">(*</span><span class="gmail-typ">function_pointer_t</span><span class="gmail-pun">)(</span><span class="gmail-typ">int</span><span class="gmail-pun">&);</span><span class="gmail-pln">
</span><span class="gmail-kwd">typedef</span><span class="gmail-pln"> </span><span class="gmail-typ">int</span><span class="gmail-pln"> myInt</span><span class="gmail-pun">;</span><span class="gmail-pln">
</span><span class="gmail-pun">}</span></code></pre>
<p><strong>clang++ -Xclang -ast-dump -fsyntax-only</strong> output:</p>
<pre class="gmail-lang-cpp gmail-prettyprint gmail-prettyprinted"><code><span class="gmail-pun">`-</span><span class="gmail-typ">NamespaceDecl</span><span class="gmail-pln"> </span><span class="gmail-lit">0x3e4fe331a8</span><span class="gmail-pln"> </span><span class="gmail-pun"><</span><span class="gmail-pln">test</span><span class="gmail-pun">.</span><span class="gmail-pln">h</span><span class="gmail-pun">:</span><span class="gmail-lit">3</span><span class="gmail-pun">:</span><span class="gmail-lit">1</span><span class="gmail-pun">,</span><span class="gmail-pln"> line</span><span class="gmail-pun">:</span><span class="gmail-lit">7</span><span class="gmail-pun">:</span><span class="gmail-lit">1</span><span class="gmail-pun">></span><span class="gmail-pln"> line</span><span class="gmail-pun">:</span><span class="gmail-lit">3</span><span class="gmail-pun">:</span><span class="gmail-lit">11</span><span class="gmail-pln"> TESTS
</span><span class="gmail-pun">|-</span><span class="gmail-typ">TypedefDecl</span><span class="gmail-pln"> </span><span class="gmail-lit">0x3e4fe333b8</span><span class="gmail-pln"> </span><span class="gmail-pun"><</span><span class="gmail-pln">line</span><span class="gmail-pun">:</span><span class="gmail-lit">4</span><span class="gmail-pun">:</span><span class="gmail-lit">2</span><span class="gmail-pun">,</span><span class="gmail-pln"> col</span><span class="gmail-pun">:</span><span class="gmail-lit">42</span><span class="gmail-pun">></span><span class="gmail-pln"> col</span><span class="gmail-pun">:</span><span class="gmail-lit">18</span><span class="gmail-pln"> </span><span class="gmail-typ">function_pointer_t</span><span class="gmail-pln"> </span><span class="gmail-str">'double (*)(int &)'</span><span class="gmail-pln">
</span><span class="gmail-pun">|</span><span class="gmail-pln"> </span><span class="gmail-pun">`-</span><span class="gmail-typ">PointerType</span><span class="gmail-pln"> </span><span class="gmail-lit">0x3e4fe33350</span><span class="gmail-pln"> </span><span class="gmail-str">'double (*)(int &)'</span><span class="gmail-pln">
</span><span class="gmail-pun">|</span><span class="gmail-pln"> </span><span class="gmail-pun">`-</span><span class="gmail-typ">ParenType</span><span class="gmail-pln"> </span><span class="gmail-lit">0x3e4fe332f0</span><span class="gmail-pln"> </span><span class="gmail-str">'double (int &)'</span><span class="gmail-pln"> sugar
</span><span class="gmail-pun">|</span><span class="gmail-pln"> </span><span class="gmail-pun">`-</span><span class="gmail-typ">FunctionProtoType</span><span class="gmail-pln"> </span><span class="gmail-lit">0x3e4fe332b0</span><span class="gmail-pln"> </span><span class="gmail-str">'double (int &)'</span><span class="gmail-pln"> cdecl
</span><span class="gmail-pun">|</span><span class="gmail-pln"> </span><span class="gmail-pun">|-</span><span class="gmail-typ">BuiltinType</span><span class="gmail-pln"> </span><span class="gmail-lit">0x3e4fe32b60</span><span class="gmail-pln"> </span><span class="gmail-str">'double'</span><span class="gmail-pln">
</span><span class="gmail-pun">|</span><span class="gmail-pln"> </span><span class="gmail-pun">`-</span><span class="gmail-typ">LValueReferenceType</span><span class="gmail-pln"> </span><span class="gmail-lit">0x3e4fe33210</span><span class="gmail-pln"> </span><span class="gmail-str">'int &'</span><span class="gmail-pln">
</span><span class="gmail-pun">|</span><span class="gmail-pln"> </span><span class="gmail-pun">`-</span><span class="gmail-typ">BuiltinType</span><span class="gmail-pln"> </span><span class="gmail-lit">0x3e4fe32a40</span><span class="gmail-pln"> </span><span class="gmail-str">'int'</span><span class="gmail-pln">
</span><span class="gmail-pun">`-</span><span class="gmail-typ">TypedefDecl</span><span class="gmail-pln"> </span><span class="gmail-lit">0x3e4fe33420</span><span class="gmail-pln"> </span><span class="gmail-pun"><</span><span class="gmail-pln">line</span><span class="gmail-pun">:</span><span class="gmail-lit">5</span><span class="gmail-pun">:</span><span class="gmail-lit">2</span><span class="gmail-pun">,</span><span class="gmail-pln"> col</span><span class="gmail-pun">:</span><span class="gmail-lit">14</span><span class="gmail-pun">></span><span class="gmail-pln"> col</span><span class="gmail-pun">:</span><span class="gmail-lit">14</span><span class="gmail-pln"> myInt </span><span class="gmail-str">'int'</span><span class="gmail-pln">
</span><span class="gmail-pun">`-</span><span class="gmail-typ">BuiltinType</span><span class="gmail-pln"> </span><span class="gmail-lit">0x3e4fe32a40</span><span class="gmail-pln"> </span><span class="gmail-str">'int'</span></code></pre>
<p>To parse it I create a class that inherite from MatchFinder::MatchCallback and overload MatchFinder::MatchCallback::run : </p>
<pre class="gmail-lang-cpp gmail-prettyprint gmail-prettyprinted"><code><span class="gmail-pln"> </span><span class="gmail-kwd">class</span><span class="gmail-pln"> </span><span class="gmail-typ">TypdefDeclFinder</span><span class="gmail-pln"> </span><span class="gmail-pun">:</span><span class="gmail-pln"> </span><span class="gmail-kwd">public</span><span class="gmail-pln"> </span><span class="gmail-typ">MatchFinder</span><span class="gmail-pun">::</span><span class="gmail-typ">MatchCallback</span><span class="gmail-pln"> </span><span class="gmail-pun">{</span><span class="gmail-pln">
</span><span class="gmail-kwd">public</span><span class="gmail-pun">:</span><span class="gmail-pln">
</span><span class="gmail-kwd">virtual</span><span class="gmail-pln"> </span><span class="gmail-kwd">void</span><span class="gmail-pln"> run</span><span class="gmail-pun">(</span><span class="gmail-kwd">const</span><span class="gmail-pln"> </span><span class="gmail-typ">MatchFinder</span><span class="gmail-pun">::</span><span class="gmail-typ">MatchResult</span><span class="gmail-pun">&</span><span class="gmail-pln"> result</span><span class="gmail-pun">)</span><span class="gmail-pln">
</span><span class="gmail-pun">{</span><span class="gmail-pln">
</span><span class="gmail-kwd">auto</span><span class="gmail-pln"> </span><span class="gmail-typ">Item</span><span class="gmail-pln"> </span><span class="gmail-pun">=</span><span class="gmail-pln"> result</span><span class="gmail-pun">.</span><span class="gmail-typ">Nodes</span><span class="gmail-pun">.</span><span class="gmail-pln">getNodeAs</span><span class="gmail-pun"><</span><span class="gmail-pln">clang</span><span class="gmail-pun">::</span><span class="gmail-typ">TypedefDecl</span><span class="gmail-pun">>(</span><span class="gmail-str">"typedefDeclMatch"</span><span class="gmail-pun">);</span><span class="gmail-pln">
</span><span class="gmail-kwd">if</span><span class="gmail-pln"> </span><span class="gmail-pun">(!</span><span class="gmail-typ">Item</span><span class="gmail-pun">)</span><span class="gmail-pln"> </span><span class="gmail-kwd">return</span><span class="gmail-pun">;</span><span class="gmail-pln">
</span><span class="gmail-kwd">if</span><span class="gmail-pln"> </span><span class="gmail-pun">(!</span><span class="gmail-typ">IsDeclFromInputFiles</span><span class="gmail-pun">(</span><span class="gmail-typ">Item</span><span class="gmail-pun">,</span><span class="gmail-pln"> result</span><span class="gmail-pun">.</span><span class="gmail-typ">SourceManager</span><span class="gmail-pun">))</span><span class="gmail-pln"> </span><span class="gmail-kwd">return</span><span class="gmail-pun">;</span><span class="gmail-pln">
</span><span class="gmail-kwd">if</span><span class="gmail-pln"> </span><span class="gmail-pun">(!</span><span class="gmail-typ">Item</span><span class="gmail-pun">-></span><span class="gmail-pln">getIdentifier</span><span class="gmail-pun">())</span><span class="gmail-pln"> </span><span class="gmail-kwd">return</span><span class="gmail-pun">;</span><span class="gmail-pln">
</span><span class="gmail-kwd">if</span><span class="gmail-pln"> </span><span class="gmail-pun">(</span><span class="gmail-typ">IsInsideTemplateContext</span><span class="gmail-pun">(</span><span class="gmail-typ">Item</span><span class="gmail-pun">))</span><span class="gmail-pln"> </span><span class="gmail-kwd">return</span><span class="gmail-pun">;</span><span class="gmail-pln">
print</span><span class="gmail-pun">(</span><span class="gmail-typ">Item</span><span class="gmail-pun">);</span><span class="gmail-pln">
</span><span class="gmail-pun">}</span><span class="gmail-pln">
</span><span class="gmail-pun">};</span></code></pre>
<p>But <strong>Item</strong> pointer is equal to null for the first typedef. I can parse/print
function, variable, class, struct template, method templates, enum ...
with MacthFinder::MatchCallback but this way doesn't run on typedef.
What's wrong with this code?</p>
<p>thanks in advance for your help.</p>
</div></div>