1 //===- CompilerInvocation.cpp ---------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "flang/Frontend/CompilerInvocation.h" 10 #include "clang/Basic/AllDiagnostics.h" 11 #include "clang/Basic/DiagnosticDriver.h" 12 #include "clang/Basic/DiagnosticOptions.h" 13 #include "clang/Driver/DriverDiagnostic.h" 14 #include "clang/Driver/Options.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/ADT/StringSwitch.h" 17 #include "llvm/Option/Arg.h" 18 #include "llvm/Option/ArgList.h" 19 #include "llvm/Option/OptTable.h" 20 #include "llvm/Support/Process.h" 21 #include "llvm/Support/raw_ostream.h" 22 23 using namespace Fortran::frontend; 24 25 //===----------------------------------------------------------------------===// 26 // Initialization. 27 //===----------------------------------------------------------------------===// 28 CompilerInvocationBase::CompilerInvocationBase() 29 : diagnosticOpts_(new clang::DiagnosticOptions()) {} 30 31 CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &x) 32 : diagnosticOpts_(new clang::DiagnosticOptions(x.GetDiagnosticOpts())) {} 33 34 CompilerInvocationBase::~CompilerInvocationBase() = default; 35 36 //===----------------------------------------------------------------------===// 37 // Deserialization (from args) 38 //===----------------------------------------------------------------------===// 39 static bool parseShowColorsArgs( 40 const llvm::opt::ArgList &args, bool defaultColor) { 41 // Color diagnostics default to auto ("on" if terminal supports) in the driver 42 // but default to off in cc1, needing an explicit OPT_fdiagnostics_color. 43 // Support both clang's -f[no-]color-diagnostics and gcc's 44 // -f[no-]diagnostics-colors[=never|always|auto]. 45 enum { 46 Colors_On, 47 Colors_Off, 48 Colors_Auto 49 } ShowColors = defaultColor ? Colors_Auto : Colors_Off; 50 51 for (auto *a : args) { 52 const llvm::opt::Option &O = a->getOption(); 53 if (O.matches(clang::driver::options::OPT_fcolor_diagnostics) || 54 O.matches(clang::driver::options::OPT_fdiagnostics_color)) { 55 ShowColors = Colors_On; 56 } else if (O.matches(clang::driver::options::OPT_fno_color_diagnostics) || 57 O.matches(clang::driver::options::OPT_fno_diagnostics_color)) { 58 ShowColors = Colors_Off; 59 } else if (O.matches(clang::driver::options::OPT_fdiagnostics_color_EQ)) { 60 llvm::StringRef value(a->getValue()); 61 if (value == "always") 62 ShowColors = Colors_On; 63 else if (value == "never") 64 ShowColors = Colors_Off; 65 else if (value == "auto") 66 ShowColors = Colors_Auto; 67 } 68 } 69 70 return ShowColors == Colors_On || 71 (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()); 72 } 73 74 bool Fortran::frontend::ParseDiagnosticArgs(clang::DiagnosticOptions &opts, 75 llvm::opt::ArgList &args, bool defaultDiagColor) { 76 opts.ShowColors = parseShowColorsArgs(args, defaultDiagColor); 77 78 return true; 79 } 80 81 static InputKind ParseFrontendArgs(FrontendOptions &opts, 82 llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) { 83 // Identify the action (i.e. opts.ProgramAction) 84 if (const llvm::opt::Arg *a = 85 args.getLastArg(clang::driver::options::OPT_Action_Group)) { 86 switch (a->getOption().getID()) { 87 default: { 88 llvm_unreachable("Invalid option in group!"); 89 } 90 // TODO: 91 // case clang::driver::options::OPT_E: 92 // case clang::driver::options::OPT_emit_obj: 93 // case calng::driver::options::OPT_emit_llvm: 94 // case clang::driver::options::OPT_emit_llvm_only: 95 // case clang::driver::options::OPT_emit_codegen_only: 96 // case clang::driver::options::OPT_emit_module: 97 // (...) 98 } 99 } 100 101 opts.showHelp_ = args.hasArg(clang::driver::options::OPT_help); 102 opts.showVersion_ = args.hasArg(clang::driver::options::OPT_version); 103 104 // Get the input kind (from the value passed via `-x`) 105 InputKind dashX(Language::Unknown); 106 if (const llvm::opt::Arg *a = 107 args.getLastArg(clang::driver::options::OPT_x)) { 108 llvm::StringRef XValue = a->getValue(); 109 // Principal languages. 110 dashX = llvm::StringSwitch<InputKind>(XValue) 111 .Case("f90", Language::Fortran) 112 .Default(Language::Unknown); 113 114 // Some special cases cannot be combined with suffixes. 115 if (dashX.IsUnknown()) 116 dashX = llvm::StringSwitch<InputKind>(XValue) 117 .Case("ir", Language::LLVM_IR) 118 .Default(Language::Unknown); 119 120 if (dashX.IsUnknown()) 121 diags.Report(clang::diag::err_drv_invalid_value) 122 << a->getAsString(args) << a->getValue(); 123 } 124 125 return dashX; 126 } 127 128 bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res, 129 llvm::ArrayRef<const char *> commandLineArgs, 130 clang::DiagnosticsEngine &diags) { 131 132 bool success = true; 133 134 // Parse the arguments 135 const llvm::opt::OptTable &opts = clang::driver::getDriverOptTable(); 136 const unsigned includedFlagsBitmask = 137 clang::driver::options::FC1Option; 138 unsigned missingArgIndex, missingArgCount; 139 llvm::opt::InputArgList args = opts.ParseArgs( 140 commandLineArgs, missingArgIndex, missingArgCount, includedFlagsBitmask); 141 142 // Issue errors on unknown arguments 143 for (const auto *a : args.filtered(clang::driver::options::OPT_UNKNOWN)) { 144 auto argString = a->getAsString(args); 145 std::string nearest; 146 if (opts.findNearest(argString, nearest, includedFlagsBitmask) > 1) 147 diags.Report(clang::diag::err_drv_unknown_argument) << argString; 148 else 149 diags.Report(clang::diag::err_drv_unknown_argument_with_suggestion) 150 << argString << nearest; 151 success = false; 152 } 153 154 // Parse the frontend args 155 ParseFrontendArgs(res.GetFrontendOpts(), args, diags); 156 157 return success; 158 } 159