<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">You spell it `VistVarDecl` in the example, t<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Mar 18, 2021, at 12:02 PM, Michael Chiu <<a href="mailto:chiumichel@gmail.com" class="">chiumichel@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="auto" class="">Hi David,</div><div dir="auto" class=""><br class=""></div><div dir="auto" class="">Thanks for the prompt reply. I should’ve been more clear but I am returning bool in all of the visitor method functions, but clang::VarDecl is still not visited which is why I’m so perplexed.</div><div class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Mar 18, 2021 at 8:58 AM David Rector <<a href="mailto:davrecthreads@gmail.com" class="">davrecthreads@gmail.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div style="word-wrap:break-word;line-break:after-white-space" class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Mar 18, 2021, at 11:42 AM, Michael Chiu via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class=""><div class="">
<div text="#000000" class="">
Hi All,<br class="">
  <br class="">
I have  c code, below, that I'm trying to visit the AST of by using 
RecursiveASTVisitor. I'm trying to get the RecursiveASTVisitor to visit 
variable declarations (clang::VarDecl) by implementing <code style="font-family:monospace" class="">bool 
VistVarDecl(clang::VarDecl *vardecl)</code>. However, the <code style="font-family:monospace" class="">clang::VarDecl</code>
 nodes are never visited even though I've managed to visit all the other
 nodes in the AST though. Moreover, using <code style="font-family:monospace" class="">clang-query</code> on <code style="font-family:monospace" class="">test1.c</code>
 I <em class="">can</em> match <code style="font-family:monospace" class="">varDecl</code>.<br class="">

  <br class="">
Does anyone know the <code style="font-family:monospace" class="">clang::VarDecl</code> nodes are the only 
nodes that aren't visited by the <code style="font-family:monospace" class="">RecursiveASTVisitor</code> but 
are matched by <code style="font-family:monospace" class="">clang-query</code>?<br class="">
  <br class="">
Thanks in advaince!<br class="">
  <br class="">
