Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
340 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;
    }
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...