FarisRehman created this revision.
Herald added a reviewer: sscalpone.
Herald added a subscriber: dang.
Herald added a reviewer: awarzynski.
Herald added a reviewer: jansvoboda11.
FarisRehman requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Add the following options:

- -fdebug-dump-symbols
- -fdebug-dump-parse-tree
- -fdebug-dump-provenance

Summary of changes:

- Add 3 new frontend actions: DebugDumpSymbolsAction, DebugDumpParseTreeAction 
and DebugDumpProvenanceAction
- Add a unique pointer to the Semantics instance created in PrescanAndSemaAction
- Move fatal semantic error reporting to its own method, 
FrontendActions#reportFatalSemanticErrors
- Port most tests using `-fdebug-dump-symbols` and `-fdebug-dump-parse-tree` to 
the new driver if built, otherwise default to f18


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96716

Files:
  clang/include/clang/Driver/Options.td
  flang/include/flang/Frontend/FrontendActions.h
  flang/include/flang/Frontend/FrontendOptions.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
  flang/test/Flang-Driver/debug-provenance.f90
  flang/test/Flang-Driver/driver-help.f90
  flang/test/Semantics/data05.f90
  flang/test/Semantics/data08.f90
  flang/test/Semantics/data09.f90
  flang/test/Semantics/offsets01.f90
  flang/test/Semantics/offsets02.f90
  flang/test/Semantics/offsets03.f90
  flang/test/Semantics/resolve100.f90
  flang/test/Semantics/rewrite01.f90

