[flang-commits] [flang] 6f8ef1d - [flang][driver] Add actions that execute despite semantic errors

Andrzej Warzynski via flang-commits flang-commits at lists.llvm.org
Mon Oct 11 04:53:45 PDT 2021


Author: Andrzej Warzynski
Date: 2021-10-11T11:52:05Z
New Revision: 6f8ef1d6e8796993b69d4b1cf8e56590273268e3

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

LOG: [flang][driver] Add actions that execute despite semantic errors

This patch adds a new abstract class for frontend actions:
`PrescanAndSemaDebugAction`. It's almost identical to
`PrescanAndSemaAction`, but in the presence of semantic errors it does
not skip the corresponding `ExecuteAction` specialisation. Instead, it
runs it as if there were no semantic errors. This class is for developer
actions only (i.e.  front-end driver options).

The new behaviour does not affect the return code from `flang-new -fc1`
when the input file is semantically incorrect. The return code is
inferred from the number of driver diagnostics generated in
`CompilerInstance::ExecuteAction` and this patch does not change that.
More specifically, the semantic errors are still reported and hence the
driver is able to correctly report that the compilation has failed (with
a non-zero return code).

This new base class is meant for debug actions only and
`DebugDumpAllAction` is updated to demonstrate the new behaviour. With
this change, `flang-new -fc1 -fdebug-dump-all` dumps the parse tree and
symbols for all input files, regardless of whether any semantic errors
were found.

This patch addresses https://bugs.llvm.org/show_bug.cgi?id=52097.

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

Added: 
    flang/test/Driver/dump-all-bad.f90

Modified: 
    flang/include/flang/Frontend/FrontendActions.h
    flang/lib/Frontend/FrontendActions.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Frontend/FrontendActions.h b/flang/include/flang/Frontend/FrontendActions.h
index 70a052ca405b0..1e17f194ba5f2 100644
--- a/flang/include/flang/Frontend/FrontendActions.h
+++ b/flang/include/flang/Frontend/FrontendActions.h
@@ -84,6 +84,9 @@ class DebugDumpParseTreeNoSemaAction : public PrescanAndParseAction {
 
 //===----------------------------------------------------------------------===//
 // PrescanAndSema Actions
+//
+// These actions will parse the input, run the semantic checks and execute
+// their actions provided that no parsing or semantic errors were found.
 //===----------------------------------------------------------------------===//
 class PrescanAndSemaAction : public FrontendAction {
 
@@ -107,10 +110,6 @@ class DebugDumpParseTreeAction : public PrescanAndSemaAction {
   void ExecuteAction() override;
 };
 
-class DebugDumpAllAction : public PrescanAndSemaAction {
-  void ExecuteAction() override;
-};
-
 class DebugPreFIRTreeAction : public PrescanAndSemaAction {
   void ExecuteAction() override;
 };
@@ -131,6 +130,22 @@ class PluginParseTreeAction : public PrescanAndSemaAction {
   void ExecuteAction() override = 0;
 };
 
+//===----------------------------------------------------------------------===//
+// PrescanAndSemaDebug Actions
+//
+// These actions will parse the input, run the semantic checks and execute
+// their actions regardless of whether any semantic errors are found.
+//===----------------------------------------------------------------------===//
+class PrescanAndSemaDebugAction : public FrontendAction {
+
+  void ExecuteAction() override = 0;
+  bool BeginSourceFileAction() override;
+};
+
+class DebugDumpAllAction : public PrescanAndSemaDebugAction {
+  void ExecuteAction() override;
+};
+
 } // namespace Fortran::frontend
 
 #endif // LLVM_FLANG_FRONTEND_FRONTENDACTIONS_H

diff  --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 4dc0c2c73a13e..cdc2031275fb3 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -40,6 +40,11 @@ bool PrescanAndSemaAction::BeginSourceFileAction() {
   return RunPrescan() && RunParse() && RunSemanticChecks();
 }
 
+bool PrescanAndSemaDebugAction::BeginSourceFileAction() {
+  // Semantic checks are made to succeed unconditionally.
+  return RunPrescan() && RunParse() && (RunSemanticChecks() || true);
+}
+
 //===----------------------------------------------------------------------===//
 // Custom ExecuteAction
 //===----------------------------------------------------------------------===//

diff  --git a/flang/test/Driver/dump-all-bad.f90 b/flang/test/Driver/dump-all-bad.f90
new file mode 100644
index 0000000000000..f29ab240936f1
--- /dev/null
+++ b/flang/test/Driver/dump-all-bad.f90
@@ -0,0 +1,21 @@
+! Verify that -fdebug-dump-all dumps both symbols and the parse tree, even when semantic errors are present
+
+!----------
+! RUN lines
+!----------
+! RUN: not %flang_fc1 -fdebug-dump-all %s 2>&1 | FileCheck %s
+
+!----------------
+! EXPECTED OUTPUT
+!----------------
+! CHECK: error: Semantic errors in
+! CHECK: Flang: parse tree dump
+! CHECK: Flang: symbols dump
+
+!-------
+! INPUT
+!-------
+program bad
+  real,pointer :: x
+  x = null()      ! Error - must be pointer assignment
+end


        


More information about the flang-commits mailing list