18

I can traverse the specific subtrees of clang AST using the recursivevisitor class but what I want to do is to traverse the clang AST node by node.

I'd be really grateful if anybody can help me with this.

Thanks in advance.

jw_
  • 1,663
  • 18
  • 32
hbn1991
  • 264
  • 3
  • 10
  • whats wrong with `RecursiveASTVisitor`? – m.s. Jun 12 '15 at 14:33
  • @m.s. There's nothing wrong with the RecursiveASTVisitor it's just that it traverses only specific subtrees, for example like the set of all MethodCalls in the AST, but what i want is to traverse through all the nodes of the AST one by one extracting some set of information about each node. That is why i need a method to traverse the AST node by node. – hbn1991 Jun 12 '15 at 15:24
  • 1
    You may use dump() of the root node: Context.getTranslationUnitDecl()->dump(). If this doesn't satisfy your need, you can read and edit dump() source code. – jw_ Dec 31 '19 at 03:29

1 Answers1

25

RecursiveASTVisitor can do what you need.

Implementing the member methods TraverseDecl(Decl *x), TraverseStmt(Stmt *x) and TraverseType(QualType x) for your RecursiveASTVisitor`-derived class (e.g. MyClass) will do the trick. Combined, those three methods will take you to each and every node in your AST.

Example:

class MyClass : public RecursiveASTVisitor<MyClass> {
public:
    bool TraverseDecl(Decl *D) {
        // your logic here
        RecursiveASTVisitor<MyClass>::TraverseDecl(D); // Forward to base class
        return true; // Return false to stop the AST analyzing
    }
    bool TraverseStmt(Stmt *x) {
        // your logic here
        RecursiveASTVisitor<MyClass>::TraverseStmt(x);
        return true;
    }
    bool TraverseType(QualType x) {
        // your logic here
        RecursiveASTVisitor<MyClass>::TraverseType(x);
        return true;
    }
};
rettichschnidi
  • 904
  • 7
  • 23
  • The documentation of RecursiveASTVisitor lists more Traverse...() methods. Shouldn't these also be implemented? Or will that duplicate traversion? – Folkert van Heusden Aug 01 '16 at 13:41
  • You can use any of the methods, but usually you will probably want to traverse the most abstract types (Expr, Stmt), for example in IfStmt if you want to conditionally traverse the Then/Else parts (without caring about their actual type). – Kuba Beránek Oct 07 '16 at 21:39