Index: flang/test/Semantics/rewrite01.f90
===================================================================
--- flang/test/Semantics/rewrite01.f90
+++ flang/test/Semantics/rewrite01.f90
@@ -1,4 +1,4 @@
-! RUN: %f18 -fdebug-dump-parse-tree %s 2>&1 | FileCheck %s
+! RUN: %flang_fc1 -fdebug-dump-parse-tree %s 2>&1 | FileCheck %s
 ! Ensure that READ(CVAR) [, item-list] is corrected when CVAR is a
 ! character variable so as to be a formatted read from the default
 ! unit, not an unformatted read from an internal unit (which is not
Index: flang/test/Semantics/resolve100.f90
===================================================================
--- flang/test/Semantics/resolve100.f90
+++ flang/test/Semantics/resolve100.f90
@@ -1,4 +1,4 @@
-!RUN: %f18 -fdebug-dump-symbols %s | FileCheck %s
+!RUN: %flang_fc1 -fdebug-dump-symbols %s | FileCheck %s
 
 program p
   ! CHECK: a size=4 offset=0: ObjectEntity type: LOGICAL(4)
Index: flang/test/Semantics/offsets03.f90
===================================================================
--- flang/test/Semantics/offsets03.f90
+++ flang/test/Semantics/offsets03.f90
@@ -1,4 +1,4 @@
-!RUN: %f18 -fdebug-dump-symbols %s | FileCheck %s
+!RUN: %flang_fc1 -fdebug-dump-symbols %s | FileCheck %s
 
 ! Size and alignment with EQUIVALENCE and COMMON
 
Index: flang/test/Semantics/offsets02.f90
===================================================================
--- flang/test/Semantics/offsets02.f90
+++ flang/test/Semantics/offsets02.f90
@@ -1,4 +1,4 @@
-!RUN: %f18 -fdebug-dump-symbols %s | FileCheck %s
+!RUN: %flang_fc1 -fdebug-dump-symbols %s | FileCheck %s
 
 ! Size and alignment of derived types
 
Index: flang/test/Semantics/offsets01.f90
===================================================================
--- flang/test/Semantics/offsets01.f90
+++ flang/test/Semantics/offsets01.f90
@@ -1,4 +1,4 @@
-!RUN: %f18 -fdebug-dump-symbols %s | FileCheck %s
+!RUN: %flang_fc1 -fdebug-dump-symbols %s | FileCheck %s
 
 ! Size and alignment of intrinsic types
 subroutine s1
Index: flang/test/Semantics/data09.f90
===================================================================
--- flang/test/Semantics/data09.f90
+++ flang/test/Semantics/data09.f90
@@ -1,4 +1,4 @@
-! RUN: %f18 -fsyntax-only -fdebug-dump-symbols %s 2>&1 | FileCheck %s
+! RUN: %flang_fc1 -fdebug-dump-symbols %s 2>&1 | FileCheck %s
 ! CHECK: init:[INTEGER(4)::1065353216_4,1073741824_4,1077936128_4,1082130432_4]
 ! Verify that the closure of EQUIVALENCE'd symbols with any DATA
 ! initialization produces a combined initializer.
Index: flang/test/Semantics/data08.f90
===================================================================
--- flang/test/Semantics/data08.f90
+++ flang/test/Semantics/data08.f90
@@ -1,4 +1,4 @@
-! RUN: %f18 -fdebug-dump-symbols %s 2>&1 | FileCheck %s
+! RUN: %flang_fc1 -fdebug-dump-symbols %s 2>&1 | FileCheck %s
 ! CHECK: DATA statement value initializes 'jx' of type 'INTEGER(4)' with CHARACTER
 ! CHECK: DATA statement value initializes 'jy' of type 'INTEGER(4)' with CHARACTER
 ! CHECK: DATA statement value initializes 'jz' of type 'INTEGER(4)' with CHARACTER
Index: flang/test/Semantics/data05.f90
===================================================================
--- flang/test/Semantics/data05.f90
+++ flang/test/Semantics/data05.f90
@@ -1,4 +1,4 @@
-!RUN: %f18 -fdebug-dump-symbols %s | FileCheck %s
+!RUN: %flang_fc1 -fdebug-dump-symbols %s | FileCheck %s
 module m
   interface
     integer function ifunc(n)
Index: flang/test/Flang-Driver/driver-help.f90
===================================================================
--- flang/test/Flang-Driver/driver-help.f90
+++ flang/test/Flang-Driver/driver-help.f90
@@ -46,6 +46,9 @@
 ! HELP-FC1-NEXT: -D <macro>=<value>     Define <macro> to <value> (or 1 if <value> omitted)
 ! HELP-FC1-NEXT: -emit-obj Emit native object files
 ! HELP-FC1-NEXT: -E                     Only run the preprocessor
+! HELP-FC1-NEXT: -fdebug-dump-parse-tree Dump the parse tree
+! HELP-FC1-NEXT: -fdebug-dump-provenance Dump provenance
+! HELP-FC1-NEXT: -fdebug-dump-symbols    Dump symbols after the semantic analysis
 ! HELP-FC1-NEXT: -fdebug-unparse-with-symbols
 ! HELP-FC1-NEXT:                        Unparse and stop.
 ! HELP-FC1-NEXT: -fdebug-unparse        Unparse and stop.
Index: flang/test/Flang-Driver/debug-provenance.f90
===================================================================
--- /dev/null
+++ flang/test/Flang-Driver/debug-provenance.f90
@@ -0,0 +1,26 @@
+! Ensure argument -fdebug-dump-provenance works as expected.
+
+! REQUIRES: new-flang-driver
+
+!--------------------------
+! FLANG DRIVER (flang-new)
+!--------------------------
+! RUN: not %flang-new -fdebug-dump-provenance %s  2>&1 | FileCheck %s --check-prefix=FLANG
+
+!----------------------------------------
+! FRONTEND FLANG DRIVER (flang-new -fc1)
+!----------------------------------------
+! RUN: %flang-new -fc1 -fdebug-dump-provenance %s  2>&1 | FileCheck %s --check-prefix=FRONTEND
+
+!----------------------------------
+! EXPECTED OUTPUT WITH `flang-new`
+!----------------------------------
+! FLANG:warning: argument unused during compilation: '-fdebug-dump-provenance'
+
+!---------------------------------------
+! EXPECTED OUTPUT WITH `flang-new -fc1`
+!---------------------------------------
+! FRONTEND:AllSources:
+! FRONTEND:CookedSource::provenanceMap_:
+program A
+end
\ No newline at end of file
Index: flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
===================================================================
--- flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -43,6 +43,15 @@
   case DebugUnparseWithSymbols:
     return std::make_unique<DebugUnparseWithSymbolsAction>();
     break;
+  case DebugDumpSymbols:
+    return std::make_unique<DebugDumpSymbolsAction>();
+    break;
+  case DebugDumpParseTree:
+    return std::make_unique<DebugDumpParseTreeAction>();
+    break;
+  case DebugDumpProvenance:
+    return std::make_unique<DebugDumpProvenanceAction>();
+    break;
   default:
     break;
     // TODO:
Index: flang/lib/Frontend/FrontendActions.cpp
===================================================================
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -9,16 +9,28 @@
 #include "flang/Frontend/FrontendActions.h"
 #include "flang/Common/default-kinds.h"
 #include "flang/Frontend/CompilerInstance.h"
-#include "flang/Frontend/FrontendOptions.h"
+#include "flang/Parser/dump-parse-tree.h"
 #include "flang/Parser/parsing.h"
 #include "flang/Parser/provenance.h"
 #include "flang/Parser/source.h"
 #include "flang/Parser/unparse.h"
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/unparse-with-symbols.h"
+#include "llvm/ADT/StringRef.h"
+#include <clang/Basic/Diagnostic.h>
+#include <memory>
 
 using namespace Fortran::frontend;
 
+void reportFatalSemanticErrors(const Fortran::semantics::Semantics &semantics,
+    clang::DiagnosticsEngine &diags, const llvm::StringRef &bufferName) {
+  if (semantics.AnyFatalError()) {
+    unsigned DiagID = diags.getCustomDiagID(
+        clang::DiagnosticsEngine::Error, "Semantic errors in %0");
+    diags.Report(DiagID) << bufferName;
+  }
+}
+
 bool PrescanAction::BeginSourceFileAction(CompilerInstance &c1) {
   CompilerInstance &ci = this->instance();
 
@@ -101,23 +113,15 @@
   auto &parseTree{*ci.parsing().parseTree()};
 
   // Prepare semantics
-  Fortran::semantics::Semantics semantics{ci.invocation().semanticsContext(),
-      parseTree, ci.parsing().cooked().AsCharBlock()};
+  semantics_ = std::make_unique<Fortran::semantics::Semantics>(
+      ci.invocation().semanticsContext(), parseTree,
+      ci.parsing().cooked().AsCharBlock());
 
   // Run semantic checks
-  semantics.Perform();
+  semantics_->Perform();
 
   // Report the diagnostics from the semantic checks
-  semantics.EmitMessages(ci.semaOutputStream());
-
-  if (semantics.AnyFatalError()) {
-    unsigned DiagID = ci.diagnostics().getCustomDiagID(
-        clang::DiagnosticsEngine::Error, "Semantic errors in %0");
-    ci.diagnostics().Report(DiagID) << GetCurrentFileOrBufferName();
-
-    return false;
-  }
-
+  semantics_->EmitMessages(ci.semaOutputStream());
   return true;
 }
 
@@ -183,7 +187,14 @@
   }
 }
 
