[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
This commit is contained in:
Andrzej Warzynski 2021-02-04 13:06:43 +00:00
parent 75b2555d6e
commit d06e94031b
5 changed files with 63 additions and 37 deletions

View File

@ -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.
///

View File

@ -21,18 +21,26 @@ class InputOutputTestAction : public FrontendAction {
void ExecuteAction() override;
};
class PrintPreprocessedAction : public FrontendAction {
void ExecuteAction() override;
};
class ParseSyntaxOnlyAction : public FrontendAction {
void ExecuteAction() override;
};
class EmitObjAction : public FrontendAction {
void ExecuteAction() override;
};
//===----------------------------------------------------------------------===//
// Prescan Actions
//===----------------------------------------------------------------------===//
class PrescanAction : public FrontendAction {
void ExecuteAction() override = 0;
bool BeginSourceFileAction(CompilerInstance &ci) override;
};
class PrintPreprocessedAction : public PrescanAction {
void ExecuteAction() override;
};
class ParseSyntaxOnlyAction : public PrescanAction {
void ExecuteAction() override;
};
} // namespace Fortran::frontend
#endif // LLVM_FLANG_FRONTEND_FRONTENDACTIONS_H

View File

@ -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));
}

View File

@ -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();

View File

@ -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;