2020-09-11 10:17:31 +01:00
|
|
|
//===- CompilerInvocation.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/CompilerInvocation.h"
|
2021-02-10 09:24:45 +00:00
|
|
|
#include "flang/Common/Fortran-features.h"
|
2021-01-06 15:42:24 +00:00
|
|
|
#include "flang/Frontend/PreprocessorOptions.h"
|
[flang][driver] Add support for `--target`/`--triple`
This patch adds support for:
* `--target` in the compiler driver (`flang-new`)
* `--triple` in the frontend driver (`flang-new -fc1`)
The semantics of these flags are inherited from `clangDriver`, i.e.
consistent with `clang --target` and `clang -cc1 --triple`,
respectively.
A new structure is defined, `TargetOptions`, that will hold various
Frontend options related to the target. Currently, this is mostly a
placeholder that contains the target triple. In the future, it will be
used for storing e.g. the CPU to tune for or the target features to
enable.
Additionally, the following target/triple related options are enabled
[*]: `-print-effective-triple`, `-print-target-triple`. Definitions in
Options.td are updated accordingly and, to facilated testing,
`-emit-llvm` is added to the list of options available in `flang-new`
(previously it was only enabled in `flang-new -fc1`).
[*] These options were actually available before (like all other options
defined in `clangDriver`), but not included in `flang-new --help`.
Before this change, `flang-new` would just use `native` for defining the
target, so these options were of little value.
Differential Revision: https://reviews.llvm.org/D120246
2022-02-21 11:51:32 +00:00
|
|
|
#include "flang/Frontend/TargetOptions.h"
|
2021-02-10 09:24:45 +00:00
|
|
|
#include "flang/Semantics/semantics.h"
|
2021-01-19 13:15:53 +00:00
|
|
|
#include "flang/Version.inc"
|
2020-09-11 10:17:31 +01:00
|
|
|
#include "clang/Basic/AllDiagnostics.h"
|
|
|
|
#include "clang/Basic/DiagnosticDriver.h"
|
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
|
|
|
#include "clang/Driver/Options.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/OptTable.h"
|
2021-03-23 12:24:57 -04:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2022-01-21 17:05:36 +01:00
|
|
|
#include "llvm/Support/Path.h"
|
2020-10-05 17:42:00 +01:00
|
|
|
#include "llvm/Support/Process.h"
|
2020-09-11 10:17:31 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2021-02-10 09:24:45 +00:00
|
|
|
#include <memory>
|
2020-09-11 10:17:31 +01:00
|
|
|
|
|
|
|
using namespace Fortran::frontend;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Initialization.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
CompilerInvocationBase::CompilerInvocationBase()
|
2021-01-06 15:42:24 +00:00
|
|
|
: diagnosticOpts_(new clang::DiagnosticOptions()),
|
|
|
|
preprocessorOpts_(new PreprocessorOptions()) {}
|
2020-09-11 10:17:31 +01:00
|
|
|
|
|
|
|
CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &x)
|
2021-01-06 15:42:24 +00:00
|
|
|
: diagnosticOpts_(new clang::DiagnosticOptions(x.GetDiagnosticOpts())),
|
|
|
|
preprocessorOpts_(new PreprocessorOptions(x.preprocessorOpts())) {}
|
2020-09-11 10:17:31 +01:00
|
|
|
|
|
|
|
CompilerInvocationBase::~CompilerInvocationBase() = default;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Deserialization (from args)
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-10-05 17:42:00 +01:00
|
|
|
static bool parseShowColorsArgs(
|
|
|
|
const llvm::opt::ArgList &args, bool defaultColor) {
|
|
|
|
// Color diagnostics default to auto ("on" if terminal supports) in the driver
|
|
|
|
// but default to off in cc1, needing an explicit OPT_fdiagnostics_color.
|
|
|
|
// Support both clang's -f[no-]color-diagnostics and gcc's
|
|
|
|
// -f[no-]diagnostics-colors[=never|always|auto].
|
|
|
|
enum {
|
|
|
|
Colors_On,
|
|
|
|
Colors_Off,
|
|
|
|
Colors_Auto
|
|
|
|
} ShowColors = defaultColor ? Colors_Auto : Colors_Off;
|
|
|
|
|
|
|
|
for (auto *a : args) {
|
|
|
|
const llvm::opt::Option &O = a->getOption();
|
|
|
|
if (O.matches(clang::driver::options::OPT_fcolor_diagnostics) ||
|
|
|
|
O.matches(clang::driver::options::OPT_fdiagnostics_color)) {
|
|
|
|
ShowColors = Colors_On;
|
|
|
|
} else if (O.matches(clang::driver::options::OPT_fno_color_diagnostics) ||
|
|
|
|
O.matches(clang::driver::options::OPT_fno_diagnostics_color)) {
|
|
|
|
ShowColors = Colors_Off;
|
|
|
|
} else if (O.matches(clang::driver::options::OPT_fdiagnostics_color_EQ)) {
|
|
|
|
llvm::StringRef value(a->getValue());
|
|
|
|
if (value == "always")
|
|
|
|
ShowColors = Colors_On;
|
|
|
|
else if (value == "never")
|
|
|
|
ShowColors = Colors_Off;
|
|
|
|
else if (value == "auto")
|
|
|
|
ShowColors = Colors_Auto;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ShowColors == Colors_On ||
|
|
|
|
(ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Fortran::frontend::ParseDiagnosticArgs(clang::DiagnosticOptions &opts,
|
|
|
|
llvm::opt::ArgList &args, bool defaultDiagColor) {
|
|
|
|
opts.ShowColors = parseShowColorsArgs(args, defaultDiagColor);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
[flang][driver] Add support for `--target`/`--triple`
This patch adds support for:
* `--target` in the compiler driver (`flang-new`)
* `--triple` in the frontend driver (`flang-new -fc1`)
The semantics of these flags are inherited from `clangDriver`, i.e.
consistent with `clang --target` and `clang -cc1 --triple`,
respectively.
A new structure is defined, `TargetOptions`, that will hold various
Frontend options related to the target. Currently, this is mostly a
placeholder that contains the target triple. In the future, it will be
used for storing e.g. the CPU to tune for or the target features to
enable.
Additionally, the following target/triple related options are enabled
[*]: `-print-effective-triple`, `-print-target-triple`. Definitions in
Options.td are updated accordingly and, to facilated testing,
`-emit-llvm` is added to the list of options available in `flang-new`
(previously it was only enabled in `flang-new -fc1`).
[*] These options were actually available before (like all other options
defined in `clangDriver`), but not included in `flang-new --help`.
Before this change, `flang-new` would just use `native` for defining the
target, so these options were of little value.
Differential Revision: https://reviews.llvm.org/D120246
2022-02-21 11:51:32 +00:00
|
|
|
/// Parses all target input arguments and populates the target
|
|
|
|
/// options accordingly.
|
|
|
|
///
|
|
|
|
/// \param [in] opts The target options instance to update
|
|
|
|
/// \param [in] args The list of input arguments (from the compiler invocation)
|
|
|
|
static void ParseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
|
|
|
|
opts.triple = args.getLastArgValue(clang::driver::options::OPT_triple);
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:59:17 +00:00
|
|
|
// Tweak the frontend configuration based on the frontend action
|
|
|
|
static void setUpFrontendBasedOnAction(FrontendOptions &opts) {
|
2021-07-29 12:39:10 +00:00
|
|
|
assert(opts.programAction != Fortran::frontend::InvalidAction &&
|
2021-02-23 17:59:17 +00:00
|
|
|
"Fortran frontend action not set!");
|
|
|
|
|
2021-07-29 12:39:10 +00:00
|
|
|
if (opts.programAction == DebugDumpParsingLog)
|
|
|
|
opts.instrumentedParse = true;
|
2021-04-07 13:10:35 +00:00
|
|
|
|
2021-07-29 12:39:10 +00:00
|
|
|
if (opts.programAction == DebugDumpProvenance ||
|
|
|
|
opts.programAction == Fortran::frontend::GetDefinition)
|
|
|
|
opts.needProvenanceRangeToCharBlockMappings = true;
|
2021-02-23 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 10:43:14 +00:00
|
|
|
static bool ParseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
|
2021-04-14 11:42:11 +00:00
|
|
|
clang::DiagnosticsEngine &diags) {
|
2021-04-14 10:43:14 +00:00
|
|
|
unsigned numErrorsBefore = diags.getNumErrors();
|
2021-02-02 09:07:33 +00:00
|
|
|
|
|
|
|
// By default the frontend driver creates a ParseSyntaxOnly action.
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = ParseSyntaxOnly;
|
2021-02-02 09:07:33 +00:00
|
|
|
|
2021-10-14 08:07:30 +00:00
|
|
|
// Treat multiple action options as an invocation error. Note that `clang
|
|
|
|
// -cc1` does accept multiple action options, but will only consider the
|
|
|
|
// rightmost one.
|
|
|
|
if (args.hasMultipleArgs(clang::driver::options::OPT_Action_Group)) {
|
|
|
|
const unsigned diagID = diags.getCustomDiagID(
|
|
|
|
clang::DiagnosticsEngine::Error, "Only one action option is allowed");
|
|
|
|
diags.Report(diagID);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-11 10:17:31 +01:00
|
|
|
// Identify the action (i.e. opts.ProgramAction)
|
|
|
|
if (const llvm::opt::Arg *a =
|
|
|
|
args.getLastArg(clang::driver::options::OPT_Action_Group)) {
|
|
|
|
switch (a->getOption().getID()) {
|
|
|
|
default: {
|
|
|
|
llvm_unreachable("Invalid option in group!");
|
|
|
|
}
|
2020-10-24 12:33:19 +01:00
|
|
|
case clang::driver::options::OPT_test_io:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = InputOutputTest;
|
2020-10-24 12:33:19 +01:00
|
|
|
break;
|
2020-10-27 12:26:47 +00:00
|
|
|
case clang::driver::options::OPT_E:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = PrintPreprocessedInput;
|
2020-10-27 12:26:47 +00:00
|
|
|
break;
|
2020-12-08 16:27:46 +00:00
|
|
|
case clang::driver::options::OPT_fsyntax_only:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = ParseSyntaxOnly;
|
2020-12-08 16:27:46 +00:00
|
|
|
break;
|
2022-02-03 17:00:27 +00:00
|
|
|
case clang::driver::options::OPT_emit_mlir:
|
|
|
|
opts.programAction = EmitMLIR;
|
|
|
|
break;
|
2022-02-04 17:15:12 +00:00
|
|
|
case clang::driver::options::OPT_emit_llvm:
|
|
|
|
opts.programAction = EmitLLVM;
|
|
|
|
break;
|
[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 clang::driver::options::OPT_emit_obj:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = EmitObj;
|
[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
|
|
|
break;
|
2021-02-04 11:14:57 +00:00
|
|
|
case clang::driver::options::OPT_fdebug_unparse:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugUnparse;
|
2021-02-04 11:14:57 +00:00
|
|
|
break;
|
2021-03-30 10:35:42 +00:00
|
|
|
case clang::driver::options::OPT_fdebug_unparse_no_sema:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugUnparseNoSema;
|
2021-03-30 10:35:42 +00:00
|
|
|
break;
|
2021-02-04 11:14:57 +00:00
|
|
|
case clang::driver::options::OPT_fdebug_unparse_with_symbols:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugUnparseWithSymbols;
|
2021-02-04 11:14:57 +00:00
|
|
|
break;
|
2021-02-17 15:55:56 +00:00
|
|
|
case clang::driver::options::OPT_fdebug_dump_symbols:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugDumpSymbols;
|
2021-02-17 15:55:56 +00:00
|
|
|
break;
|
|
|
|
case clang::driver::options::OPT_fdebug_dump_parse_tree:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugDumpParseTree;
|
2021-02-17 15:55:56 +00:00
|
|
|
break;
|
2022-03-08 10:01:55 +00:00
|
|
|
case clang::driver::options::OPT_fdebug_dump_pft:
|
|
|
|
opts.programAction = DebugDumpPFT;
|
|
|
|
break;
|
2021-06-15 15:30:23 +00:00
|
|
|
case clang::driver::options::OPT_fdebug_dump_all:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugDumpAll;
|
2021-06-15 15:30:23 +00:00
|
|
|
break;
|
2021-03-30 10:35:42 +00:00
|
|
|
case clang::driver::options::OPT_fdebug_dump_parse_tree_no_sema:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugDumpParseTreeNoSema;
|
2021-03-30 10:35:42 +00:00
|
|
|
break;
|
2021-02-17 15:55:56 +00:00
|
|
|
case clang::driver::options::OPT_fdebug_dump_provenance:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugDumpProvenance;
|
2021-02-17 15:55:56 +00:00
|
|
|
break;
|
2021-02-23 17:59:17 +00:00
|
|
|
case clang::driver::options::OPT_fdebug_dump_parsing_log:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugDumpParsingLog;
|
2021-02-23 17:59:17 +00:00
|
|
|
break;
|
2021-02-17 18:53:05 +00:00
|
|
|
case clang::driver::options::OPT_fdebug_measure_parse_tree:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugMeasureParseTree;
|
2021-02-17 18:53:05 +00:00
|
|
|
break;
|
|
|
|
case clang::driver::options::OPT_fdebug_pre_fir_tree:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = DebugPreFIRTree;
|
2021-02-17 18:53:05 +00:00
|
|
|
break;
|
2021-03-08 16:54:11 +00:00
|
|
|
case clang::driver::options::OPT_fget_symbols_sources:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = GetSymbolsSources;
|
2021-03-08 16:54:11 +00:00
|
|
|
break;
|
2021-04-14 10:43:14 +00:00
|
|
|
case clang::driver::options::OPT_fget_definition:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = GetDefinition;
|
2021-04-14 10:43:14 +00:00
|
|
|
break;
|
2021-06-07 15:40:26 +01:00
|
|
|
case clang::driver::options::OPT_init_only:
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.programAction = InitOnly;
|
2021-06-07 15:40:26 +01:00
|
|
|
break;
|
2020-10-27 12:26:47 +00:00
|
|
|
|
2020-09-11 10:17:31 +01:00
|
|
|
// TODO:
|
2021-06-07 15:40:26 +01:00
|
|
|
// case clang::driver::options::OPT_emit_llvm:
|
2020-09-11 10:17:31 +01:00
|
|
|
// case clang::driver::options::OPT_emit_llvm_only:
|
|
|
|
// case clang::driver::options::OPT_emit_codegen_only:
|
|
|
|
// case clang::driver::options::OPT_emit_module:
|
|
|
|
// (...)
|
|
|
|
}
|
2021-04-14 10:43:14 +00:00
|
|
|
|
|
|
|
// Parse the values provided with `-fget-definition` (there should be 3
|
|
|
|
// integers)
|
|
|
|
if (llvm::opt::OptSpecifier(a->getOption().getID()) ==
|
|
|
|
clang::driver::options::OPT_fget_definition) {
|
|
|
|
unsigned optVals[3] = {0, 0, 0};
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 3; i++) {
|
|
|
|
llvm::StringRef val = a->getValue(i);
|
|
|
|
|
|
|
|
if (val.getAsInteger(10, optVals[i])) {
|
|
|
|
// A non-integer was encountered - that's an error.
|
|
|
|
diags.Report(clang::diag::err_drv_invalid_value)
|
|
|
|
<< a->getOption().getName() << val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.getDefVals.line = optVals[0];
|
|
|
|
opts.getDefVals.startColumn = optVals[1];
|
|
|
|
opts.getDefVals.endColumn = optVals[2];
|
2021-04-14 10:43:14 +00:00
|
|
|
}
|
2020-09-11 10:17:31 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 11:42:08 +01:00
|
|
|
// Parsing -load <dsopath> option and storing shared object path
|
|
|
|
if (llvm::opt::Arg *a = args.getLastArg(clang::driver::options::OPT_load)) {
|
|
|
|
opts.plugins.push_back(a->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing -plugin <name> option and storing plugin name and setting action
|
|
|
|
if (const llvm::opt::Arg *a =
|
|
|
|
args.getLastArg(clang::driver::options::OPT_plugin)) {
|
|
|
|
opts.programAction = PluginAction;
|
|
|
|
opts.ActionName = a->getValue();
|
|
|
|
}
|
|
|
|
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.outputFile = args.getLastArgValue(clang::driver::options::OPT_o);
|
|
|
|
opts.showHelp = args.hasArg(clang::driver::options::OPT_help);
|
|
|
|
opts.showVersion = args.hasArg(clang::driver::options::OPT_version);
|
2020-09-11 10:17:31 +01:00
|
|
|
|
|
|
|
// Get the input kind (from the value passed via `-x`)
|
|
|
|
InputKind dashX(Language::Unknown);
|
|
|
|
if (const llvm::opt::Arg *a =
|
|
|
|
args.getLastArg(clang::driver::options::OPT_x)) {
|
|
|
|
llvm::StringRef XValue = a->getValue();
|
|
|
|
// Principal languages.
|
|
|
|
dashX = llvm::StringSwitch<InputKind>(XValue)
|
|
|
|
.Case("f90", Language::Fortran)
|
|
|
|
.Default(Language::Unknown);
|
|
|
|
|
|
|
|
// Some special cases cannot be combined with suffixes.
|
|
|
|
if (dashX.IsUnknown())
|
|
|
|
dashX = llvm::StringSwitch<InputKind>(XValue)
|
|
|
|
.Case("ir", Language::LLVM_IR)
|
|
|
|
.Default(Language::Unknown);
|
|
|
|
|
|
|
|
if (dashX.IsUnknown())
|
|
|
|
diags.Report(clang::diag::err_drv_invalid_value)
|
|
|
|
<< a->getAsString(args) << a->getValue();
|
|
|
|
}
|
|
|
|
|
2020-10-24 12:33:19 +01:00
|
|
|
// Collect the input files and save them in our instance of FrontendOptions.
|
|
|
|
std::vector<std::string> inputs =
|
|
|
|
args.getAllArgValues(clang::driver::options::OPT_INPUT);
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.inputs.clear();
|
2020-10-24 12:33:19 +01:00
|
|
|
if (inputs.empty())
|
|
|
|
// '-' is the default input if none is given.
|
|
|
|
inputs.push_back("-");
|
|
|
|
for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
|
|
|
|
InputKind ik = dashX;
|
|
|
|
if (ik.IsUnknown()) {
|
|
|
|
ik = FrontendOptions::GetInputKindForExtension(
|
|
|
|
llvm::StringRef(inputs[i]).rsplit('.').second);
|
|
|
|
if (ik.IsUnknown())
|
|
|
|
ik = Language::Unknown;
|
|
|
|
if (i == 0)
|
|
|
|
dashX = ik;
|
|
|
|
}
|
|
|
|
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.inputs.emplace_back(std::move(inputs[i]), ik);
|
2020-10-24 12:33:19 +01:00
|
|
|
}
|
2021-01-26 16:27:30 +00:00
|
|
|
|
2021-07-29 12:39:10 +00:00
|
|
|
// Set fortranForm based on options -ffree-form and -ffixed-form.
|
2021-01-26 16:27:30 +00:00
|
|
|
if (const auto *arg = args.getLastArg(clang::driver::options::OPT_ffixed_form,
|
|
|
|
clang::driver::options::OPT_ffree_form)) {
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.fortranForm =
|
2021-01-26 16:27:30 +00:00
|
|
|
arg->getOption().matches(clang::driver::options::OPT_ffixed_form)
|
|
|
|
? FortranForm::FixedForm
|
|
|
|
: FortranForm::FreeForm;
|
|
|
|
}
|
|
|
|
|
2021-07-29 12:39:10 +00:00
|
|
|
// Set fixedFormColumns based on -ffixed-line-length=<value>
|
2021-01-26 16:27:30 +00:00
|
|
|
if (const auto *arg =
|
|
|
|
args.getLastArg(clang::driver::options::OPT_ffixed_line_length_EQ)) {
|
|
|
|
llvm::StringRef argValue = llvm::StringRef(arg->getValue());
|
|
|
|
std::int64_t columns = -1;
|
|
|
|
if (argValue == "none") {
|
|
|
|
columns = 0;
|
|
|
|
} else if (argValue.getAsInteger(/*Radix=*/10, columns)) {
|
|
|
|
columns = -1;
|
|
|
|
}
|
|
|
|
if (columns < 0) {
|
2021-05-05 11:01:33 -07:00
|
|
|
diags.Report(clang::diag::err_drv_negative_columns)
|
|
|
|
<< arg->getOption().getName() << arg->getValue();
|
2021-01-26 16:27:30 +00:00
|
|
|
} else if (columns == 0) {
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.fixedFormColumns = 1000000;
|
2021-01-26 16:27:30 +00:00
|
|
|
} else if (columns < 7) {
|
2021-05-05 11:01:33 -07:00
|
|
|
diags.Report(clang::diag::err_drv_small_columns)
|
|
|
|
<< arg->getOption().getName() << arg->getValue() << "7";
|
2021-01-26 16:27:30 +00:00
|
|
|
} else {
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.fixedFormColumns = columns;
|
2021-01-26 16:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-10 09:24:45 +00:00
|
|
|
|
[flang][driver] Refactor boolean options
For boolean options, e.g. `-fxor-operator`/`-fno-xor-operator`, we ought
to be using TableGen multi-classes. This way, we only have to write one
definition to have both forms auto-generated. This patch refactors all
of Flang's boolean options to use two new multi-classes:
`OptInFC1FFOption` and `OptOutFC1FFOption`. These multi-classes are
based on `OptInFFOption`/`OptOutFFOption`, respectively. I've also
simplified the processing of the updated options in
CompilerInvocation.cpp.
With the new approach, "empty" help text (i.e. no `HelpText`) is now
replaced with an empty string (i.e. HelpText<"">). When running
flang-new --help, that's considered as non-empty help messages, which is
then printed (that's controlled by `printHelp` from
llvm/lib/Option/OptTable.cpp). This means that with this patch,
flang-new --help will start printing e.g. -fno-backslash, even though
there is no actual help text to print for this option (apart from the
empty string ""). Tests are updated accordingly.
Note that with this patch, both `-fxor-operator` and `-fno-xor-operator`
(and other boolean options refactored here) remain available in
`flang-new` and `flang-new -fc1`. In this respect, nothing changes. In a
forthcoming patch, I will refine this so that `flang-new -fc1` only
accepts `-ffoo` (`OptInFC1FFOption`) or `-fno-foo` (`OptOutCC1FFOption`).
For clarity, `OptInFFOption`/`OptOutFFOption` are renamed as
`OptInCC1FFOption`/`OptOutCC1FFOption`, respectively. Otherwise, this is
an NFC from Clang's perspective.
Differential Revision: https://reviews.llvm.org/D105881
2021-06-30 10:57:48 +00:00
|
|
|
// -f{no-}implicit-none
|
|
|
|
opts.features.Enable(
|
|
|
|
Fortran::common::LanguageFeature::ImplicitNoneTypeAlways,
|
|
|
|
args.hasFlag(clang::driver::options::OPT_fimplicit_none,
|
|
|
|
clang::driver::options::OPT_fno_implicit_none, false));
|
|
|
|
|
|
|
|
// -f{no-}backslash
|
|
|
|
opts.features.Enable(Fortran::common::LanguageFeature::BackslashEscapes,
|
|
|
|
args.hasFlag(clang::driver::options::OPT_fbackslash,
|
|
|
|
clang::driver::options::OPT_fno_backslash, false));
|
|
|
|
|
|
|
|
// -f{no-}logical-abbreviations
|
|
|
|
opts.features.Enable(Fortran::common::LanguageFeature::LogicalAbbreviations,
|
|
|
|
args.hasFlag(clang::driver::options::OPT_flogical_abbreviations,
|
|
|
|
clang::driver::options::OPT_fno_logical_abbreviations, false));
|
|
|
|
|
|
|
|
// -f{no-}xor-operator
|
|
|
|
opts.features.Enable(Fortran::common::LanguageFeature::XOROperator,
|
|
|
|
args.hasFlag(clang::driver::options::OPT_fxor_operator,
|
|
|
|
clang::driver::options::OPT_fno_xor_operator, false));
|
|
|
|
|
[flang] Add -fno-automatic, refine IsSaved()
This legacy option (available in other Fortran compilers with various
spellings) implies the SAVE attribute for local variables on subprograms
that are not explicitly RECURSIVE. The SAVE attribute essentially implies
static rather than stack storage. This was the default setting in Fortran
until surprisingly recently, so explicit SAVE statements & attributes
could be and often were omitted from older codes. Note that initialized
objects already have an implied SAVE attribute, and objects in COMMON
effectively do too, as data overlays are extinct; and since objects that are
expected to survive from one invocation of a procedure to the next in static
storage should probably be explicit initialized in the first place, so the
use cases for this option are somewhat rare, and all of them could be
handled with explicit SAVE statements or attributes.
This implicit SAVE attribute must not apply to automatic (in the Fortran sense)
local objects, whose sizes cannot be known at compilation time. To get the
semantics of IsSaved() right, the IsAutomatic() predicate was moved into
Evaluate/tools.cpp to allow for dynamic linking of the compiler. The
redundant predicate IsAutomatic() was noticed, removed, and its uses replaced.
GNU Fortran's spelling of the option (-fno-automatic) was added to
the clang-based driver and used for basic sanity testing.
Differential Revision: https://reviews.llvm.org/D114209
2021-11-18 11:48:42 -08:00
|
|
|
// -fno-automatic
|
|
|
|
if (args.hasArg(clang::driver::options::OPT_fno_automatic)) {
|
|
|
|
opts.features.Enable(Fortran::common::LanguageFeature::DefaultSave);
|
|
|
|
}
|
|
|
|
|
2021-02-04 21:11:22 +00:00
|
|
|
if (args.hasArg(
|
|
|
|
clang::driver::options::OPT_falternative_parameter_statement)) {
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.features.Enable(Fortran::common::LanguageFeature::OldStyleParameter);
|
2021-02-04 21:11:22 +00:00
|
|
|
}
|
|
|
|
if (const llvm::opt::Arg *arg =
|
|
|
|
args.getLastArg(clang::driver::options::OPT_finput_charset_EQ)) {
|
|
|
|
llvm::StringRef argValue = arg->getValue();
|
|
|
|
if (argValue == "utf-8") {
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.encoding = Fortran::parser::Encoding::UTF_8;
|
2021-02-04 21:11:22 +00:00
|
|
|
} else if (argValue == "latin-1") {
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.encoding = Fortran::parser::Encoding::LATIN_1;
|
2021-02-04 21:11:22 +00:00
|
|
|
} else {
|
|
|
|
diags.Report(clang::diag::err_drv_invalid_value)
|
|
|
|
<< arg->getAsString(args) << argValue;
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 17:59:17 +00:00
|
|
|
|
|
|
|
setUpFrontendBasedOnAction(opts);
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.dashX = dashX;
|
2021-04-14 10:43:14 +00:00
|
|
|
|
|
|
|
return diags.getNumErrors() == numErrorsBefore;
|
2020-09-11 10:17:31 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 12:24:57 -04:00
|
|
|
// Generate the path to look for intrinsic modules
|
|
|
|
static std::string getIntrinsicDir() {
|
|
|
|
// TODO: Find a system independent API
|
|
|
|
llvm::SmallString<128> driverPath;
|
|
|
|
driverPath.assign(llvm::sys::fs::getMainExecutable(nullptr, nullptr));
|
|
|
|
llvm::sys::path::remove_filename(driverPath);
|
|
|
|
driverPath.append("/../include/flang/");
|
|
|
|
return std::string(driverPath);
|
|
|
|
}
|
|
|
|
|
2021-01-06 15:42:24 +00:00
|
|
|
/// Parses all preprocessor input arguments and populates the preprocessor
|
|
|
|
/// options accordingly.
|
|
|
|
///
|
|
|
|
/// \param [in] opts The preprocessor options instance
|
|
|
|
/// \param [out] args The list of input arguments
|
|
|
|
static void parsePreprocessorArgs(
|
|
|
|
Fortran::frontend::PreprocessorOptions &opts, llvm::opt::ArgList &args) {
|
|
|
|
// Add macros from the command line.
|
|
|
|
for (const auto *currentArg : args.filtered(
|
|
|
|
clang::driver::options::OPT_D, clang::driver::options::OPT_U)) {
|
|
|
|
if (currentArg->getOption().matches(clang::driver::options::OPT_D)) {
|
|
|
|
opts.addMacroDef(currentArg->getValue());
|
|
|
|
} else {
|
|
|
|
opts.addMacroUndef(currentArg->getValue());
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 10:01:38 +00:00
|
|
|
|
|
|
|
// Add the ordered list of -I's.
|
|
|
|
for (const auto *currentArg : args.filtered(clang::driver::options::OPT_I))
|
|
|
|
opts.searchDirectoriesFromDashI.emplace_back(currentArg->getValue());
|
2021-03-23 12:24:57 -04:00
|
|
|
|
|
|
|
// Prepend the ordered list of -intrinsic-modules-path
|
|
|
|
// to the default location to search.
|
|
|
|
for (const auto *currentArg :
|
|
|
|
args.filtered(clang::driver::options::OPT_fintrinsic_modules_path))
|
|
|
|
opts.searchDirectoriesFromIntrModPath.emplace_back(currentArg->getValue());
|
2021-04-07 11:42:37 +00:00
|
|
|
|
|
|
|
// -cpp/-nocpp
|
|
|
|
if (const auto *currentArg = args.getLastArg(
|
|
|
|
clang::driver::options::OPT_cpp, clang::driver::options::OPT_nocpp))
|
2021-07-29 12:39:10 +00:00
|
|
|
opts.macrosFlag =
|
2021-04-07 11:42:37 +00:00
|
|
|
(currentArg->getOption().matches(clang::driver::options::OPT_cpp))
|
|
|
|
? PPMacrosFlag::Include
|
|
|
|
: PPMacrosFlag::Exclude;
|
2021-07-23 16:41:04 -07:00
|
|
|
|
|
|
|
opts.noReformat = args.hasArg(clang::driver::options::OPT_fno_reformat);
|
|
|
|
opts.noLineDirectives = args.hasArg(clang::driver::options::OPT_P);
|
2021-01-06 15:42:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 16:13:04 +00:00
|
|
|
/// Parses all semantic related arguments and populates the variables
|
2021-04-14 11:42:11 +00:00
|
|
|
/// options accordingly. Returns false if new errors are generated.
|
|
|
|
static bool parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
|
2021-02-04 16:13:04 +00:00
|
|
|
clang::DiagnosticsEngine &diags) {
|
2021-04-14 11:42:11 +00:00
|
|
|
unsigned numErrorsBefore = diags.getNumErrors();
|
2021-02-04 16:13:04 +00:00
|
|
|
|
2021-03-11 07:35:45 -05:00
|
|
|
// -J/module-dir option
|
2021-02-04 16:13:04 +00:00
|
|
|
auto moduleDirList =
|
|
|
|
args.getAllArgValues(clang::driver::options::OPT_module_dir);
|
|
|
|
// User can only specify -J/-module-dir once
|
|
|
|
// https://gcc.gnu.org/onlinedocs/gfortran/Directory-Options.html
|
|
|
|
if (moduleDirList.size() > 1) {
|
|
|
|
const unsigned diagID =
|
|
|
|
diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
|
|
|
|
"Only one '-module-dir/-J' option allowed");
|
|
|
|
diags.Report(diagID);
|
|
|
|
}
|
|
|
|
if (moduleDirList.size() == 1)
|
2021-03-11 07:35:45 -05:00
|
|
|
res.SetModuleDir(moduleDirList[0]);
|
|
|
|
|
|
|
|
// -fdebug-module-writer option
|
|
|
|
if (args.hasArg(clang::driver::options::OPT_fdebug_module_writer)) {
|
|
|
|
res.SetDebugModuleDir(true);
|
|
|
|
}
|
2021-04-14 11:42:11 +00:00
|
|
|
|
2021-06-04 13:58:03 +01:00
|
|
|
// -module-suffix
|
|
|
|
if (const auto *moduleSuffix =
|
|
|
|
args.getLastArg(clang::driver::options::OPT_module_suffix)) {
|
|
|
|
res.SetModuleFileSuffix(moduleSuffix->getValue());
|
|
|
|
}
|
|
|
|
|
[flang][driver] Refactor boolean options
For boolean options, e.g. `-fxor-operator`/`-fno-xor-operator`, we ought
to be using TableGen multi-classes. This way, we only have to write one
definition to have both forms auto-generated. This patch refactors all
of Flang's boolean options to use two new multi-classes:
`OptInFC1FFOption` and `OptOutFC1FFOption`. These multi-classes are
based on `OptInFFOption`/`OptOutFFOption`, respectively. I've also
simplified the processing of the updated options in
CompilerInvocation.cpp.
With the new approach, "empty" help text (i.e. no `HelpText`) is now
replaced with an empty string (i.e. HelpText<"">). When running
flang-new --help, that's considered as non-empty help messages, which is
then printed (that's controlled by `printHelp` from
llvm/lib/Option/OptTable.cpp). This means that with this patch,
flang-new --help will start printing e.g. -fno-backslash, even though
there is no actual help text to print for this option (apart from the
empty string ""). Tests are updated accordingly.
Note that with this patch, both `-fxor-operator` and `-fno-xor-operator`
(and other boolean options refactored here) remain available in
`flang-new` and `flang-new -fc1`. In this respect, nothing changes. In a
forthcoming patch, I will refine this so that `flang-new -fc1` only
accepts `-ffoo` (`OptInFC1FFOption`) or `-fno-foo` (`OptOutCC1FFOption`).
For clarity, `OptInFFOption`/`OptOutFFOption` are renamed as
`OptInCC1FFOption`/`OptOutCC1FFOption`, respectively. Otherwise, this is
an NFC from Clang's perspective.
Differential Revision: https://reviews.llvm.org/D105881
2021-06-30 10:57:48 +00:00
|
|
|
// -f{no-}analyzed-objects-for-unparse
|
|
|
|
res.SetUseAnalyzedObjectsForUnparse(
|
|
|
|
args.hasFlag(clang::driver::options::OPT_fanalyzed_objects_for_unparse,
|
|
|
|
clang::driver::options::OPT_fno_analyzed_objects_for_unparse, true));
|
2021-06-04 15:25:58 +01:00
|
|
|
|
2021-04-14 11:42:11 +00:00
|
|
|
return diags.getNumErrors() == numErrorsBefore;
|
2021-02-04 16:13:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 12:41:46 -04:00
|
|
|
/// Parses all diagnostics related arguments and populates the variables
|
2021-04-14 11:42:11 +00:00
|
|
|
/// options accordingly. Returns false if new errors are generated.
|
|
|
|
static bool parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
|
2021-04-05 12:41:46 -04:00
|
|
|
clang::DiagnosticsEngine &diags) {
|
2021-04-14 11:42:11 +00:00
|
|
|
unsigned numErrorsBefore = diags.getNumErrors();
|
|
|
|
|
2021-04-05 12:41:46 -04:00
|
|
|
// -Werror option
|
|
|
|
// TODO: Currently throws a Diagnostic for anything other than -W<error>,
|
|
|
|
// this has to change when other -W<opt>'s are supported.
|
|
|
|
if (args.hasArg(clang::driver::options::OPT_W_Joined)) {
|
|
|
|
if (args.getLastArgValue(clang::driver::options::OPT_W_Joined)
|
|
|
|
.equals("error")) {
|
|
|
|
res.SetWarnAsErr(true);
|
|
|
|
} else {
|
|
|
|
const unsigned diagID =
|
|
|
|
diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
|
|
|
|
"Only `-Werror` is supported currently.");
|
|
|
|
diags.Report(diagID);
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 11:42:11 +00:00
|
|
|
|
|
|
|
return diags.getNumErrors() == numErrorsBefore;
|
2021-04-05 12:41:46 -04:00
|
|
|
}
|
|
|
|
|
2021-03-03 16:28:11 +00:00
|
|
|
/// Parses all Dialect related arguments and populates the variables
|
2021-04-14 11:42:11 +00:00
|
|
|
/// options accordingly. Returns false if new errors are generated.
|
|
|
|
static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
|
2021-03-03 16:28:11 +00:00
|
|
|
clang::DiagnosticsEngine &diags) {
|
2021-04-14 11:42:11 +00:00
|
|
|
unsigned numErrorsBefore = diags.getNumErrors();
|
2021-03-03 16:28:11 +00:00
|
|
|
|
|
|
|
// -fdefault* family
|
|
|
|
if (args.hasArg(clang::driver::options::OPT_fdefault_real_8)) {
|
|
|
|
res.defaultKinds().set_defaultRealKind(8);
|
|
|
|
res.defaultKinds().set_doublePrecisionKind(16);
|
|
|
|
}
|
|
|
|
if (args.hasArg(clang::driver::options::OPT_fdefault_integer_8)) {
|
|
|
|
res.defaultKinds().set_defaultIntegerKind(8);
|
|
|
|
res.defaultKinds().set_subscriptIntegerKind(8);
|
|
|
|
res.defaultKinds().set_sizeIntegerKind(8);
|
|
|
|
}
|
|
|
|
if (args.hasArg(clang::driver::options::OPT_fdefault_double_8)) {
|
|
|
|
if (!args.hasArg(clang::driver::options::OPT_fdefault_real_8)) {
|
|
|
|
// -fdefault-double-8 has to be used with -fdefault-real-8
|
|
|
|
// to be compatible with gfortran
|
|
|
|
const unsigned diagID =
|
|
|
|
diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
|
|
|
|
"Use of `-fdefault-double-8` requires `-fdefault-real-8`");
|
|
|
|
diags.Report(diagID);
|
|
|
|
}
|
|
|
|
// https://gcc.gnu.org/onlinedocs/gfortran/Fortran-Dialect-Options.html
|
|
|
|
res.defaultKinds().set_doublePrecisionKind(8);
|
|
|
|
}
|
|
|
|
if (args.hasArg(clang::driver::options::OPT_flarge_sizes))
|
|
|
|
res.defaultKinds().set_sizeIntegerKind(8);
|
|
|
|
|
|
|
|
// -fopenmp and -fopenacc
|
|
|
|
if (args.hasArg(clang::driver::options::OPT_fopenacc)) {
|
2021-07-29 12:39:10 +00:00
|
|
|
res.frontendOpts().features.Enable(
|
2021-03-03 16:28:11 +00:00
|
|
|
Fortran::common::LanguageFeature::OpenACC);
|
|
|
|
}
|
|
|
|
if (args.hasArg(clang::driver::options::OPT_fopenmp)) {
|
2021-07-29 12:39:10 +00:00
|
|
|
res.frontendOpts().features.Enable(
|
2021-03-03 16:28:11 +00:00
|
|
|
Fortran::common::LanguageFeature::OpenMP);
|
|
|
|
}
|
2021-03-25 13:02:05 -04:00
|
|
|
|
2021-03-25 18:59:48 +00:00
|
|
|
// -pedantic
|
2021-03-25 13:02:05 -04:00
|
|
|
if (args.hasArg(clang::driver::options::OPT_pedantic)) {
|
|
|
|
res.set_EnableConformanceChecks();
|
|
|
|
}
|
2021-03-25 18:59:48 +00:00
|
|
|
// -std=f2018 (currently this implies -pedantic)
|
2021-03-25 13:02:05 -04:00
|
|
|
// TODO: Set proper options when more fortran standards
|
|
|
|
// are supported.
|
|
|
|
if (args.hasArg(clang::driver::options::OPT_std_EQ)) {
|
|
|
|
auto standard = args.getLastArgValue(clang::driver::options::OPT_std_EQ);
|
|
|
|
// We only allow f2018 as the given standard
|
|
|
|
if (standard.equals("f2018")) {
|
|
|
|
res.set_EnableConformanceChecks();
|
|
|
|
} else {
|
|
|
|
const unsigned diagID =
|
|
|
|
diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
|
|
|
|
"Only -std=f2018 is allowed currently.");
|
|
|
|
diags.Report(diagID);
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 11:42:11 +00:00
|
|
|
return diags.getNumErrors() == numErrorsBefore;
|
2021-03-03 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 10:17:31 +01:00
|
|
|
bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
|
|
|
|
llvm::ArrayRef<const char *> commandLineArgs,
|
|
|
|
clang::DiagnosticsEngine &diags) {
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
// Parse the arguments
|
|
|
|
const llvm::opt::OptTable &opts = clang::driver::getDriverOptTable();
|
2021-01-25 11:58:52 +00:00
|
|
|
const unsigned includedFlagsBitmask = clang::driver::options::FC1Option;
|
2020-09-11 10:17:31 +01:00
|
|
|
unsigned missingArgIndex, missingArgCount;
|
|
|
|
llvm::opt::InputArgList args = opts.ParseArgs(
|
|
|
|
commandLineArgs, missingArgIndex, missingArgCount, includedFlagsBitmask);
|
|
|
|
|
2021-06-02 21:53:02 +01:00
|
|
|
// Check for missing argument error.
|
|
|
|
if (missingArgCount) {
|
|
|
|
diags.Report(clang::diag::err_drv_missing_argument)
|
|
|
|
<< args.getArgString(missingArgIndex) << missingArgCount;
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
2020-09-11 10:17:31 +01:00
|
|
|
// Issue errors on unknown arguments
|
|
|
|
for (const auto *a : args.filtered(clang::driver::options::OPT_UNKNOWN)) {
|
|
|
|
auto argString = a->getAsString(args);
|
|
|
|
std::string nearest;
|
|
|
|
if (opts.findNearest(argString, nearest, includedFlagsBitmask) > 1)
|
|
|
|
diags.Report(clang::diag::err_drv_unknown_argument) << argString;
|
|
|
|
else
|
|
|
|
diags.Report(clang::diag::err_drv_unknown_argument_with_suggestion)
|
|
|
|
<< argString << nearest;
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:43:14 +00:00
|
|
|
success &= ParseFrontendArgs(res.frontendOpts(), args, diags);
|
[flang][driver] Add support for `--target`/`--triple`
This patch adds support for:
* `--target` in the compiler driver (`flang-new`)
* `--triple` in the frontend driver (`flang-new -fc1`)
The semantics of these flags are inherited from `clangDriver`, i.e.
consistent with `clang --target` and `clang -cc1 --triple`,
respectively.
A new structure is defined, `TargetOptions`, that will hold various
Frontend options related to the target. Currently, this is mostly a
placeholder that contains the target triple. In the future, it will be
used for storing e.g. the CPU to tune for or the target features to
enable.
Additionally, the following target/triple related options are enabled
[*]: `-print-effective-triple`, `-print-target-triple`. Definitions in
Options.td are updated accordingly and, to facilated testing,
`-emit-llvm` is added to the list of options available in `flang-new`
(previously it was only enabled in `flang-new -fc1`).
[*] These options were actually available before (like all other options
defined in `clangDriver`), but not included in `flang-new --help`.
Before this change, `flang-new` would just use `native` for defining the
target, so these options were of little value.
Differential Revision: https://reviews.llvm.org/D120246
2022-02-21 11:51:32 +00:00
|
|
|
ParseTargetArgs(res.targetOpts(), args);
|
2021-01-06 15:42:24 +00:00
|
|
|
parsePreprocessorArgs(res.preprocessorOpts(), args);
|
2021-04-14 11:42:11 +00:00
|
|
|
success &= parseSemaArgs(res, args, diags);
|
|
|
|
success &= parseDialectArgs(res, args, diags);
|
|
|
|
success &= parseDiagArgs(res, args, diags);
|
2020-09-11 10:17:31 +01:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
2020-10-27 12:26:47 +00:00
|
|
|
|
2022-01-27 16:44:44 +00:00
|
|
|
void CompilerInvocation::CollectMacroDefinitions() {
|
2021-04-07 11:42:37 +00:00
|
|
|
auto &ppOpts = this->preprocessorOpts();
|
|
|
|
|
2021-01-06 15:42:24 +00:00
|
|
|
for (unsigned i = 0, n = ppOpts.macros.size(); i != n; ++i) {
|
|
|
|
llvm::StringRef macro = ppOpts.macros[i].first;
|
|
|
|
bool isUndef = ppOpts.macros[i].second;
|
|
|
|
|
|
|
|
std::pair<llvm::StringRef, llvm::StringRef> macroPair = macro.split('=');
|
|
|
|
llvm::StringRef macroName = macroPair.first;
|
|
|
|
llvm::StringRef macroBody = macroPair.second;
|
|
|
|
|
|
|
|
// For an #undef'd macro, we only care about the name.
|
|
|
|
if (isUndef) {
|
2021-04-07 11:42:37 +00:00
|
|
|
parserOpts_.predefinitions.emplace_back(
|
2021-01-06 15:42:24 +00:00
|
|
|
macroName.str(), std::optional<std::string>{});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For a #define'd macro, figure out the actual definition.
|
|
|
|
if (macroName.size() == macro.size())
|
|
|
|
macroBody = "1";
|
|
|
|
else {
|
|
|
|
// Note: GCC drops anything following an end-of-line character.
|
|
|
|
llvm::StringRef::size_type End = macroBody.find_first_of("\n\r");
|
|
|
|
macroBody = macroBody.substr(0, End);
|
|
|
|
}
|
2021-04-07 11:42:37 +00:00
|
|
|
parserOpts_.predefinitions.emplace_back(
|
2021-01-06 15:42:24 +00:00
|
|
|
macroName, std::optional<std::string>(macroBody.str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:26:47 +00:00
|
|
|
void CompilerInvocation::SetDefaultFortranOpts() {
|
2020-12-14 16:07:24 +00:00
|
|
|
auto &fortranOptions = fortranOpts();
|
2020-10-27 12:26:47 +00:00
|
|
|
|
|
|
|
std::vector<std::string> searchDirectories{"."s};
|
|
|
|
fortranOptions.searchDirectories = searchDirectories;
|
|
|
|
fortranOptions.isFixedForm = false;
|
2021-02-11 11:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: When expanding this method, consider creating a dedicated API for
|
|
|
|
// this. Also at some point we will need to differentiate between different
|
|
|
|
// targets and add dedicated predefines for each.
|
2022-01-27 16:44:44 +00:00
|
|
|
void CompilerInvocation::SetDefaultPredefinitions() {
|
2021-02-11 11:37:24 +00:00
|
|
|
auto &fortranOptions = fortranOpts();
|
|
|
|
const auto &frontendOptions = frontendOpts();
|
2021-01-19 13:15:53 +00:00
|
|
|
|
|
|
|
// Populate the macro list with version numbers and other predefinitions.
|
|
|
|
fortranOptions.predefinitions.emplace_back("__flang__", "1");
|
|
|
|
fortranOptions.predefinitions.emplace_back(
|
|
|
|
"__flang_major__", FLANG_VERSION_MAJOR_STRING);
|
|
|
|
fortranOptions.predefinitions.emplace_back(
|
|
|
|
"__flang_minor__", FLANG_VERSION_MINOR_STRING);
|
|
|
|
fortranOptions.predefinitions.emplace_back(
|
|
|
|
"__flang_patchlevel__", FLANG_VERSION_PATCHLEVEL_STRING);
|
2021-02-10 09:24:45 +00:00
|
|
|
|
|
|
|
// Add predefinitions based on extensions enabled
|
2021-07-29 12:39:10 +00:00
|
|
|
if (frontendOptions.features.IsEnabled(
|
2021-02-10 09:24:45 +00:00
|
|
|
Fortran::common::LanguageFeature::OpenACC)) {
|
|
|
|
fortranOptions.predefinitions.emplace_back("_OPENACC", "202011");
|
|
|
|
}
|
2021-07-29 12:39:10 +00:00
|
|
|
if (frontendOptions.features.IsEnabled(
|
2021-02-10 09:24:45 +00:00
|
|
|
Fortran::common::LanguageFeature::OpenMP)) {
|
|
|
|
fortranOptions.predefinitions.emplace_back("_OPENMP", "201511");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 16:44:44 +00:00
|
|
|
void CompilerInvocation::SetFortranOpts() {
|
2021-01-06 15:42:24 +00:00
|
|
|
auto &fortranOptions = fortranOpts();
|
2021-01-26 16:27:30 +00:00
|
|
|
const auto &frontendOptions = frontendOpts();
|
2021-01-06 15:42:24 +00:00
|
|
|
const auto &preprocessorOptions = preprocessorOpts();
|
2021-02-04 16:13:04 +00:00
|
|
|
auto &moduleDirJ = moduleDir();
|
2021-01-06 15:42:24 +00:00
|
|
|
|
2021-07-29 12:39:10 +00:00
|
|
|
if (frontendOptions.fortranForm != FortranForm::Unknown) {
|
2021-01-26 16:27:30 +00:00
|
|
|
fortranOptions.isFixedForm =
|
2021-07-29 12:39:10 +00:00
|
|
|
frontendOptions.fortranForm == FortranForm::FixedForm;
|
2021-01-26 16:27:30 +00:00
|
|
|
}
|
2021-07-29 12:39:10 +00:00
|
|
|
fortranOptions.fixedFormColumns = frontendOptions.fixedFormColumns;
|
2021-01-26 16:27:30 +00:00
|
|
|
|
2021-07-29 12:39:10 +00:00
|
|
|
fortranOptions.features = frontendOptions.features;
|
|
|
|
fortranOptions.encoding = frontendOptions.encoding;
|
2021-02-10 09:24:45 +00:00
|
|
|
|
2021-03-23 12:24:57 -04:00
|
|
|
// Adding search directories specified by -I
|
2021-01-19 10:01:38 +00:00
|
|
|
fortranOptions.searchDirectories.insert(
|
|
|
|
fortranOptions.searchDirectories.end(),
|
|
|
|
preprocessorOptions.searchDirectoriesFromDashI.begin(),
|
|
|
|
preprocessorOptions.searchDirectoriesFromDashI.end());
|
2021-02-04 16:13:04 +00:00
|
|
|
|
2021-03-23 12:24:57 -04:00
|
|
|
// Add the ordered list of -intrinsic-modules-path
|
|
|
|
fortranOptions.searchDirectories.insert(
|
|
|
|
fortranOptions.searchDirectories.end(),
|
|
|
|
preprocessorOptions.searchDirectoriesFromIntrModPath.begin(),
|
|
|
|
preprocessorOptions.searchDirectoriesFromIntrModPath.end());
|
|
|
|
|
2022-01-26 09:54:58 -08:00
|
|
|
// Add the default intrinsic module directory
|
|
|
|
fortranOptions.intrinsicModuleDirectories.emplace_back(getIntrinsicDir());
|
2021-03-23 12:24:57 -04:00
|
|
|
|
2021-02-04 16:13:04 +00:00
|
|
|
// Add the directory supplied through -J/-module-dir to the list of search
|
|
|
|
// directories
|
|
|
|
if (moduleDirJ.compare(".") != 0)
|
|
|
|
fortranOptions.searchDirectories.emplace_back(moduleDirJ);
|
2021-02-23 17:59:17 +00:00
|
|
|
|
2021-07-29 12:39:10 +00:00
|
|
|
if (frontendOptions.instrumentedParse)
|
2021-02-23 17:59:17 +00:00
|
|
|
fortranOptions.instrumentedParse = true;
|
2021-03-25 13:02:05 -04:00
|
|
|
|
2021-07-29 12:39:10 +00:00
|
|
|
if (frontendOptions.needProvenanceRangeToCharBlockMappings)
|
2021-04-07 13:10:35 +00:00
|
|
|
fortranOptions.needProvenanceRangeToCharBlockMappings = true;
|
|
|
|
|
2021-03-25 13:02:05 -04:00
|
|
|
if (enableConformanceChecks()) {
|
|
|
|
fortranOptions.features.WarnOnAllNonstandard();
|
|
|
|
}
|
2021-02-04 16:13:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 16:44:44 +00:00
|
|
|
void CompilerInvocation::SetSemanticsOpts(
|
2021-02-10 09:24:45 +00:00
|
|
|
Fortran::parser::AllCookedSources &allCookedSources) {
|
|
|
|
const auto &fortranOptions = fortranOpts();
|
|
|
|
|
|
|
|
semanticsContext_ = std::make_unique<semantics::SemanticsContext>(
|
2021-03-03 16:28:11 +00:00
|
|
|
defaultKinds(), fortranOptions.features, allCookedSources);
|
2021-02-10 09:24:45 +00:00
|
|
|
|
2021-03-11 07:35:45 -05:00
|
|
|
semanticsContext_->set_moduleDirectory(moduleDir())
|
2021-03-25 13:02:05 -04:00
|
|
|
.set_searchDirectories(fortranOptions.searchDirectories)
|
2022-01-26 09:54:58 -08:00
|
|
|
.set_intrinsicModuleDirectories(fortranOptions.intrinsicModuleDirectories)
|
2021-04-05 12:41:46 -04:00
|
|
|
.set_warnOnNonstandardUsage(enableConformanceChecks())
|
2021-06-04 13:58:03 +01:00
|
|
|
.set_warningsAreErrors(warnAsErr())
|
|
|
|
.set_moduleFileSuffix(moduleFileSuffix());
|
2021-01-06 15:42:24 +00:00
|
|
|
}
|