-void ParseSyntaxOnlyAction::ExecuteAction() {}
+void DebugDumpProvenanceAction::ExecuteAction() {
+  this->instance().parsing().DumpProvenance(llvm::outs());
+}
+
+void ParseSyntaxOnlyAction::ExecuteAction() {
+  reportFatalSemanticErrors(*semantics_, this->instance().diagnostics(),
+      GetCurrentFileOrBufferName());
+}
 
 void DebugUnparseAction::ExecuteAction() {
   auto &parseTree{instance().parsing().parseTree()};
@@ -195,6 +206,10 @@
       /*encoding=*/Fortran::parser::Encoding::UTF_8,
       /*capitalizeKeywords=*/true, /*backslashEscapes=*/false,
       /*preStatement=*/nullptr, &asFortran);
+
+  // Report fatal semantic errors
+  reportFatalSemanticErrors(*semantics_, this->instance().diagnostics(),
+      GetCurrentFileOrBufferName());
 }
 
 void DebugUnparseWithSymbolsAction::ExecuteAction() {
@@ -202,6 +217,30 @@
 
   Fortran::semantics::UnparseWithSymbols(
       llvm::outs(), parseTree, /*encoding=*/Fortran::parser::Encoding::UTF_8);
+
+  // Report fatal semantic errors
+  reportFatalSemanticErrors(*semantics_, this->instance().diagnostics(),
+      GetCurrentFileOrBufferName());
+}
+
+void DebugDumpSymbolsAction::ExecuteAction() {
+  // Dump symbols
+  semantics_->DumpSymbols(llvm::outs());
+  // Report fatal semantic errors
+  reportFatalSemanticErrors(*semantics_, this->instance().diagnostics(),
+      GetCurrentFileOrBufferName());
+}
+
+void DebugDumpParseTreeAction::ExecuteAction() {
+  auto &parseTree{instance().parsing().parseTree()};
+  Fortran::parser::AnalyzedObjectsAsFortran asFortran =
+      Fortran::frontend::getBasicAsFortran();
+
+  // Dump parse tree
+  Fortran::parser::DumpTree(llvm::outs(), parseTree, &asFortran);
+  // Report fatal semantic errors
+  reportFatalSemanticErrors(*semantics_, this->instance().diagnostics(),
+      GetCurrentFileOrBufferName());
 }
 
 void EmitObjAction::ExecuteAction() {
Index: flang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- flang/lib/Frontend/CompilerInvocation.cpp
+++ flang/lib/Frontend/CompilerInvocation.cpp
@@ -116,6 +116,15 @@
     case clang::driver::options::OPT_fdebug_unparse_with_symbols:
       opts.programAction_ = DebugUnparseWithSymbols;
       break;
+    case clang::driver::options::OPT_fdebug_dump_symbols:
+      opts.programAction_ = DebugDumpSymbols;
+      break;
+    case clang::driver::options::OPT_fdebug_dump_parse_tree:
+      opts.programAction_ = DebugDumpParseTree;
+      break;
+    case clang::driver::options::OPT_fdebug_dump_provenance:
+      opts.programAction_ = DebugDumpProvenance;
+      break;
 
       // TODO:
       // case calng::driver::options::OPT_emit_llvm:
Index: flang/include/flang/Frontend/FrontendOptions.h
===================================================================
--- flang/include/flang/Frontend/FrontendOptions.h
+++ flang/include/flang/Frontend/FrontendOptions.h
@@ -40,6 +40,15 @@
   /// Fortran source file
   DebugUnparseWithSymbols,
 
+  /// -fdebug-dump-symbols
+  DebugDumpSymbols,
+
+  /// -fdebug-dump-parse-tree
+  DebugDumpParseTree,
+
+  /// -fdebug-dump-provenance
+  DebugDumpProvenance,
+
   /// TODO: RunPreprocessor, EmitLLVM, EmitLLVMOnly,
   /// EmitCodeGenOnly, EmitAssembly, (...)
 };
