return false;
}
+template <unsigned N>
+static bool reportFatalErrors(
+ const FrontendAction *act, const char (&message)[N]) {
+ CompilerInstance &ci = act->instance();
+ if (!ci.parsing().messages().empty() &&
+ (ci.invocation().warnAsErr() ||
+ ci.parsing().messages().AnyFatalError())) {
+ const unsigned diagID = ci.diagnostics().getCustomDiagID(
+ clang::DiagnosticsEngine::Error, message);
+ ci.diagnostics().Report(diagID) << act->GetCurrentFileOrBufferName();
+ ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
+ return true;
+ }
+ return false;
+}
+
+inline bool reportFatalScanningErrors(const FrontendAction *act) {
+ return reportFatalErrors(act, "Could not scan %0");
+}
+
+inline bool reportFatalParsingErrors(const FrontendAction *act) {
+ return reportFatalErrors(act, "Could not parse %0");
+}
+
bool PrescanAction::BeginSourceFileAction(CompilerInstance &c1) {
CompilerInstance &ci = this->instance();
std::string currentInputPath{GetCurrentFileOrBufferName()};
// Prescan. In case of failure, report and return.
ci.parsing().Prescan(currentInputPath, parserOptions);
- if (!ci.parsing().messages().empty() &&
- (ci.invocation().warnAsErr() ||
- ci.parsing().messages().AnyFatalError())) {
- const unsigned diagID = ci.diagnostics().getCustomDiagID(
- clang::DiagnosticsEngine::Error, "Could not scan %0");
- ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
- ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
-
- return false;
- }
-
- return true;
+ return !reportFatalScanningErrors(this);
}
bool PrescanAndParseAction::BeginSourceFileAction(CompilerInstance &c1) {
// Prescan. In case of failure, report and return.
ci.parsing().Prescan(currentInputPath, parserOptions);
- if (ci.parsing().messages().AnyFatalError()) {
- const unsigned diagID = ci.diagnostics().getCustomDiagID(
- clang::DiagnosticsEngine::Error, "Could not scan %0");
- ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
- ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
-
+ if (reportFatalScanningErrors(this))
return false;
- }
// Parse. In case of failure, report and return.
ci.parsing().Parse(llvm::outs());
- if (ci.parsing().messages().AnyFatalError()) {
- unsigned diagID = ci.diagnostics().getCustomDiagID(
- clang::DiagnosticsEngine::Error, "Could not parse %0");
- ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
-
- ci.parsing().messages().Emit(
- llvm::errs(), this->instance().allCookedSources());
+ if (reportFatalParsingErrors(this))
return false;
- }
// Report the diagnostics from parsing
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
// Prescan. In case of failure, report and return.
ci.parsing().Prescan(currentInputPath, parserOptions);
- if (!ci.parsing().messages().empty() &&
- (ci.invocation().warnAsErr() ||
- ci.parsing().messages().AnyFatalError())) {
- const unsigned diagID = ci.diagnostics().getCustomDiagID(
- clang::DiagnosticsEngine::Error, "Could not scan %0");
- ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
- ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
-
+ if (reportFatalScanningErrors(this))
return false;
- }
// Parse. In case of failure, report and return.
ci.parsing().Parse(llvm::outs());
- if (!ci.parsing().messages().empty() &&
- (ci.invocation().warnAsErr() ||
- ci.parsing().messages().AnyFatalError())) {
- unsigned diagID = ci.diagnostics().getCustomDiagID(
- clang::DiagnosticsEngine::Error, "Could not parse %0");
- ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
-
- ci.parsing().messages().Emit(
- llvm::errs(), this->instance().allCookedSources());
+ if (reportFatalParsingErrors(this))
return false;
- }
// Report the diagnostics from parsing
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());