我正在构建一个基于ASTMatcher的工具,我想在我的资源上运行它:
int main(int argc, const char** argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
MatchFinder Finder;
// Repeated calls to Finder.addMatcher(...);
Tool.run(newFrontendActionFactory(&Finder).get());
// Handle the results of the matching.
}
在依赖于其他标头的源文件上运行此命令会产生以下错误:
~$ /path/to/my/tool /path/to/my/file.cpp --
/path/to/my/file.cpp:8:10: fatal error: 'string' file not found
#include <string>
^~~~~~~~
1 error generated.
Error while processing /path/to/my/file.cpp.
我不想在此处理中包括任何其他标头,以免匹配器在那些我不想处理的标头中找到内容。
我尝试将-fsyntax_only
传递给该工具,但得到的结果与上述相同:
~$ /path/to/my/tool /path/to/my/file.cpp -- -fsyntax-only
我在ASTMatcher tutorial中注意到有一个clang::SyntaxOnlyAction
。但是,我无法弄清楚MatchFinder
和SyntaxOnlyAction
可以如何结合使用。同样,我已经能够从同一文件的命令行执行AST转储,没问题,所以我知道这是可能的。
是否可以配置基于MatchFinder的工具来实现纯语法行为?