2020-09-11 10:17:31 +01:00
|
|
|
//===--- ExecuteCompilerInvocation.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
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// This file holds ExecuteCompilerInvocation(). It is split into its own file to
|
|
|
|
|
// minimize the impact of pulling in essentially everything else in Flang.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2022-04-29 17:36:26 +00:00
|
|
|
//
|
|
|
|
|
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-09-11 10:17:31 +01:00
|
|
|
|
|
|
|
|
#include "flang/Frontend/CompilerInstance.h"
|
2020-10-24 12:33:19 +01:00
|
|
|
#include "flang/Frontend/FrontendActions.h"
|
2021-08-12 11:42:08 +01:00
|
|
|
#include "flang/Frontend/FrontendPluginRegistry.h"
|
2022-04-29 17:36:26 +00:00
|
|
|
|
|
|
|
|
#include "mlir/IR/MLIRContext.h"
|
|
|
|
|
#include "mlir/Pass/PassManager.h"
|
2020-09-11 10:17:31 +01:00
|
|
|
#include "clang/Driver/Options.h"
|
|
|
|
|
#include "llvm/Option/OptTable.h"
|
2020-10-24 12:33:19 +01:00
|
|
|
#include "llvm/Option/Option.h"
|
|
|
|
|
#include "llvm/Support/BuryPointer.h"
|
2020-09-11 10:17:31 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
|
|
|
|
|
|
namespace Fortran::frontend {
|
2020-10-24 12:33:19 +01:00
|
|
|
|
2022-04-29 17:36:26 +00:00
|
|
|
static std::unique_ptr<FrontendAction>
|
|
|
|
|
createFrontendAction(CompilerInstance &ci) {
|
2020-10-24 12:33:19 +01:00
|
|
|
|
2022-04-29 17:36:26 +00:00
|
|
|
switch (ci.getFrontendOpts().programAction) {
|
2020-10-24 12:33:19 +01:00
|
|
|
case InputOutputTest:
|
|
|
|
|
return std::make_unique<InputOutputTestAction>();
|
2020-10-27 12:26:47 +00:00
|
|
|
case PrintPreprocessedInput:
|
|
|
|
|
return std::make_unique<PrintPreprocessedAction>();
|
2020-12-08 16:27:46 +00:00
|
|
|
case ParseSyntaxOnly:
|
|
|
|
|
return std::make_unique<ParseSyntaxOnlyAction>();
|
2022-02-03 17:00:27 +00:00
|
|
|
case EmitMLIR:
|
|
|
|
|
return std::make_unique<EmitMLIRAction>();
|
2022-02-04 17:15:12 +00:00
|
|
|
case EmitLLVM:
|
|
|
|
|
return std::make_unique<EmitLLVMAction>();
|
2022-04-06 11:59:28 +00:00
|
|
|
case EmitLLVMBitcode:
|
|
|
|
|
return std::make_unique<EmitLLVMBitcodeAction>();
|
[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
|
|
|
case EmitObj:
|
2022-04-22 09:07:31 +00:00
|
|
|
return std::make_unique<EmitObjAction>();
|
2022-02-24 17:34:27 +00:00
|
|
|
case EmitAssembly:
|
2022-04-22 09:07:31 +00:00
|
|
|
return std::make_unique<EmitAssemblyAction>();
|
2021-02-04 11:14:57 +00:00
|
|
|
case DebugUnparse:
|
|
|
|
|
return std::make_unique<DebugUnparseAction>();
|
2021-03-30 10:35:42 +00:00
|
|
|
case DebugUnparseNoSema:
|
|
|
|
|
return std::make_unique<DebugUnparseNoSemaAction>();
|
2021-02-04 11:14:57 +00:00
|
|
|
case DebugUnparseWithSymbols:
|
|
|
|
|
return std::make_unique<DebugUnparseWithSymbolsAction>();
|
2021-02-17 15:55:56 +00:00
|
|
|
case DebugDumpSymbols:
|
|
|
|
|
return std::make_unique<DebugDumpSymbolsAction>();
|
|
|
|
|
case DebugDumpParseTree:
|
|
|
|
|
return std::make_unique<DebugDumpParseTreeAction>();
|
2022-03-08 10:01:55 +00:00
|
|
|
case DebugDumpPFT:
|
|
|
|
|
return std::make_unique<DebugDumpPFTAction>();
|
2021-03-30 10:35:42 +00:00
|
|
|
case DebugDumpParseTreeNoSema:
|
|
|
|
|
return std::make_unique<DebugDumpParseTreeNoSemaAction>();
|
2021-06-15 15:30:23 +00:00
|
|
|
case DebugDumpAll:
|
|
|
|
|
return std::make_unique<DebugDumpAllAction>();
|
2021-02-17 15:55:56 +00:00
|
|
|
case DebugDumpProvenance:
|
|
|
|
|
return std::make_unique<DebugDumpProvenanceAction>();
|
2021-02-23 17:59:17 +00:00
|
|
|
case DebugDumpParsingLog:
|
|
|
|
|
return std::make_unique<DebugDumpParsingLogAction>();
|
2021-02-17 18:53:05 +00:00
|
|
|
case DebugMeasureParseTree:
|
|
|
|
|
return std::make_unique<DebugMeasureParseTreeAction>();
|
|
|
|
|
case DebugPreFIRTree:
|
|
|
|
|
return std::make_unique<DebugPreFIRTreeAction>();
|
2021-04-14 10:43:14 +00:00
|
|
|
case GetDefinition:
|
|
|
|
|
return std::make_unique<GetDefinitionAction>();
|
2021-03-08 16:54:11 +00:00
|
|
|
case GetSymbolsSources:
|
|
|
|
|
return std::make_unique<GetSymbolsSourcesAction>();
|
2021-06-07 15:40:26 +01:00
|
|
|
case InitOnly:
|
|
|
|
|
return std::make_unique<InitOnlyAction>();
|
2021-08-12 11:42:08 +01:00
|
|
|
case PluginAction: {
|
|
|
|
|
for (const FrontendPluginRegistry::entry &plugin :
|
|
|
|
|
FrontendPluginRegistry::entries()) {
|
2022-04-29 17:36:26 +00:00
|
|
|
if (plugin.getName() == ci.getFrontendOpts().actionName) {
|
2021-08-12 11:42:08 +01:00
|
|
|
std::unique_ptr<PluginParseTreeAction> p(plugin.instantiate());
|
|
|
|
|
return std::move(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-29 17:36:26 +00:00
|
|
|
unsigned diagID = ci.getDiagnostics().getCustomDiagID(
|
2021-08-12 11:42:08 +01:00
|
|
|
clang::DiagnosticsEngine::Error, "unable to find plugin '%0'");
|
2022-04-29 17:36:26 +00:00
|
|
|
ci.getDiagnostics().Report(diagID) << ci.getFrontendOpts().actionName;
|
2021-08-12 11:42:08 +01:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-10-24 12:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-22 10:59:31 +00:00
|
|
|
llvm_unreachable("Invalid program action!");
|
2020-10-24 12:33:19 +01:00
|
|
|
}
|
2021-08-12 11:42:08 +01:00
|
|
|
|
2022-04-29 17:36:26 +00:00
|
|
|
bool executeCompilerInvocation(CompilerInstance *flang) {
|
2020-09-11 10:17:31 +01:00
|
|
|
// Honor -help.
|
2022-04-29 17:36:26 +00:00
|
|
|
if (flang->getFrontendOpts().showHelp) {
|
2021-06-24 14:47:03 -07:00
|
|
|
clang::driver::getDriverOptTable().printHelp(llvm::outs(),
|
2020-09-11 10:17:31 +01:00
|
|
|
"flang-new -fc1 [options] file...", "LLVM 'Flang' Compiler",
|
2020-09-22 19:08:18 +01:00
|
|
|
/*Include=*/clang::driver::options::FC1Option,
|
2020-10-24 12:33:19 +01:00
|
|
|
/*Exclude=*/llvm::opt::DriverFlag::HelpHidden,
|
|
|
|
|
/*ShowAllAliases=*/false);
|
2020-09-11 10:17:31 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Honor -version.
|
2022-04-29 17:36:26 +00:00
|
|
|
if (flang->getFrontendOpts().showVersion) {
|
2020-09-11 10:17:31 +01:00
|
|
|
llvm::cl::PrintVersionMessage();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 11:42:08 +01:00
|
|
|
// Load any requested plugins.
|
2022-04-29 17:36:26 +00:00
|
|
|
for (const std::string &path : flang->getFrontendOpts().plugins) {
|
|
|
|
|
std::string error;
|
|
|
|
|
if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(path.c_str(),
|
|
|
|
|
&error)) {
|
|
|
|
|
unsigned diagID = flang->getDiagnostics().getCustomDiagID(
|
2021-08-12 11:42:08 +01:00
|
|
|
clang::DiagnosticsEngine::Error, "unable to load plugin '%0': '%1'");
|
2022-04-29 17:36:26 +00:00
|
|
|
flang->getDiagnostics().Report(diagID) << path << error;
|
2021-08-12 11:42:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 15:40:16 +00:00
|
|
|
// Honor -mllvm. This should happen AFTER plugins have been loaded!
|
2022-04-29 17:36:26 +00:00
|
|
|
if (!flang->getFrontendOpts().llvmArgs.empty()) {
|
|
|
|
|
unsigned numArgs = flang->getFrontendOpts().llvmArgs.size();
|
2022-03-02 15:40:16 +00:00
|
|
|
auto args = std::make_unique<const char *[]>(numArgs + 2);
|
|
|
|
|
args[0] = "flang (LLVM option parsing)";
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != numArgs; ++i)
|
2022-04-29 17:36:26 +00:00
|
|
|
args[i + 1] = flang->getFrontendOpts().llvmArgs[i].c_str();
|
2022-03-02 15:40:16 +00:00
|
|
|
|
|
|
|
|
args[numArgs + 1] = nullptr;
|
|
|
|
|
llvm::cl::ParseCommandLineOptions(numArgs + 1, args.get());
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 09:47:23 +00:00
|
|
|
// Honor -mmlir. This should happen AFTER plugins have been loaded!
|
2022-04-29 17:36:26 +00:00
|
|
|
if (!flang->getFrontendOpts().mlirArgs.empty()) {
|
2022-04-07 09:47:23 +00:00
|
|
|
mlir::registerMLIRContextCLOptions();
|
|
|
|
|
mlir::registerPassManagerCLOptions();
|
2022-04-29 17:36:26 +00:00
|
|
|
unsigned numArgs = flang->getFrontendOpts().mlirArgs.size();
|
2022-04-07 09:47:23 +00:00
|
|
|
auto args = std::make_unique<const char *[]>(numArgs + 2);
|
|
|
|
|
args[0] = "flang (MLIR option parsing)";
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != numArgs; ++i)
|
2022-04-29 17:36:26 +00:00
|
|
|
args[i + 1] = flang->getFrontendOpts().mlirArgs[i].c_str();
|
2022-04-07 09:47:23 +00:00
|
|
|
|
|
|
|
|
args[numArgs + 1] = nullptr;
|
|
|
|
|
llvm::cl::ParseCommandLineOptions(numArgs + 1, args.get());
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 11:42:08 +01:00
|
|
|
// If there were errors in processing arguments, don't do anything else.
|
2022-04-29 17:36:26 +00:00
|
|
|
if (flang->getDiagnostics().hasErrorOccurred()) {
|
2021-08-12 11:42:08 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
[flang][Driver] Refine _when_ driver diagnostics are formatted
This patch refines //when// driver diagnostics are formatted so that
`flang-new` and `flang-new -fc1` behave consistently with `clang` and
`clang -cc1`, respectively. This change only applies to driver diagnostics.
Scanning, parsing and semantic diagnostics are separate and not covered here.
**NEW BEHAVIOUR**
To illustrate the new behaviour, consider the following input file:
```! file.f90
program m
integer :: i = k
end
```
In the following invocations, "error: Semantic errors in file.f90" _will be_
formatted:
```
$ flang-new file.f90
error: Semantic errors in file.f90
./file.f90:2:18: error: Must be a constant value
integer :: i = k
$ flang-new -fc1 -fcolor-diagnostics file.f90
error: Semantic errors in file.f90
./file.f90:2:18: error: Must be a constant value
integer :: i = k
```
However, in the following invocations, "error: Semantic errors in file.f90"
_will not be_ formatted:
```
$ flang-new -fno-color-diagnostics file.f90
error: Semantic errors in file.f90
./file.f90:2:18: error: Must be a constant value
integer :: i = k
$ flang-new -fc1 file.f90
error: Semantic errors in file.f90
./file.f90:2:18: error: Must be a constant value
integer :: i = k
```
Before this change, none of the above would be formatted. Note also that the
default behaviour in `flang-new` is different to `flang-new -fc1` (this is
consistent with Clang).
**NOTES ON IMPLEMENTATION**
Note that the diagnostic options are parsed in `createAndPopulateDiagOpt`s in
driver.cpp. That's where the driver's `DiagnosticEngine` options are set. Like
most command-line compiler driver options, these flags are "claimed" in
Flang.cpp (i.e. when creating a frontend driver invocation) by calling
`getLastArg` rather than in driver.cpp.
In Clang's Options.td, `defm color_diagnostics` is replaced with two separate
definitions: `def fcolor_diagnostics` and def fno_color_diagnostics`. That's
because originally `color_diagnostics` derived from `OptInCC1FFlag`, which is a
multiclass for opt-in options in CC1. In order to preserve the current
behaviour in `clang -cc1` (i.e. to keep `-fno-color-diagnostics` unavailable in
`clang -cc1`) and to implement similar behaviour in `flang-new -fc1`, we can't
re-use `OptInCC1FFlag`.
Formatting is only available in consoles that support it and will normally mean that
the message is printed in bold + color.
Co-authored-by: Andrzej Warzynski <andrzej.warzynski@arm.com>
Reviewed By: rovka
Differential Revision: https://reviews.llvm.org/D126164
2022-06-22 23:56:34 +08:00
|
|
|
// Honor color diagnostics.
|
|
|
|
|
flang->getDiagnosticOpts().ShowColors = flang->getFrontendOpts().showColors;
|
|
|
|
|
|
2020-10-24 12:33:19 +01:00
|
|
|
// Create and execute the frontend action.
|
2022-04-29 17:36:26 +00:00
|
|
|
std::unique_ptr<FrontendAction> act(createFrontendAction(*flang));
|
2020-10-24 12:33:19 +01:00
|
|
|
if (!act)
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-04-29 17:36:26 +00:00
|
|
|
bool success = flang->executeAction(*act);
|
2020-10-24 12:33:19 +01:00
|
|
|
return success;
|
2020-09-11 10:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Fortran::frontend
|