我正在尝试升级我的 AutoFFI项目通过使其更加优雅并使用 Clang 的 ASTMatchers更广泛。我想创建一个匹配器来过滤指定的文件路径。是否可以做这样的事情,或者我是否需要在匹配器之外添加自定义逻辑才能使其工作?据我所知,没有办法获取 SourceManager
并使用它来创建 FullSourceLoc
,但也许我遗漏了一些东西。
一些相关链接:
- https://clang.llvm.org/doxygen/classclang_1_1FullSourceLoc.html
- https://github.com/llvm-mirror/clang/blob/f3b7928366f63b51ffc97e74f8afcff497c57e8d/include/clang/ASTMatchers/ASTMatchersMacros.h#L28
如果有人能告诉我这是否是 Clang 的 ASTMatcher API 的限制,我将非常感激!
最佳答案
没关系,我通过查看 isExpansionInMainFile
的来源找到了答案:
AST_POLYMORPHIC_MATCHER(isExpansionInMainFile,
AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) {
auto &SourceManager = Finder->getASTContext().getSourceManager();
return SourceManager.isInMainFile(
SourceManager.getExpansionLoc(Node.getBeginLoc()));
}
结果我错过了 MatchFinder
中的 getASTContext
,它保留了源管理器。
关于c++ - 在 Clang 中,编写自定义 ASTMatcher 时可以访问 SourceManager 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53851704/