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