[cfe-dev] RecursiveASTVisitor Behavior

Michael Chiu via cfe-dev cfe-dev at lists.llvm.org
Thu Mar 18 08:42:57 PDT 2021

Hi All,

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 |bool 
VistVarDecl(clang::VarDecl *vardecl)|. However, the |clang::VarDecl| 
nodes are never visited even though I've managed to visit all the other 
nodes in the AST though. Moreover, using |clang-query| on |test1.c| I 
/can/ match |varDecl|.

Does anyone know the |clang::VarDecl| nodes are the only nodes that 
aren't visited by the |RecursiveASTVisitor| but are matched by 

Thanks in advaince!

double multiply(double x, double y) { |

|return x * y * y; } int main(int argc, char const *argv[]) { double a; 
int b; float d; double x = 3.0; double y = 5.0; double z = 
multiply(x,y); return 0; } ``` |

My RecursiveASTVisitor is as follows:
struct MyASTVisitor : public clang::RecursiveASTVistor<MyASTVisitor> {|||||

|bool VistVarDecl(clang::VarDecl *vardecl) { llvm::outs() << "Found a 
VarDecl"; }; bool VisitFunctionDecl(clang::FunctionDecl *decl) { 
llvm::outs() << "Found a FunctionDecl"; }; // other functions 
implemented similarly just to see if it visits properly bool 
VisitParmVarmDecl(clang::ParmVarDecl *paramvardecl); bool 
VisitCallExpr(clang::CallExpr *callexpr); bool 
VisitImplicitCastExpr(clang::ImplicitCastExpr *castexpr); bool 
VisitBinaryOperator(clang::BinaryOperator *bo); bool 
VisitDeclStmt(clang::DeclStmt *declstmt); bool 
VisitDeclRefExpr(clang::DeclRefExpr *declrefexpr); bool 
VisitFloatingLiteral(clang::FloatingLiteral *floatliteral); }; struct 
MyASTConsumer : public clang::ASTConsumer { bool 
HandleTopLevelDecl(clang::DeclGroupRef DR) override { for 
(clang::DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; 
++b) { Visitor.TraverseDecl(*b); } return true; } private: MyASTVisitor 
Visitor; }; ``` |

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210318/ff7b6451/attachment.html>

More information about the cfe-dev mailing list