【问题标题】:Clang AST - Is it possible to match only on a subtree?Clang AST - 是否可以仅在子树上匹配?
【发布时间】:2017-05-16 16:21:42
【问题描述】:

我想找到一个名为“x”的变量,所以我定义了一个DeclarationMatcher

DeclarationMatcher decmatch = varDecl().bind("id");

我还使用匹配器和 MatchCallBack 设置了 MatchFinder

 matcher.addMatcher(decmatch, &callback);

我用它来匹配给定的 AST

 matcher.matchAST(astcontext);

我想知道是否可以使用此匹配器仅在子树内进行匹配,而不是在整个 AST 上进行匹配。例如,如果 a 有一个给定的 FunctionDecl 节点,我想使用匹配器仅匹配低于该节点的节点。我已经搜索了文档,但找不到任何可以帮助我解决此问题的内容。

【问题讨论】:

    标签: c++ clang abstract-syntax-tree


    【解决方案1】:

    我通过在 CompoundStmt 上使用 MatchFinder::match(node, astContext) 尝试了类似的操作。我的回忆是只检查了特定的节点,并且我添加了forEachDescendant 来处理后代。我不记得它是否在整个子树中递归。

    如果这对您不起作用,ASTMatchFinder.cpp 中的源代码应该会有所帮助。 matchAST() 的源代码只有大约五行。我希望如果你写了一个重载,matchAST(ASTContext &Context, Decl * myNode),并替换了

    Visitor.TraverseDecl(Context.getTranslationUnitDecl());
    

    Visitor.TraverseDecl(myNode);
    

    那会做你想做的。

    【讨论】:

    • 感谢您的想法!向 MatchFinder 中添加了一种类似于 matchAST 的新方法……效果很好!
    猜你喜欢
    • 1970-01-01
    • 2021-02-25
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    相关资源
    最近更新 更多