Search code examples
c++clangabstract-syntax-treelibclang

What is the difference between AST traversal by Cursors and Nodes


I'm playing with libclang to parse small c++ files. I've seen examples about how to parse the AST trees.

As I understood AST constitutes of ASTNodes which has the type of either Decl or Stmt. To traverse the tree I can either use the ASTConsumer which visits ASTNodes or CxCursors.

What is the difference between these two traversal methods?


Solution

  • After having read some pages, I found some information about this subject. According to the clang Tooling documentation there are two tools (among other clang tools) : - Libclang - Libtooling

    The CxCursors belongs to the Libclang library. The Libclang library provides the cursors to traverse the AST although the control on the AST is limited.

    The classes Decl and Stmt are the part of the Libtooling. By defining a RecursiveASTVisitor and its corresponding visiting function one can traverse these nodes. With this method one can have full control over on the AST.