llvm-project/flang/lib/Frontend/FrontendAction.cpp
Caroline Concatto d28de0d7f2 [Flang][Driver] Add PrintPreprocessedInput FrontendAction (flang-new -E)
This patch implements the first frontend action for the Flang parser (i.e.
Fortran::parser). This action runs the preprocessor and is invoked with the
`-E` flag. (i.e. `flang-new -E <input-file>). The generated output is printed
to either stdout or the output file (specified with `-` or `-o <output-file>`).

Note that currently there is no mechanism to map options for the
frontend driver (i.e. Fortran::frontend::FrontendOptions) to options for
the parser (i.e. Fortran::parser::Options). Instead,
Frotran::parser::options are hard-coded to:

```
std::vector<std::string> searchDirectories{"."s};
searchDirectories = searchDirectories;
isFixedForm = false;
_encoding(Fortran::parser::Encoding::UTF_8);
```

These default settings are compatible with the current Flang driver. Further
work is required in order for CompilerInvocation to read and map
clang::driver::options to Fortran::parser::options.

Co-authored-by: Andrzej Warzynski <andrzej.warzynski@arm.com>

Differential Revision: https://reviews.llvm.org/D88381
2020-11-02 14:03:35 +00:00

70 lines
2.1 KiB
C++

//===--- FrontendAction.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Frontend/FrontendAction.h"
#include "flang/Frontend/CompilerInstance.h"
#include "flang/Frontend/FrontendActions.h"
#include "llvm/Support/Errc.h"
using namespace Fortran::frontend;
void FrontendAction::set_currentInput(const FrontendInputFile &currentInput) {
this->currentInput_ = currentInput;
}
// Call this method if BeginSourceFile fails.
// Deallocate compiler instance, input and output descriptors
static void BeginSourceFileCleanUp(FrontendAction &fa, CompilerInstance &ci) {
ci.ClearOutputFiles(/*EraseFiles=*/true);
fa.set_currentInput(FrontendInputFile());
fa.set_instance(nullptr);
}
bool FrontendAction::BeginSourceFile(
CompilerInstance &ci, const FrontendInputFile &realInput) {
FrontendInputFile input(realInput);
assert(!instance_ && "Already processing a source file!");
assert(!realInput.IsEmpty() && "Unexpected empty filename!");
set_currentInput(realInput);
set_instance(&ci);
if (!ci.HasAllSources()) {
BeginSourceFileCleanUp(*this, ci);
return false;
}
return true;
}
bool FrontendAction::ShouldEraseOutputFiles() {
return instance().diagnostics().hasErrorOccurred();
}
llvm::Error FrontendAction::Execute() {
std::string currentInputPath{GetCurrentFileOrBufferName()};
Fortran::parser::Options parserOptions =
this->instance().invocation().fortranOpts();
this->instance().parsing().Prescan(currentInputPath, parserOptions);
ExecuteAction();
return llvm::Error::success();
}
void FrontendAction::EndSourceFile() {
CompilerInstance &ci = instance();
// Cleanup the output streams, and erase the output files if instructed by the
// FrontendAction.
ci.ClearOutputFiles(/*EraseFiles=*/ShouldEraseOutputFiles());
set_instance(nullptr);
set_currentInput(FrontendInputFile());
}