如果为我的插件将要运行的 AST 编译代码时出现错误,我希望我的 clang 插件实际上不执行任何操作。
但是,我无法弄清楚哪个对象包含编译期间生成的错误列表。
是否有用于判断是否存在错误的 bool 查询或用于获取 TU 编译期间生成的所有错误(或所有诊断)的列表 API?
谢谢。
最佳答案
我最终找到了一种从 CompilerInstance
对象获取错误计数的方法:
auto & ast_context = compiler_instance.getASTContext();
auto & diagnostics_engine = ast_context.getDiagnostics();
auto diagnostic_consumer = diagnostics_engine.getClient();
auto error_count = diagnostic_consumer->getNumErrors();
if (error_count > 0) {
llvm::report_fatal_error("Errors during compilation, plugin aborting");
}
我不知道这是否是最好的方法,但是当我将上面的代码放在我从 ast_matchers::MatchFinder 派生的类的
。onStartOfTranslationUnit
中时,它对我有用::匹配回调
关于c++ - 在 clang 插件中,如何查看编译翻译单元是否出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47835491/