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 case clang::driver::options::OPT_test_io: 91 opts.programAction_ = InputOutputTest; 92 break; 93 // TODO: 94 // case clang::driver::options::OPT_E: 95 // case clang::driver::options::OPT_emit_obj: 96 // case calng::driver::options::OPT_emit_llvm: 97 // case clang::driver::options::OPT_emit_llvm_only: 98 // case clang::driver::options::OPT_emit_codegen_only: 99 // case clang::driver::options::OPT_emit_module: 100 // (...) 101 } 102 } 103 104 opts.outputFile_ = args.getLastArgValue(clang::driver::options::OPT_o); 105 opts.showHelp_ = args.hasArg(clang::driver::options::OPT_help); 106 opts.showVersion_ = args.hasArg(clang::driver::options::OPT_version); 107 108 // Get the input kind (from the value passed via `-x`) 109 InputKind dashX(Language::Unknown); 110 if (const llvm::opt::Arg *a = 111 args.getLastArg(clang::driver::options::OPT_x)) { 112 llvm::StringRef XValue = a->getValue(); 113 // Principal languages. 114 dashX = llvm::StringSwitch<InputKind>(XValue) 115 .Case("f90", Language::Fortran) 116 .Default(Language::Unknown); 117 118 // Some special cases cannot be combined with suffixes. 119 if (dashX.IsUnknown()) 120 dashX = llvm::StringSwitch<InputKind>(XValue) 121 .Case("ir", Language::LLVM_IR) 122 .Default(Language::Unknown); 123 124 if (dashX.IsUnknown()) 125 diags.Report(clang::diag::err_drv_invalid_value) 126 << a->getAsString(args) << a->getValue(); 127 } 128 129 // Collect the input files and save them in our instance of FrontendOptions. 130 std::vector<std::string> inputs = 131 args.getAllArgValues(clang::driver::options::OPT_INPUT); 132 opts.inputs_.clear(); 133 if (inputs.empty()) 134 // '-' is the default input if none is given. 135 inputs.push_back("-"); 136 for (unsigned i = 0, e = inputs.size(); i != e; ++i) { 137 InputKind ik = dashX; 138 if (ik.IsUnknown()) { 139 ik = FrontendOptions::GetInputKindForExtension( 140 llvm::StringRef(inputs[i]).rsplit('.').second); 141 if (ik.IsUnknown()) 142 ik = Language::Unknown; 143 if (i == 0) 144 dashX = ik; 145 } 146 147 opts.inputs_.emplace_back(std::move(inputs[i]), ik); 148 } 149 return dashX; 150 } 151 152 bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res, 153 llvm::ArrayRef<const char *> commandLineArgs, 154 clang::DiagnosticsEngine &diags) { 155 156 bool success = true; 157 158 // Parse the arguments 159 const llvm::opt::OptTable &opts = clang::driver::getDriverOptTable(); 160 const unsigned includedFlagsBitmask = 161 clang::driver::options::FC1Option; 162 unsigned missingArgIndex, missingArgCount; 163 llvm::opt::InputArgList args = opts.ParseArgs( 164 commandLineArgs, missingArgIndex, missingArgCount, includedFlagsBitmask); 165 166 // Issue errors on unknown arguments 167 for (const auto *a : args.filtered(clang::driver::options::OPT_UNKNOWN)) { 168 auto argString = a->getAsString(args); 169 std::string nearest; 170 if (opts.findNearest(argString, nearest, includedFlagsBitmask) > 1) 171 diags.Report(clang::diag::err_drv_unknown_argument) << argString; 172 else 173 diags.Report(clang::diag::err_drv_unknown_argument_with_suggestion) 174 << argString << nearest; 175 success = false; 176 } 177 178 // Parse the frontend args 179 ParseFrontendArgs(res.GetFrontendOpts(), args, diags); 180 181 return success; 182 } 183