8

I've read this link but still don't fully understand what's the difference between TraverseDecl and VisitDecl (and their use case) http://clang.llvm.org/doxygen/classclang_1_1RecursiveASTVisitor.html

Which method should I be overriding when writing my RecursiveASTVisitor?

Jeremy Kuah
  • 519
  • 1
  • 6
  • 18

1 Answers1

5

TraverseDecl tells the frontend library's ASTConsumer to visit declarations recursively from the AST. Then VisitDecl is called where you can extract the relevant information.

Follow these two links for more details and a simple checker example:

http://clang.llvm.org/docs/RAVFrontendAction.html

How to traverse clang AST manually ?

Community
  • 1
  • 1
Nishant Sharma
  • 683
  • 9
  • 16
  • Are there any difference to using TraverseDecl or VisitDecl within the ASTVisitor though. In the example, TraverseDecl is used within ASTConsumer and VisitDecl in the ASTVisitor. However, the other stackoverflow link has TraverseDecl within ASTVisitor itself. – Jeremy Kuah Jun 15 '16 at 02:03
  • [RecursiveASTVisiter](http://clang.llvm.org/doxygen/classclang_1_1RecursiveASTVisitor.html) If you look at the detailed description in the link: You can see that traverseDecl will come at an upper tier than visitDecl. So if it were up to me I will follow [Clang Plugin](http://clang.llvm.org/docs/RAVFrontendAction.html) example as it maintains that hierarchy and is more understandable. – Nishant Sharma Jun 15 '16 at 05:58
  • That means to use Traverse within the ASTConsumer and Visits within ASTVisitor? Mhmm. – Jeremy Kuah Jun 17 '16 at 02:15
  • To be a little honest with you, I am also relatively new to Clang. But, from what I understand it is, in fact, more intuitive to do visits in ASTVisitor and not in ASTConsumer. If you look at the VISITOR Pattern that might help to get the intuition behind why ASTVisitors are implemented the way they are implemented. – Nishant Sharma Jun 18 '16 at 07:00