
Summary: This is the first patch implementing the new Flang driver as outlined in [1], [2] & [3]. It creates Flang driver (`flang-new`) and Flang frontend driver (`flang-new -fc1`). These will be renamed as `flang` and `flang -fc1` once the current Flang throwaway driver, `flang`, can be replaced with `flang-new`. Currently only 2 options are supported: `-help` and `--version`. `flang-new` is implemented in terms of libclangDriver, defaulting the driver mode to `FlangMode` (added to libclangDriver in [4]). This ensures that the driver runs in Flang mode regardless of the name of the binary inferred from argv[0]. The design of the new Flang compiler and frontend drivers is inspired by it counterparts in Clang [3]. Currently, the new Flang compiler and frontend drivers re-use Clang libraries: clangBasic, clangDriver and clangFrontend. To identify Flang options, this patch adds FlangOption/FC1Option enums. Driver::printHelp is updated so that `flang-new` prints only Flang options. The new Flang driver is disabled by default. To enable it, set `-DBUILD_FLANG_NEW_DRIVER=ON` when configuring CMake and add clang to `LLVM_ENABLE_PROJECTS` (e.g. -DLLVM_ENABLE_PROJECTS=“clang;flang;mlir”). [1] “RFC: new Flang driver - next steps” http://lists.llvm.org/pipermail/flang-dev/2020-July/000470.html [2] “RFC: Adding a fortran mode to the clang driver for flang” http://lists.llvm.org/pipermail/cfe-dev/2019-June/062669.html [3] “RFC: refactoring libclangDriver/libclangFrontend to share with Flang” http://lists.llvm.org/pipermail/cfe-dev/2020-July/066393.html [4] https://reviews.llvm.org/rG6bf55804924d5a1d902925ad080b1a2b57c5c75c co-authored-by: Andrzej Warzynski <andrzej.warzynski@arm.com> Reviewed By: richard.barton.arm, sameeranjoshi Differential Revision: https://reviews.llvm.org/D86089
116 lines
4.3 KiB
C++
116 lines
4.3 KiB
C++
//===- 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"
|
|
#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"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace Fortran::frontend;
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Initialization.
|
|
//===----------------------------------------------------------------------===//
|
|
CompilerInvocationBase::CompilerInvocationBase()
|
|
: diagnosticOpts_(new clang::DiagnosticOptions()) {}
|
|
|
|
CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &x)
|
|
: diagnosticOpts_(new clang::DiagnosticOptions(x.GetDiagnosticOpts())) {}
|
|
|
|
CompilerInvocationBase::~CompilerInvocationBase() = default;
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Deserialization (from args)
|
|
//===----------------------------------------------------------------------===//
|
|
static InputKind ParseFrontendArgs(FrontendOptions &opts,
|
|
llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) {
|
|
// 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!");
|
|
}
|
|
// TODO:
|
|
// case clang::driver::options::OPT_E:
|
|
// case clang::driver::options::OPT_emit_obj:
|
|
// case calng::driver::options::OPT_emit_llvm:
|
|
// case clang::driver::options::OPT_emit_llvm_only:
|
|
// case clang::driver::options::OPT_emit_codegen_only:
|
|
// case clang::driver::options::OPT_emit_module:
|
|
// (...)
|
|
}
|
|
}
|
|
|
|
opts.showHelp_ = args.hasArg(clang::driver::options::OPT_help);
|
|
opts.showVersion_ = args.hasArg(clang::driver::options::OPT_version);
|
|
|
|
// 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();
|
|
}
|
|
|
|
return dashX;
|
|
}
|
|
|
|
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();
|
|
const unsigned includedFlagsBitmask =
|
|
clang::driver::options::FC1Option;
|
|
unsigned missingArgIndex, missingArgCount;
|
|
llvm::opt::InputArgList args = opts.ParseArgs(
|
|
commandLineArgs, missingArgIndex, missingArgCount, includedFlagsBitmask);
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Parse the frontend args
|
|
ParseFrontendArgs(res.GetFrontendOpts(), args, diags);
|
|
|
|
return success;
|
|
}
|