如何遍历clang AST的所有节点? [英] How to traverse all nodes of clang AST?

查看:419
本文介绍了如何遍历clang AST的所有节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用recursivevisitor类遍历clang AST的特定子树,但我要做的是逐节点遍历clang AST节点。

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.

预先感谢。

推荐答案

RecursiveASTVisitor可以满足您的需求。

RecursiveASTVisitor can do what you need.

实施成员方法 TraverseDecl(Decl * x) TraverseStmt(Stmt * x) TraverseType(QualType x)为您的RecursiveASTVisitor`派生类(例如MyClass)将达到目的。这三种方法结合起来,将带您进入AST中的每个节点。

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.

示例:

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

这篇关于如何遍历clang AST的所有节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