在 Clang 中,我可以在编写自定义 ASTMatcher 时访问 SourceManager 吗?
In Clang, can I access the SourceManager when writing a custom ASTMatcher?
我正在尝试更广泛地升级我的 AutoFFI project by making it more elegant and use Clang's 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
,它保留在源代码管理器中。
我正在尝试更广泛地升级我的 AutoFFI project by making it more elegant and use Clang's 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
,它保留在源代码管理器中。