Clang AST - 是否可以只匹配子树?

Clang AST - Is it possible to match only on a subtree?

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

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

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

 matcher.addMatcher(decmatch, &callback);

我用它来匹配给定的 AST

 matcher.matchAST(astcontext);

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

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

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

Visitor.TraverseDecl(Context.getTranslationUnitDecl());

Visitor.TraverseDecl(myNode);

那会做你想要的。