Index: flang/include/flang/Frontend/FrontendActions.h
===================================================================
--- flang/include/flang/Frontend/FrontendActions.h
+++ flang/include/flang/Frontend/FrontendActions.h
@@ -10,6 +10,8 @@
 #define LLVM_FLANG_FRONTEND_FRONTENDACTIONS_H
 
 #include "flang/Frontend/FrontendAction.h"
+#include "flang/Semantics/semantics.h"
+#include <memory>
 
 namespace Fortran::frontend {
 
@@ -37,12 +39,19 @@
   void ExecuteAction() override;
 };
 
+class DebugDumpProvenanceAction : public PrescanAction {
+  void ExecuteAction() override;
+};
+
 //===----------------------------------------------------------------------===//
 // PrescanAndSema Actions
 //===----------------------------------------------------------------------===//
 class PrescanAndSemaAction : public FrontendAction {
   void ExecuteAction() override = 0;
   bool BeginSourceFileAction(CompilerInstance &ci) override;
+
+public:
+  std::unique_ptr<Fortran::semantics::Semantics> semantics_;
 };
 
 class DebugUnparseWithSymbolsAction : public PrescanAndSemaAction {
@@ -53,6 +62,14 @@
   void ExecuteAction() override;
 };
 
+class DebugDumpSymbolsAction : public PrescanAndSemaAction {
+  void ExecuteAction() override;
+};
+
+class DebugDumpParseTreeAction : public PrescanAndSemaAction {
+  void ExecuteAction() override;
+};
+
 class ParseSyntaxOnlyAction : public PrescanAndSemaAction {
   void ExecuteAction() override;
 };
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -4246,6 +4246,12 @@
   HelpText<"Unparse and stop.">;
 def fdebug_unparse_with_symbols : Flag<["-"], "fdebug-unparse-with-symbols">, Group<Action_Group>,
   HelpText<"Unparse and stop.">;
+def fdebug_dump_symbols : Flag<["-"], "fdebug-dump-symbols">, Group<Action_Group>,
+  HelpText<"Dump symbols after the semantic analysis">;
+def fdebug_dump_parse_tree : Flag<["-"], "fdebug-dump-parse-tree">, Group<Action_Group>,
+  HelpText<"Dump the parse tree">;
+def fdebug_dump_provenance : Flag<["-"], "fdebug-dump-provenance">, Group<Action_Group>,
+  HelpText<"Dump provenance">;
 
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to