2020-10-24 12:33:19 +01:00
|
|
|
//===--- FrontendActions.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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-10-27 12:26:47 +00:00
|
|
|
|
2020-10-24 12:33:19 +01:00
|
|
|
#include "flang/Frontend/FrontendActions.h"
|
2020-12-08 16:27:46 +00:00
|
|
|
#include "flang/Common/default-kinds.h"
|
2020-10-24 12:33:19 +01:00
|
|
|
#include "flang/Frontend/CompilerInstance.h"
|
2021-02-04 11:14:57 +00:00
|
|
|
#include "flang/Frontend/FrontendOptions.h"
|
2021-02-17 18:53:05 +00:00
|
|
|
#include "flang/Lower/PFTBuilder.h"
|
2021-02-17 15:55:56 +00:00
|
|
|
#include "flang/Parser/dump-parse-tree.h"
|
2020-10-27 12:26:47 +00:00
|
|
|
#include "flang/Parser/parsing.h"
|
|
|
|
#include "flang/Parser/provenance.h"
|
2020-10-24 12:33:19 +01:00
|
|
|
#include "flang/Parser/source.h"
|
2021-02-04 11:14:57 +00:00
|
|
|
#include "flang/Parser/unparse.h"
|
2020-12-08 16:27:46 +00:00
|
|
|
#include "flang/Semantics/semantics.h"
|
2021-02-04 11:14:57 +00:00
|
|
|
#include "flang/Semantics/unparse-with-symbols.h"
|
2021-02-17 15:55:56 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include <clang/Basic/Diagnostic.h>
|
|
|
|
#include <memory>
|
2020-10-24 12:33:19 +01:00
|
|
|
|
|
|
|
using namespace Fortran::frontend;
|
|
|
|
|
2021-02-17 18:53:05 +00:00
|
|
|
/// Report fatal semantic errors if present.
|
|
|
|
///
|
|
|
|
/// \param semantics The semantics instance
|
|
|
|
/// \param diags The diagnostics engine instance
|
|
|
|
/// \param bufferName The file or buffer name
|
|
|
|
///
|
|
|
|
/// \return True if fatal semantic errors are present, false if not
|
|
|
|
bool reportFatalSemanticErrors(const Fortran::semantics::Semantics &semantics,
|
2021-02-17 15:55:56 +00:00
|
|
|
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;
|
2021-02-17 18:53:05 +00:00
|
|
|
return true;
|
2021-02-17 15:55:56 +00:00
|
|
|
}
|
2021-02-17 18:53:05 +00:00
|
|
|
return false;
|
2021-02-17 15:55:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 13:06:43 +00:00
|
|
|
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);
|
2020-10-24 12:33:19 +01:00
|
|
|
|
2021-02-04 13:06:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-04 11:14:57 +00:00
|
|
|
bool PrescanAndSemaAction::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse. In case of failure, report and return.
|
|
|
|
ci.parsing().Parse(llvm::outs());
|
|
|
|
|
|
|
|
if (ci.parsing().messages().AnyFatalError()) {
|
|
|
|
unsigned diagID = ci.diagnostics().getCustomDiagID(
|
|
|
|
clang::DiagnosticsEngine::Error, "Could not parse %0");
|
|
|
|
ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
|
|
|
|
|
|
|
|
ci.parsing().messages().Emit(
|
|
|
|
llvm::errs(), this->instance().allCookedSources());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report the diagnostics from parsing
|
|
|
|
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
|
|
|
|
|
|
|
|
auto &parseTree{*ci.parsing().parseTree()};
|
|
|
|
|
|
|
|
// Prepare semantics
|
2021-02-17 15:55:56 +00:00
|
|
|
setSemantics(std::make_unique<Fortran::semantics::Semantics>(
|
|
|
|
ci.invocation().semanticsContext(), parseTree,
|
|
|
|
ci.parsing().cooked().AsCharBlock()));
|
|
|
|
auto &semantics = this->semantics();
|
2021-02-04 11:14:57 +00:00
|
|
|
|
|
|
|
// Run semantic checks
|
|
|
|
semantics.Perform();
|
|
|
|
|
|
|
|
// Report the diagnostics from the semantic checks
|
|
|
|
semantics.EmitMessages(ci.semaOutputStream());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:06:43 +00:00
|
|
|
void InputOutputTestAction::ExecuteAction() {
|
2021-02-04 16:23:42 +00:00
|
|
|
CompilerInstance &ci = instance();
|
|
|
|
|
|
|
|
// Create a stream for errors
|
2020-10-24 12:33:19 +01:00
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream error_stream{buf};
|
|
|
|
|
2021-02-04 16:23:42 +00:00
|
|
|
// Read the input file
|
2020-10-28 10:47:48 +00:00
|
|
|
Fortran::parser::AllSources &allSources{ci.allSources()};
|
2021-02-04 16:23:42 +00:00
|
|
|
std::string path{GetCurrentFileOrBufferName()};
|
2020-10-24 12:33:19 +01:00
|
|
|
const Fortran::parser::SourceFile *sf;
|
2021-02-04 16:23:42 +00:00
|
|
|
if (path == "-")
|
|
|
|
sf = allSources.ReadStandardInput(error_stream);
|
|
|
|
else
|
|
|
|
sf = allSources.Open(path, error_stream, std::optional<std::string>{"."s});
|
2020-10-24 12:33:19 +01:00
|
|
|
llvm::ArrayRef<char> fileContent = sf->content();
|
|
|
|
|
2021-02-04 16:23:42 +00:00
|
|
|
// Output file descriptor to receive the contents of the input file.
|
2020-10-24 12:33:19 +01:00
|
|
|
std::unique_ptr<llvm::raw_ostream> os;
|
|
|
|
|
2021-02-04 16:23:42 +00:00
|
|
|
// Copy the contents from the input file to the output file
|
|
|
|
if (!ci.IsOutputStreamNull()) {
|
|
|
|
// An output stream (outputStream_) was set earlier
|
|
|
|
ci.WriteOutputStream(fileContent.data());
|
|
|
|
} else {
|
|
|
|
// No pre-set output stream - create an output file
|
2020-10-24 12:33:19 +01:00
|
|
|
os = ci.CreateDefaultOutputFile(
|
2021-02-04 16:23:42 +00:00
|
|
|
/*binary=*/true, GetCurrentFileOrBufferName(), "txt");
|
2020-10-24 12:33:19 +01:00
|
|
|
if (!os)
|
|
|
|
return;
|
|
|
|
(*os) << fileContent.data();
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 12:26:47 +00:00
|
|
|
|
|
|
|
void PrintPreprocessedAction::ExecuteAction() {
|
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream outForPP{buf};
|
|
|
|
|
|
|
|
// Run the preprocessor
|
|
|
|
CompilerInstance &ci = this->instance();
|
|
|
|
ci.parsing().DumpCookedChars(outForPP);
|
|
|
|
|
|
|
|
// If a pre-defined output stream exists, dump the preprocessed content there
|
|
|
|
if (!ci.IsOutputStreamNull()) {
|
|
|
|
// Send the output to the pre-defined output buffer.
|
|
|
|
ci.WriteOutputStream(outForPP.str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-21 15:02:22 +00:00
|
|
|
// Print diagnostics from the preprocessor
|
|
|
|
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
|
|
|
|
|
2020-10-27 12:26:47 +00:00
|
|
|
// Create a file and save the preprocessed output there
|
|
|
|
if (auto os{ci.CreateDefaultOutputFile(
|
|
|
|
/*Binary=*/true, /*InFile=*/GetCurrentFileOrBufferName())}) {
|
|
|
|
(*os) << outForPP.str();
|
|
|
|
} else {
|
|
|
|
llvm::errs() << "Unable to create the output file\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 16:27:46 +00:00
|
|
|
|
2021-02-17 15:55:56 +00:00
|
|
|
void DebugDumpProvenanceAction::ExecuteAction() {
|
|
|
|
this->instance().parsing().DumpProvenance(llvm::outs());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParseSyntaxOnlyAction::ExecuteAction() {
|
|
|
|
reportFatalSemanticErrors(semantics(), this->instance().diagnostics(),
|
|
|
|
GetCurrentFileOrBufferName());
|
|
|
|
}
|
2021-01-06 09:54:30 +00:00
|
|
|
|
2021-02-04 11:14:57 +00:00
|
|
|
void DebugUnparseAction::ExecuteAction() {
|
|
|
|
auto &parseTree{instance().parsing().parseTree()};
|
|
|
|
Fortran::parser::AnalyzedObjectsAsFortran asFortran =
|
|
|
|
Fortran::frontend::getBasicAsFortran();
|
2020-12-08 16:27:46 +00:00
|
|
|
|
2021-02-04 11:14:57 +00:00
|
|
|
// TODO: Options should come from CompilerInvocation
|
|
|
|
Unparse(llvm::outs(), *parseTree,
|
|
|
|
/*encoding=*/Fortran::parser::Encoding::UTF_8,
|
|
|
|
/*capitalizeKeywords=*/true, /*backslashEscapes=*/false,
|
|
|
|
/*preStatement=*/nullptr, &asFortran);
|
2021-02-17 15:55:56 +00:00
|
|
|
|
|
|
|
// Report fatal semantic errors
|
|
|
|
reportFatalSemanticErrors(semantics(), this->instance().diagnostics(),
|
|
|
|
GetCurrentFileOrBufferName());
|
2021-02-04 11:14:57 +00:00
|
|
|
}
|
2020-12-08 16:27:46 +00:00
|
|
|
|
2021-02-04 11:14:57 +00:00
|
|
|
void DebugUnparseWithSymbolsAction::ExecuteAction() {
|
|
|
|
auto &parseTree{*instance().parsing().parseTree()};
|
2020-12-08 16:27:46 +00:00
|
|
|
|
2021-02-04 11:14:57 +00:00
|
|
|
Fortran::semantics::UnparseWithSymbols(
|
|
|
|
llvm::outs(), parseTree, /*encoding=*/Fortran::parser::Encoding::UTF_8);
|
2021-02-17 15:55:56 +00:00
|
|
|
|
|
|
|
// Report fatal semantic errors
|
|
|
|
reportFatalSemanticErrors(semantics(), this->instance().diagnostics(),
|
|
|
|
GetCurrentFileOrBufferName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DebugDumpSymbolsAction::ExecuteAction() {
|
|
|
|
auto &semantics = this->semantics();
|
|
|
|
|
|
|
|
// 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());
|
2020-12-08 16:27:46 +00:00
|
|
|
}
|
[flang][driver] Add support for `-c` and `-emit-obj`
This patch adds a frontend action for emitting object files. While Flang
does not support code-generation, this action remains a placeholder.
This patch simply provides glue-code to connect the compiler driver
with the appropriate frontend action.
The new action is triggered with the `-c` compiler driver flag, i.e.
`flang-new -c`. This is then translated to `flang-new -fc1 -emit-obj`,
so `-emit-obj` has to be marked as supported as well.
As code-generation is not available yet, `flang-new -c` results in a
driver error:
```
error: code-generation is not available yet
```
Hopefully this will help communicating the level of available
functionality within Flang.
The definition of `emit-obj` is updated so that it can be shared between
Clang and Flang. As the original definition was enclosed within a
Clang-specific TableGen `let` statement, it is extracted into a new `let`
statement. That felt like the cleanest option.
I also commented out `-triple` in Flang::ConstructJob and updated some
comments there. This is similar to https://reviews.llvm.org/D93027. I
wanted to make sure that it's clear that we can't support `-triple`
until we have code-generation. However, once code-generation is
available we _will need_ `-triple`.
As this patch adds `-emit-obj`, the emit-obj.f90 becomes irrelevant and
is deleted. Instead, phases.f90 is added to demonstrate that users can
control compilation phases (indeed, `-c` is a phase control flag).
Reviewed By: SouraVX, clementval
Differential Revision: https://reviews.llvm.org/D93301
2021-01-07 09:08:54 +00:00
|
|
|
|
2021-02-17 18:53:05 +00:00
|
|
|
void DebugMeasureParseTreeAction::ExecuteAction() {
|
|
|
|
CompilerInstance &ci = this->instance();
|
|
|
|
|
|
|
|
// Parse. In case of failure, report and return.
|
|
|
|
ci.parsing().Parse(llvm::outs());
|
|
|
|
|
|
|
|
if (ci.parsing().messages().AnyFatalError()) {
|
|
|
|
unsigned diagID = ci.diagnostics().getCustomDiagID(
|
|
|
|
clang::DiagnosticsEngine::Error, "Could not parse %0");
|
|
|
|
ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
|
|
|
|
|
|
|
|
ci.parsing().messages().Emit(
|
|
|
|
llvm::errs(), this->instance().allCookedSources());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report the diagnostics from parsing
|
|
|
|
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
|
|
|
|
|
|
|
|
auto &parseTree{*ci.parsing().parseTree()};
|
|
|
|
|
|
|
|
// Measure the parse tree
|
|
|
|
MeasurementVisitor visitor;
|
|
|
|
Fortran::parser::Walk(parseTree, visitor);
|
|
|
|
llvm::outs() << "Parse tree comprises " << visitor.objects
|
|
|
|
<< " objects and occupies " << visitor.bytes
|
|
|
|
<< " total bytes.\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void DebugPreFIRTreeAction::ExecuteAction() {
|
|
|
|
CompilerInstance &ci = this->instance();
|
|
|
|
// Report and exit if fatal semantic errors are present
|
|
|
|
if (reportFatalSemanticErrors(
|
|
|
|
semantics(), ci.diagnostics(), GetCurrentFileOrBufferName())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &parseTree{*ci.parsing().parseTree()};
|
|
|
|
|
|
|
|
// Dump pre-FIR tree
|
|
|
|
if (auto ast{Fortran::lower::createPFT(
|
|
|
|
parseTree, ci.invocation().semanticsContext())}) {
|
|
|
|
Fortran::lower::dumpPFT(llvm::outs(), *ast);
|
|
|
|
} else {
|
|
|
|
unsigned diagID = ci.diagnostics().getCustomDiagID(
|
|
|
|
clang::DiagnosticsEngine::Error, "Pre FIR Tree is NULL.");
|
|
|
|
ci.diagnostics().Report(diagID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[flang][driver] Add support for `-c` and `-emit-obj`
This patch adds a frontend action for emitting object files. While Flang
does not support code-generation, this action remains a placeholder.
This patch simply provides glue-code to connect the compiler driver
with the appropriate frontend action.
The new action is triggered with the `-c` compiler driver flag, i.e.
`flang-new -c`. This is then translated to `flang-new -fc1 -emit-obj`,
so `-emit-obj` has to be marked as supported as well.
As code-generation is not available yet, `flang-new -c` results in a
driver error:
```
error: code-generation is not available yet
```
Hopefully this will help communicating the level of available
functionality within Flang.
The definition of `emit-obj` is updated so that it can be shared between
Clang and Flang. As the original definition was enclosed within a
Clang-specific TableGen `let` statement, it is extracted into a new `let`
statement. That felt like the cleanest option.
I also commented out `-triple` in Flang::ConstructJob and updated some
comments there. This is similar to https://reviews.llvm.org/D93027. I
wanted to make sure that it's clear that we can't support `-triple`
until we have code-generation. However, once code-generation is
available we _will need_ `-triple`.
As this patch adds `-emit-obj`, the emit-obj.f90 becomes irrelevant and
is deleted. Instead, phases.f90 is added to demonstrate that users can
control compilation phases (indeed, `-c` is a phase control flag).
Reviewed By: SouraVX, clementval
Differential Revision: https://reviews.llvm.org/D93301
2021-01-07 09:08:54 +00:00
|
|
|
void EmitObjAction::ExecuteAction() {
|
|
|
|
CompilerInstance &ci = this->instance();
|
|
|
|
unsigned DiagID = ci.diagnostics().getCustomDiagID(
|
|
|
|
clang::DiagnosticsEngine::Error, "code-generation is not available yet");
|
|
|
|
ci.diagnostics().Report(DiagID);
|
|
|
|
}
|