[flang] Lift -Werror checks into local functions
authorAsher Mancinelli <ashermancinelli@gmail.com>
Fri, 6 Aug 2021 16:47:18 +0000 (10:47 -0600)
committerAsher Mancinelli <ashermancinelli@gmail.com>
Fri, 6 Aug 2021 18:43:19 +0000 (12:43 -0600)
Lift checks for -Werror into local functions.

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D101261

flang/lib/Frontend/FrontendActions.cpp
flang/test/Driver/parse-error.f95

index c1c9a10..d8bdb46 100644 (file)
@@ -45,6 +45,30 @@ bool reportFatalSemanticErrors(const Fortran::semantics::Semantics &semantics,
   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()};
@@ -53,18 +77,7 @@ bool PrescanAction::BeginSourceFileAction(CompilerInstance &c1) {
   // 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) {
@@ -88,27 +101,14 @@ 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());
@@ -124,31 +124,14 @@ bool PrescanAndSemaAction::BeginSourceFileAction(CompilerInstance &c1) {
   // 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());
index 0012604..3b9ad93 100644 (file)
@@ -1,5 +1,9 @@
 ! Verify that parsing errors are correctly reported by the driver
+! Focuses on actions inheriting from the following:
+! * PrescanAndSemaAction (-fsyntax-only)
+! * PrescanAndParseAction (-fdebug-unparse-no-sema)
 
+! RUN: not %flang_fc1 -fdebug-unparse-no-sema %s 2>&1 | FileCheck %s --check-prefix=ERROR
 ! RUN: not %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix=ERROR
 
 ! ERROR: Could not parse {{.*}}parse-error.f95