[llvm-branch-commits] [flang] e49dc29 - [flang][driver] Add checks for errors from `Prescan` and `Parse`

Andrzej Warzynski via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jan 6 02:24:56 PST 2021


Author: Andrzej Warzynski
Date: 2021-01-06T10:19:44Z
New Revision: e49dc2981cb311ac2b696ebb016fac9a8cd60922

URL: https://github.com/llvm/llvm-project/commit/e49dc2981cb311ac2b696ebb016fac9a8cd60922
DIFF: https://github.com/llvm/llvm-project/commit/e49dc2981cb311ac2b696ebb016fac9a8cd60922.diff

LOG: [flang][driver] Add checks for errors from `Prescan` and `Parse`

If either `Prescan` or `Parse` generate any fatal errors, the new driver
will:
  * report it (i.e. issue an error diagnostic)
  * exit early
  * return non-zero exit code
This behaviour is consistent with f18 (i.e. the old driver).

Reviewed By: sameeranjoshi

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

Added: 
    flang/test/Flang-Driver/parse-error.f95
    flang/test/Flang-Driver/scanning-error.f95

Modified: 
    flang/lib/Frontend/FrontendAction.cpp
    flang/lib/Frontend/FrontendActions.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Frontend/FrontendAction.cpp b/flang/lib/Frontend/FrontendAction.cpp
index ad1079d47db1..4972669bec87 100644
--- a/flang/lib/Frontend/FrontendAction.cpp
+++ b/flang/lib/Frontend/FrontendAction.cpp
@@ -45,12 +45,24 @@ bool FrontendAction::ShouldEraseOutputFiles() {
 }
 
 llvm::Error FrontendAction::Execute() {
+  CompilerInstance &ci = this->instance();
+
   std::string currentInputPath{GetCurrentFileOrBufferName()};
 
   Fortran::parser::Options parserOptions =
       this->instance().invocation().fortranOpts();
 
-  this->instance().parsing().Prescan(currentInputPath, parserOptions);
+  // 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());
+
+    return llvm::Error::success();
+  }
 
   ExecuteAction();
 

diff  --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index b34dae7cbf17..fe21fc5e3d2e 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -78,8 +78,19 @@ void ParseSyntaxOnlyAction::ExecuteAction() {
   common::LanguageFeatureControl features;
   Fortran::common::IntrinsicTypeDefaultKinds defaultKinds;
 
-  // Parse
+  // 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());
+    return;
+  }
+
   auto &parseTree{*ci.parsing().parseTree()};
 
   // Prepare semantics

diff  --git a/flang/test/Flang-Driver/parse-error.f95 b/flang/test/Flang-Driver/parse-error.f95
new file mode 100644
index 000000000000..34af7e2d2ed6
--- /dev/null
+++ b/flang/test/Flang-Driver/parse-error.f95
@@ -0,0 +1,8 @@
+! RUN: not %flang-new -fc1 -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix=ERROR
+! RUN: not %f18 -parse-only %s 2>&1 | FileCheck %s --check-prefix=ERROR
+
+! REQUIRES: new-flang-driver
+
+! ERROR: could not parse {{.*}}parse-error.f95
+
+"This file will not parse"

diff  --git a/flang/test/Flang-Driver/scanning-error.f95 b/flang/test/Flang-Driver/scanning-error.f95
new file mode 100644
index 000000000000..5fcf89d569c1
--- /dev/null
+++ b/flang/test/Flang-Driver/scanning-error.f95
@@ -0,0 +1,8 @@
+! RUN: not %flang-new -fc1 -E %s 2>&1 | FileCheck %s --check-prefix=ERROR
+! RUN: not %f18 -E %s 2>&1 | FileCheck %s --check-prefix=ERROR
+
+! REQUIRES: new-flang-driver
+
+! ERROR: could not scan {{.*}}scanning-error.f95
+
+#NOT-PP-DIRECTIVE


        


More information about the llvm-branch-commits mailing list