<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=""><br class=""><div><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" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<meta http-equiv="content-type" content="text/html; charset=utf-8" 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 class="">bool 
VistVarDecl(clang::VarDecl *vardecl)</code>. However, the <code 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 class="">clang-query</code> on <code class="">test1.c</code>
 I <em class="">can</em> match <code class="">varDecl</code>.<br class="">

  <br class="">
Does anyone know the <code class="">clang::VarDecl</code> nodes are the only 
nodes that aren't visited by the <code class="">RecursiveASTVisitor</code> but 
are matched by <code class="">clang-query</code>?<br class="">
  <br class="">
Thanks in advaince!<br class="">
  <br class="">
```<code class=""><span class="hljs-function"><span class="hljs-keyword"><br class="">
double</span> <span class="hljs-title">multiply</span><span class="hljs-params">(<span class="hljs-keyword">double</span> x, <span class="hljs-keyword">double</span> y)</span> </span>{
  </code>
  <pre class="lang-c s-code-block hljs c"><code class="">    <span class="hljs-keyword">return</span> x * y * y;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> <span class="hljs-keyword">const</span> *argv[])</span>
</span>{
    <span class="hljs-keyword">double</span> a;

    <span class="hljs-keyword">int</span> b;

    <span class="hljs-keyword">float</span> d;

    <span class="hljs-keyword">double</span> x = <span class="hljs-number">3.0</span>;
    <span class="hljs-keyword">double</span> y = <span class="hljs-number">5.0</span>;

    <span class="hljs-keyword">double</span> z = multiply(x,y);

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
```
</code></pre>
My RecursiveASTVisitor is as follows:<br class="">
```<code class=""><span class="hljs-class"><span class="hljs-keyword"><br class="">
struct</span> <span class="hljs-title">MyASTVisitor</span> :</span> <span class="hljs-keyword">public</span> 
clang::RecursiveASTVistor<MyASTVisitor> {</code><code class="">
  </code><code class=""></code>
  <pre class="s-code-block lang-cpp hljs"><code class="">  <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">VistVarDecl</span><span class="hljs-params">(clang::VarDecl *vardecl)</span> </span>{
     llvm::outs() << <span class="hljs-string">"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" class=""><div class=""><div text="#000000" class=""><pre class="s-code-block lang-cpp hljs"><code class="">  };

  <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">VisitFunctionDecl</span><span class="hljs-params">(clang::FunctionDecl *decl)</span> </span>{
     llvm::outs() << <span class="hljs-string">"Found a FunctionDecl"</span>;
  };

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

<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">MyASTConsumer</span> :</span> <span class="hljs-keyword">public</span> clang::ASTConsumer {

  <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">HandleTopLevelDecl</span><span class="hljs-params">(clang::DeclGroupRef DR)</span> <span class="hljs-keyword">override</span> </span>{
    <span class="hljs-keyword">for</span> (clang::DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
        Visitor.TraverseDecl(*b);
     }
     <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
  }
<span class="hljs-keyword">private</span>: 
  MyASTVisitor Visitor;
};
```
</code></pre>
  <br class="">
  <div class="moz-signature">-- <br class="">Michael.<br class="">

  </div>
</div>

_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a><br class="">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev<br class=""></div></blockquote></div><br class=""></body></html>