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"
|
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"
|
2020-12-08 16:27:46 +00:00
|
|
|
#include "flang/Semantics/semantics.h"
|
2020-10-24 12:33:19 +01:00
|
|
|
|
|
|
|
using namespace Fortran::frontend;
|
|
|
|
|
|
|
|
void InputOutputTestAction::ExecuteAction() {
|
|
|
|
|
|
|
|
// Get the name of the file from FrontendInputFile current.
|
|
|
|
std::string path{GetCurrentFileOrBufferName()};
|
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream error_stream{buf};
|
|
|
|
bool binaryMode = true;
|
|
|
|
|
|
|
|
// Set/store input file info into CompilerInstance.
|
2020-10-28 10:47:48 +00:00
|
|
|
CompilerInstance &ci = instance();
|
|
|
|
Fortran::parser::AllSources &allSources{ci.allSources()};
|
2020-10-24 12:33:19 +01:00
|
|
|
const Fortran::parser::SourceFile *sf;
|
2021-01-26 13:57:44 -08:00
|
|
|
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();
|
|
|
|
|
|
|
|
// Output file descriptor to receive the content of input file.
|
|
|
|
std::unique_ptr<llvm::raw_ostream> os;
|
|
|
|
|
|
|
|
// Do not write on the output file if using outputStream_.
|
|
|
|
if (ci.IsOutputStreamNull()) {
|
|
|
|
os = ci.CreateDefaultOutputFile(
|
|
|
|
binaryMode, GetCurrentFileOrBufferName(), "txt");
|
|
|
|
if (!os)
|
|
|
|
return;
|
|
|
|
(*os) << fileContent.data();
|
|
|
|
} else {
|
|
|
|
ci.WriteOutputStream(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
|
|
|
|
|
|
|
void ParseSyntaxOnlyAction::ExecuteAction() {
|
|
|
|
CompilerInstance &ci = this->instance();
|
|
|
|
|
|
|
|
// TODO: These should be specifiable by users. For now just use the defaults.
|
|
|
|
common::LanguageFeatureControl features;
|
|
|
|
Fortran::common::IntrinsicTypeDefaultKinds defaultKinds;
|
|
|
|
|
2021-01-06 09:54:30 +00:00
|
|
|
// Parse. In case of failure, report and return.
|
2020-12-08 16:27:46 +00:00
|
|
|
ci.parsing().Parse(llvm::outs());
|
2021-01-06 09:54:30 +00:00
|
|
|
|
|
|
|
if (ci.parsing().messages().AnyFatalError()) {
|
|
|
|
unsigned diagID = ci.diagnostics().getCustomDiagID(
|
2021-01-06 10:35:00 +00:00
|
|
|
clang::DiagnosticsEngine::Error, "Could not parse %0");
|
2021-01-06 09:54:30 +00:00
|
|
|
ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
|
|
|
|
|
|
|
|
ci.parsing().messages().Emit(
|
|
|
|
llvm::errs(), this->instance().allCookedSources());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-27 09:45:56 +00:00
|
|
|
// Report the diagnostics from parsing
|
|
|
|
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
|
|
|
|
|
2020-12-08 16:27:46 +00:00
|
|
|
auto &parseTree{*ci.parsing().parseTree()};
|
|
|
|
|
|
|
|
// Prepare semantics
|
|
|
|
Fortran::semantics::SemanticsContext semanticsContext{
|
|
|
|
defaultKinds, features, ci.allCookedSources()};
|
|
|
|
Fortran::semantics::Semantics semantics{
|
|
|
|
semanticsContext, parseTree, ci.parsing().cooked().AsCharBlock()};
|
|
|
|
|
|
|
|
// Run semantic checks
|
|
|
|
semantics.Perform();
|
|
|
|
|
|
|
|
// Report the diagnostics from the semantic checks
|
|
|
|
semantics.EmitMessages(ci.semaOutputStream());
|
|
|
|
|
|
|
|
if (semantics.AnyFatalError()) {
|
|
|
|
unsigned DiagID = ci.diagnostics().getCustomDiagID(
|
2021-01-06 10:35:00 +00:00
|
|
|
clang::DiagnosticsEngine::Error, "Semantic errors in %0");
|
2020-12-08 16:27:46 +00:00
|
|
|
ci.diagnostics().Report(DiagID) << GetCurrentFileOrBufferName();
|
|
|
|
}
|
|
|
|
}
|
[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);
|
|
|
|
}
|