From: Andrzej Warzynski Date: Thu, 4 Feb 2021 13:06:43 +0000 (+0000) Subject: [flang][driver] Add PrescanAction frontend action (nfc) X-Git-Tag: llvmorg-14-init~16047 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d06e94031bcdfa43512bf7b0cdfd4b4bad3ca4e1;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/flang/include/flang/Frontend/FrontendAction.h b/flang/include/flang/Frontend/FrontendAction.h index ccd2c2f..87e82fe 100644 --- a/flang/include/flang/Frontend/FrontendAction.h +++ b/flang/include/flang/Frontend/FrontendAction.h @@ -39,6 +39,12 @@ protected: /// 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 @@ public: /// @} /// @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 9f125e3..f28e3c2 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 438aeb8..9640cd7 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 027024b..650fd29 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 a89ebf0..bbf8004 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;