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

查看:94
本文介绍了如何遍历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), <您的 RecursiveASTVisitor` 派生类(例如 MyClass)的 code>TraverseStmt(Stmt *x) 和 TraverseType(QualType x) 将起作用.结合起来,这三种方法将带您访问 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天全站免登陆