[flang-commits] [flang] d06e940 - [flang][driver] Add PrescanAction frontend action (nfc)

Andrzej Warzynski via flang-commits flang-commits at lists.llvm.org
Thu Feb 4 06:24:03 PST 2021


Author: Andrzej Warzynski
Date: 2021-02-04T14:23:33Z
New Revision: d06e94031bcdfa43512bf7b0cdfd4b4bad3ca4e1

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

LOG: [flang][driver] Add PrescanAction frontend action (nfc)

This new action encapsulates all actions that require the prescanner to
be run before proceeding with other processing. By adding this new
action, we are better equipped to control which actions _do_ run the
prescanner and which _do not_.

The following actions that require the prescanner are refactored to
inherit from `PrescanAction`:
  * `PrintPreprocessedAction`
  * `ParseSyntaxOnlyAction` .

New virtual method is introduced to facilitate all this:
  * `BeginSourceFileAction`
Like in Clang, this method is run inside `BeginSourceFile`. In other
words, it is invoked before `ExecuteAction` for the corresponding
frontend action is run. This method allows us to:
  * carry out any processing that is always required by the action (e.g.
    run the prescanner)
  * fine tune the settings/options on a file-by-file basis (e.g. to
    decide between fixed-form and free-form based on file extension)

This patch implements non-functional-changes.

Reviewed By: FarisRehman

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Frontend/FrontendAction.h b/flang/include/flang/Frontend/FrontendAction.h
index ccd2c2f0f7ae..87e82fe5274f 100644
--- a/flang/include/flang/Frontend/FrontendAction.h
+++ b/flang/include/flang/Frontend/FrontendAction.h
@@ -39,6 +39,12 @@ class FrontendAction {
   /// By default it returns true if a compiler error occurred.
   virtual bool ShouldEraseOutputFiles();
 
+  /// Callback at the start of processing a single input.
+  ///
+  /// \return True on success; on failure ExecutionAction() and
+  /// EndSourceFileAction() will not be called.
+  virtual bool BeginSourceFileAction(CompilerInstance &ci) { return true; }
+
   /// @}
 
 public:
@@ -76,7 +82,7 @@ class FrontendAction {
 
   /// @}
   /// @name Public Action Interface
-  /// @}
+  /// @{
 
   /// Prepare the action for processing the input file \p input.
   ///

diff  --git a/flang/include/flang/Frontend/FrontendActions.h b/flang/include/flang/Frontend/FrontendActions.h
index 9f125e3a91e9..f28e3c2bc552 100644
--- a/flang/include/flang/Frontend/FrontendActions.h
+++ b/flang/include/flang/Frontend/FrontendActions.h
@@ -21,15 +21,23 @@ class InputOutputTestAction : public FrontendAction {
   void ExecuteAction() override;
 };
 
-class PrintPreprocessedAction : public FrontendAction {
+class EmitObjAction : public FrontendAction {
   void ExecuteAction() override;
 };
 
-class ParseSyntaxOnlyAction : public FrontendAction {
+//===----------------------------------------------------------------------===//
+// Prescan Actions
+//===----------------------------------------------------------------------===//
+class PrescanAction : public FrontendAction {
+  void ExecuteAction() override = 0;
+  bool BeginSourceFileAction(CompilerInstance &ci) override;
+};
+
+class PrintPreprocessedAction : public PrescanAction {
   void ExecuteAction() override;
 };
 
-class EmitObjAction : public FrontendAction {
+class ParseSyntaxOnlyAction : public PrescanAction {
   void ExecuteAction() override;
 };
 

diff  --git a/flang/lib/Frontend/CompilerInstance.cpp b/flang/lib/Frontend/CompilerInstance.cpp
index 438aeb8a84e5..9640cd78797d 100644
--- a/flang/lib/Frontend/CompilerInstance.cpp
+++ b/flang/lib/Frontend/CompilerInstance.cpp
@@ -148,14 +148,6 @@ bool CompilerInstance::ExecuteAction(FrontendAction &act) {
   // Run the frontend action `act` for every input file.
   for (const FrontendInputFile &fif : frontendOpts().inputs_) {
     if (act.BeginSourceFile(*this, fif)) {
-      if (invoc.frontendOpts().fortranForm_ == FortranForm::Unknown) {
-        // Switch between fixed and free form format based on the input file
-        // extension. Ideally we should have all Fortran options set before
-        // entering this loop (i.e. processing any input files). However, we
-        // can't decide between fixed and free form based on the file extension
-        // earlier than this.
-        invoc.fortranOpts().isFixedForm = fif.IsFixedForm();
-      }
       if (llvm::Error err = act.Execute()) {
         consumeError(std::move(err));
       }

diff  --git a/flang/lib/Frontend/FrontendAction.cpp b/flang/lib/Frontend/FrontendAction.cpp
index 027024b8e12b..650fd2999bed 100644
--- a/flang/lib/Frontend/FrontendAction.cpp
+++ b/flang/lib/Frontend/FrontendAction.cpp
@@ -61,10 +61,17 @@ bool FrontendAction::BeginSourceFile(
   assert(!realInput.IsEmpty() && "Unexpected empty filename!");
   set_currentInput(realInput);
   set_instance(&ci);
+
   if (!ci.HasAllSources()) {
     BeginSourceFileCleanUp(*this, ci);
     return false;
   }
+
+  if (!BeginSourceFileAction(ci)) {
+    BeginSourceFileCleanUp(*this, ci);
+    return false;
+  }
+
   return true;
 }
 
@@ -73,25 +80,6 @@ bool FrontendAction::ShouldEraseOutputFiles() {
 }
 
 llvm::Error FrontendAction::Execute() {
-  CompilerInstance &ci = this->instance();
-
-  std::string currentInputPath{GetCurrentFileOrBufferName()};
-
-  Fortran::parser::Options parserOptions =
-      this->instance().invocation().fortranOpts();
-
-  // 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();
 
   return llvm::Error::success();

diff  --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index a89ebf0b9f9a..bbf80045286d 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -16,8 +16,40 @@
 
 using namespace Fortran::frontend;
 
-void InputOutputTestAction::ExecuteAction() {
+bool PrescanAction::BeginSourceFileAction(CompilerInstance &c1) {
+  CompilerInstance &ci = this->instance();
+
+  std::string currentInputPath{GetCurrentFileOrBufferName()};
+
+  Fortran::parser::Options parserOptions = ci.invocation().fortranOpts();
 
+  if (ci.invocation().frontendOpts().fortranForm_ == FortranForm::Unknown) {
+    // Switch between fixed and free form format based on the input file
+    // extension.
+    //
+    // Ideally we should have all Fortran options set before entering this
+    // method (i.e. before processing any specific input files). However, we
+    // can't decide between fixed and free form based on the file extension
+    // earlier than this.
+    parserOptions.isFixedForm = currentInput().IsFixedForm();
+  }
+
+  // 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 false;
+  }
+
+  return true;
+}
+
+void InputOutputTestAction::ExecuteAction() {
   // Get the name of the file from FrontendInputFile current.
   std::string path{GetCurrentFileOrBufferName()};
   std::string buf;


        


More information about the flang-commits mailing list