```<code style="font-family:monospace" class=""><span style="font-family:monospace" class=""><span style="font-family:monospace" class=""><br class="">
double</span> <span style="font-family:monospace" class="">multiply</span><span style="font-family:monospace" class="">(<span style="font-family:monospace" class="">double</span> x, <span style="font-family:monospace" class="">double</span> y)</span> </span>{
  </code>
  <pre style="font-family:monospace" class=""><code style="font-family:monospace" class="">    <span style="font-family:monospace" class="">return</span> x * y * y;
}

<span style="font-family:monospace" class=""><span style="font-family:monospace" class="">int</span> <span style="font-family:monospace" class="">main</span><span style="font-family:monospace" class="">(<span style="font-family:monospace" class="">int</span> argc, <span style="font-family:monospace" class="">char</span> <span style="font-family:monospace" class="">const</span> *argv[])</span>
</span>{
    <span style="font-family:monospace" class="">double</span> a;

    <span style="font-family:monospace" class="">int</span> b;

    <span style="font-family:monospace" class="">float</span> d;

    <span style="font-family:monospace" class="">double</span> x = <span style="font-family:monospace" class="">3.0</span>;
    <span style="font-family:monospace" class="">double</span> y = <span style="font-family:monospace" class="">5.0</span>;

    <span style="font-family:monospace" class="">double</span> z = multiply(x,y);

    <span style="font-family:monospace" class="">return</span> <span style="font-family:monospace" class="">0</span>;
}
```
</code></pre>
My RecursiveASTVisitor is as follows:<br class="">
```<code style="font-family:monospace" class=""><span style="font-family:monospace" class=""><span style="font-family:monospace" class=""><br class="">
struct</span> <span style="font-family:monospace" class="">MyASTVisitor</span> :</span> <span style="font-family:monospace" class="">public</span> 
clang::RecursiveASTVistor<MyASTVisitor> {</code><code style="font-family:monospace" class="">
  </code><code style="font-family:monospace" class=""></code>
  <pre style="font-family:monospace" class=""><code style="font-family:monospace" class="">  <span style="font-family:monospace" class=""><span style="font-family:monospace" class="">bool</span> <span style="font-family:monospace" class="">VistVarDecl</span><span style="font-family:monospace" class="">(clang::VarDecl *vardecl)</span> </span>{
</code></pre></div></div></blockquote></div></div></blockquote></div></div></div></blockquote>If this misspelling of VisitVarDecl is in your code that’s your culprit.<br class=""><blockquote type="cite" class=""><div class=""><div class=""><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><blockquote type="cite" class=""><div class=""><div text="#000000" class=""><pre style="font-family:monospace" class=""><code style="font-family:monospace" class="">     llvm::outs() << <span style="font-family:monospace" class="">"Found a VarDecl"</span>;
</code></pre></div></div></blockquote></div><div class=""><div class="">Add `return true;’ here and in the others.  (Traversal halts after the first false return, and all these are returning false by default.)</div><blockquote type="cite" class=""><div class=""><div text="#000000" class=""><pre style="font-family:monospace" class=""><code style="font-family:monospace" class="">  };

  <span style="font-family:monospace" class=""><span style="font-family:monospace" class="">bool</span> <span style="font-family:monospace" class="">VisitFunctionDecl</span><span style="font-family:monospace" class="">(clang::FunctionDecl *decl)</span> </span>{
     llvm::outs() << <span style="font-family:monospace" class="">"Found a FunctionDecl"</span>;
  };

  <span style="font-family:monospace" class="">// other functions implemented similarly just to see if it visits properly</span>
  <span style="font-family:monospace" class=""><span style="font-family:monospace" class="">bool</span> <span style="font-family:monospace" class="">VisitParmVarmDecl</span><span style="font-family:monospace" class="">(clang::ParmVarDecl *paramvardecl)</span></span>;
  <span style="font-family:monospace" class=""><span style="font-family:monospace" class="">bool</span> <span style="font-family:monospace" class="">VisitCallExpr</span><span style="font-family:monospace" class="">(clang::CallExpr *callexpr)</span></span>;
  <span style="font-family:monospace" class=""><span style="font-family:monospace" class="">bool</span> <span style="font-family:monospace" class="">VisitImplicitCastExpr</span><span style="font-family:monospace" class="">(clang::ImplicitCastExpr *castexpr)</span></span>;
  <span style="font-family:monospace" class=""><span style="font-family:monospace" class="">bool</span> <span style="font-family:monospace" class="">VisitBinaryOperator</span><span style="font-family:monospace" class="">(clang::BinaryOperator *bo)</span></span>;
  <span style="font-family:monospace" class=""><span style="font-family:monospace" class="">bool</span> <span style="font-family:monospace" class="">VisitDeclStmt</span><span style="font-family:monospace" class="">(clang::DeclStmt *declstmt)</span></span>;
  <span style="font-family:monospace" class=""><span style="font-family:monospace" class="">bool</span> <span style="font-family:monospace" class="">VisitDeclRefExpr</span><span style="font-family:monospace" class="">(clang::DeclRefExpr *declrefexpr)</span></span>;
  <span style="font-family:monospace" class=""><span style="font-family:monospace" class="">bool</span> <span style="font-family:monospace" class="">VisitFloatingLiteral</span><span style="font-family:monospace" class="">(clang::FloatingLiteral *floatliteral)</span></span>;
};

<span style="font-family:monospace" class=""><span style="font-family:monospace" class="">struct</span> <span style="font-family:monospace" class="">MyASTConsumer</span> :</span> <span style="font-family:monospace" class="">public</span> clang::ASTConsumer {

  <span style="font-family:monospace" class=""><span style="font-family:monospace" class="">bool</span> <span style="font-family:monospace" class="">HandleTopLevelDecl</span><span style="font-family:monospace" class="">(clang::DeclGroupRef DR)</span> <span style="font-family:monospace" class="">override</span> </span>{
    <span style="font-family:monospace" class="">for</span> (clang::DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
        Visitor.TraverseDecl(*b);
     }
     <span style="font-family:monospace" class="">return</span> <span style="font-family:monospace" class="">true</span>;
  }
</code></pre></div></div></blockquote></div></div></blockquote></div></div></div></blockquote><div>Also, in your case, since you’re always returning true from your Handle* implem, might be simpler to instead write:</div><div>```</div><div><font face="Menlo" class="">void HandleTranslationUnit(ASTContext &Context) override {<br class="">  Visitor.TraverseDecl(Context.getTranslationUnitDecl());<br class="">}</font></div><div>```</div><div>Though that should not make a difference with your problem.</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div class=""><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><blockquote type="cite" class=""><div class=""><div text="#000000" class=""><pre style="font-family:monospace" class=""><code style="font-family:monospace" class=""><span style="font-family:monospace" class="">private</span>: 
  MyASTVisitor Visitor;
};
```
</code></pre>
  <br class="">
  <div class="">-- <br class="">Michael.<br class="">

  </div>
</div>

_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a><br class=""><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank" class="">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br class=""></div></blockquote></div><br class=""></div></blockquote></div></div>-- <br class=""><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">M.</div>
</div></blockquote></div><br class=""></body></html>