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

Andrzej Warzynski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 22 08:23:43 PST 2020


awarzynski created this revision.
Herald added a reviewer: sscalpone.
awarzynski requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93712

Files:
  flang/lib/Frontend/FrontendAction.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Flang-Driver/parse-error.f95
  flang/test/Flang-Driver/scanning-error.f95


Index: flang/test/Flang-Driver/scanning-error.f95
===================================================================
--- /dev/null
+++ 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
Index: flang/test/Flang-Driver/parse-error.f95
===================================================================
--- /dev/null
+++ 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"
Index: flang/lib/Frontend/FrontendActions.cpp
===================================================================
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -78,8 +78,19 @@
   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
Index: flang/lib/Frontend/FrontendAction.cpp
===================================================================
--- flang/lib/Frontend/FrontendAction.cpp
+++ flang/lib/Frontend/FrontendAction.cpp
@@ -45,12 +45,25 @@
 }
 
 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()) {
+    unsigned diagID = ci.diagnostics().getCustomDiagID(
+        clang::DiagnosticsEngine::Error, "could not scan %0");
+    ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
+    ci.parsing().messages().Emit(
+        llvm::errs(), this->instance().allCookedSources());
+
+    return llvm::Error::success();
+  }
 
   ExecuteAction();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93712.313355.patch
Type: text/x-patch
Size: 2822 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201222/a5f1e63e/attachment.bin>


More information about the llvm-commits mailing list