<div dir="auto">Hi David,</div><div dir="auto"><br></div><div dir="auto">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><br><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">davrecthreads@gmail.com</a>> wrote:<br></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"><br><div><br><blockquote type="cite"><div>On Mar 18, 2021, at 11:42 AM, Michael Chiu via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:</div><br><div>
<div text="#000000">
Hi All,<br>
  <br>
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">bool 
VistVarDecl(clang::VarDecl *vardecl)</code>. However, the <code style="font-family:monospace">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">clang-query</code> on <code style="font-family:monospace">test1.c</code>
 I <em>can</em> match <code style="font-family:monospace">varDecl</code>.<br>

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

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

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

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

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

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

    <span style="font-family:monospace">return</span> <span style="font-family:monospace">0</span>;
}
```
</code></pre>
My RecursiveASTVisitor is as follows:<br>
```<code style="font-family:monospace"><span style="font-family:monospace"><span style="font-family:monospace"><br>
struct</span> <span style="font-family:monospace">MyASTVisitor</span> :</span> <span style="font-family:monospace">public</span> 
clang::RecursiveASTVistor<MyASTVisitor> {</code><code style="font-family:monospace">
  </code><code style="font-family:monospace"></code>
  <pre style="font-family:monospace"><code style="font-family:monospace">  <span style="font-family:monospace"><span style="font-family:monospace">bool</span> <span style="font-family:monospace">VistVarDecl</span><span style="font-family:monospace">(clang::VarDecl *vardecl)</span> </span>{
     llvm::outs() << <span style="font-family:monospace">"Found a VarDecl"</span>;
</code></pre></div></div></blockquote></div><div><div>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"><div><div text="#000000"><pre style="font-family:monospace"><code style="font-family:monospace">  };

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

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

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

  <span style="font-family:monospace"><span style="font-family:monospace">bool</span> <span style="font-family:monospace">HandleTopLevelDecl</span><span style="font-family:monospace">(clang::DeclGroupRef DR)</span> <span style="font-family:monospace">override</span> </span>{
    <span style="font-family:monospace">for</span> (clang::DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
        Visitor.TraverseDecl(*b);
     }
     <span style="font-family:monospace">return</span> <span style="font-family:monospace">true</span>;
  }
<span style="font-family:monospace">private</span>: 
  MyASTVisitor Visitor;
};
```
</code></pre>
  <br>
  <div>-- <br>Michael.<br>

  </div>
</div>

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