我正在构建一个基于 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 教程中注意到有一个clang::SyntaxOnlyAction
. 但是,我一直无法弄清楚如何MatchFinder
以及SyntaxOnlyAction
可以相互结合使用。同样,我已经能够从同一文件的命令行进行 AST 转储,没问题,所以我知道这是可能的。
是否可以配置基于 MatchFinder 的工具来遵守纯语法行为?