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 "flang/Common/Fortran-features.h" 11 #include "flang/Frontend/PreprocessorOptions.h" 12 #include "flang/Semantics/semantics.h" 13 #include "flang/Version.inc" 14 #include "clang/Basic/AllDiagnostics.h" 15 #include "clang/Basic/DiagnosticDriver.h" 16 #include "clang/Basic/DiagnosticOptions.h" 17 #include "clang/Driver/DriverDiagnostic.h" 18 #include "clang/Driver/Options.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/ADT/StringSwitch.h" 21 #include "llvm/Option/Arg.h" 22 #include "llvm/Option/ArgList.h" 23 #include "llvm/Option/OptTable.h" 24 #include "llvm/Support/Process.h" 25 #include "llvm/Support/raw_ostream.h" 26 #include <memory> 27 28 using namespace Fortran::frontend; 29 30 //===----------------------------------------------------------------------===// 31 // Initialization. 32 //===----------------------------------------------------------------------===// 33 CompilerInvocationBase::CompilerInvocationBase() 34 : diagnosticOpts_(new clang::DiagnosticOptions()), 35 preprocessorOpts_(new PreprocessorOptions()) {} 36 37 CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &x) 38 : diagnosticOpts_(new clang::DiagnosticOptions(x.GetDiagnosticOpts())), 39 preprocessorOpts_(new PreprocessorOptions(x.preprocessorOpts())) {} 40 41 CompilerInvocationBase::~CompilerInvocationBase() = default; 42 43 //===----------------------------------------------------------------------===// 44 // Deserialization (from args) 45 //===----------------------------------------------------------------------===// 46 static bool parseShowColorsArgs( 47 const llvm::opt::ArgList &args, bool defaultColor) { 48 // Color diagnostics default to auto ("on" if terminal supports) in the driver 49 // but default to off in cc1, needing an explicit OPT_fdiagnostics_color. 50 // Support both clang's -f[no-]color-diagnostics and gcc's 51 // -f[no-]diagnostics-colors[=never|always|auto]. 52 enum { 53 Colors_On, 54 Colors_Off, 55 Colors_Auto 56 } ShowColors = defaultColor ? Colors_Auto : Colors_Off; 57 58 for (auto *a : args) { 59 const llvm::opt::Option &O = a->getOption(); 60 if (O.matches(clang::driver::options::OPT_fcolor_diagnostics) || 61 O.matches(clang::driver::options::OPT_fdiagnostics_color)) { 62 ShowColors = Colors_On; 63 } else if (O.matches(clang::driver::options::OPT_fno_color_diagnostics) || 64 O.matches(clang::driver::options::OPT_fno_diagnostics_color)) { 65 ShowColors = Colors_Off; 66 } else if (O.matches(clang::driver::options::OPT_fdiagnostics_color_EQ)) { 67 llvm::StringRef value(a->getValue()); 68 if (value == "always") 69 ShowColors = Colors_On; 70 else if (value == "never") 71 ShowColors = Colors_Off; 72 else if (value == "auto") 73 ShowColors = Colors_Auto; 74 } 75 } 76 77 return ShowColors == Colors_On || 78 (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()); 79 } 80 81 bool Fortran::frontend::ParseDiagnosticArgs(clang::DiagnosticOptions &opts, 82 llvm::opt::ArgList &args, bool defaultDiagColor) { 83 opts.ShowColors = parseShowColorsArgs(args, defaultDiagColor); 84 85 return true; 86 } 87 88 static InputKind ParseFrontendArgs(FrontendOptions &opts, 89 llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) { 90 91 // By default the frontend driver creates a ParseSyntaxOnly action. 92 opts.programAction_ = ParseSyntaxOnly; 93 94 // Identify the action (i.e. opts.ProgramAction) 95 if (const llvm::opt::Arg *a = 96 args.getLastArg(clang::driver::options::OPT_Action_Group)) { 97 switch (a->getOption().getID()) { 98 default: { 99 llvm_unreachable("Invalid option in group!"); 100 } 101 case clang::driver::options::OPT_test_io: 102 opts.programAction_ = InputOutputTest; 103 break; 104 case clang::driver::options::OPT_E: 105 opts.programAction_ = PrintPreprocessedInput; 106 break; 107 case clang::driver::options::OPT_fsyntax_only: 108 opts.programAction_ = ParseSyntaxOnly; 109 break; 110 case clang::driver::options::OPT_emit_obj: 111 opts.programAction_ = EmitObj; 112 break; 113 case clang::driver::options::OPT_fdebug_unparse: 114 opts.programAction_ = DebugUnparse; 115 break; 116 case clang::driver::options::OPT_fdebug_unparse_with_symbols: 117 opts.programAction_ = DebugUnparseWithSymbols; 118 break; 119 case clang::driver::options::OPT_fdebug_dump_symbols: 120 opts.programAction_ = DebugDumpSymbols; 121 break; 122 case clang::driver::options::OPT_fdebug_dump_parse_tree: 123 opts.programAction_ = DebugDumpParseTree; 124 break; 125 case clang::driver::options::OPT_fdebug_dump_provenance: 126 opts.programAction_ = DebugDumpProvenance; 127 break; 128 129 // TODO: 130 // case calng::driver::options::OPT_emit_llvm: 131 // case clang::driver::options::OPT_emit_llvm_only: 132 // case clang::driver::options::OPT_emit_codegen_only: 133 // case clang::driver::options::OPT_emit_module: 134 // (...) 135 } 136 } 137 138 opts.outputFile_ = args.getLastArgValue(clang::driver::options::OPT_o); 139 opts.showHelp_ = args.hasArg(clang::driver::options::OPT_help); 140 opts.showVersion_ = args.hasArg(clang::driver::options::OPT_version); 141 142 // Get the input kind (from the value passed via `-x`) 143 InputKind dashX(Language::Unknown); 144 if (const llvm::opt::Arg *a = 145 args.getLastArg(clang::driver::options::OPT_x)) { 146 llvm::StringRef XValue = a->getValue(); 147 // Principal languages. 148 dashX = llvm::StringSwitch<InputKind>(XValue) 149 .Case("f90", Language::Fortran) 150 .Default(Language::Unknown); 151 152 // Some special cases cannot be combined with suffixes. 153 if (dashX.IsUnknown()) 154 dashX = llvm::StringSwitch<InputKind>(XValue) 155 .Case("ir", Language::LLVM_IR) 156 .Default(Language::Unknown); 157 158 if (dashX.IsUnknown()) 159 diags.Report(clang::diag::err_drv_invalid_value) 160 << a->getAsString(args) << a->getValue(); 161 } 162 163 // Collect the input files and save them in our instance of FrontendOptions. 164 std::vector<std::string> inputs = 165 args.getAllArgValues(clang::driver::options::OPT_INPUT); 166 opts.inputs_.clear(); 167 if (inputs.empty()) 168 // '-' is the default input if none is given. 169 inputs.push_back("-"); 170 for (unsigned i = 0, e = inputs.size(); i != e; ++i) { 171 InputKind ik = dashX; 172 if (ik.IsUnknown()) { 173 ik = FrontendOptions::GetInputKindForExtension( 174 llvm::StringRef(inputs[i]).rsplit('.').second); 175 if (ik.IsUnknown()) 176 ik = Language::Unknown; 177 if (i == 0) 178 dashX = ik; 179 } 180 181 opts.inputs_.emplace_back(std::move(inputs[i]), ik); 182 } 183 184 // Set fortranForm_ based on options -ffree-form and -ffixed-form. 185 if (const auto *arg = args.getLastArg(clang::driver::options::OPT_ffixed_form, 186 clang::driver::options::OPT_ffree_form)) { 187 opts.fortranForm_ = 188 arg->getOption().matches(clang::driver::options::OPT_ffixed_form) 189 ? FortranForm::FixedForm 190 : FortranForm::FreeForm; 191 } 192 193 // Set fixedFormColumns_ based on -ffixed-line-length=<value> 194 if (const auto *arg = 195 args.getLastArg(clang::driver::options::OPT_ffixed_line_length_EQ)) { 196 llvm::StringRef argValue = llvm::StringRef(arg->getValue()); 197 std::int64_t columns = -1; 198 if (argValue == "none") { 199 columns = 0; 200 } else if (argValue.getAsInteger(/*Radix=*/10, columns)) { 201 columns = -1; 202 } 203 if (columns < 0) { 204 diags.Report(clang::diag::err_drv_invalid_value_with_suggestion) 205 << arg->getOption().getName() << arg->getValue() 206 << "value must be 'none' or a non-negative integer"; 207 } else if (columns == 0) { 208 opts.fixedFormColumns_ = 1000000; 209 } else if (columns < 7) { 210 diags.Report(clang::diag::err_drv_invalid_value_with_suggestion) 211 << arg->getOption().getName() << arg->getValue() 212 << "value must be at least seven"; 213 } else { 214 opts.fixedFormColumns_ = columns; 215 } 216 } 217 218 // Extensions 219 if (args.hasArg(clang::driver::options::OPT_fopenacc)) { 220 opts.features_.Enable(Fortran::common::LanguageFeature::OpenACC); 221 } 222 if (args.hasArg(clang::driver::options::OPT_fopenmp)) { 223 opts.features_.Enable(Fortran::common::LanguageFeature::OpenMP); 224 } 225 if (const llvm::opt::Arg *arg = 226 args.getLastArg(clang::driver::options::OPT_fimplicit_none, 227 clang::driver::options::OPT_fno_implicit_none)) { 228 opts.features_.Enable( 229 Fortran::common::LanguageFeature::ImplicitNoneTypeAlways, 230 arg->getOption().matches(clang::driver::options::OPT_fimplicit_none)); 231 } 232 if (const llvm::opt::Arg *arg = 233 args.getLastArg(clang::driver::options::OPT_fbackslash, 234 clang::driver::options::OPT_fno_backslash)) { 235 opts.features_.Enable(Fortran::common::LanguageFeature::BackslashEscapes, 236 arg->getOption().matches(clang::driver::options::OPT_fbackslash)); 237 } 238 if (const llvm::opt::Arg *arg = 239 args.getLastArg(clang::driver::options::OPT_flogical_abbreviations, 240 clang::driver::options::OPT_fno_logical_abbreviations)) { 241 opts.features_.Enable( 242 Fortran::common::LanguageFeature::LogicalAbbreviations, 243 arg->getOption().matches( 244 clang::driver::options::OPT_flogical_abbreviations)); 245 } 246 if (const llvm::opt::Arg *arg = 247 args.getLastArg(clang::driver::options::OPT_fxor_operator, 248 clang::driver::options::OPT_fno_xor_operator)) { 249 opts.features_.Enable(Fortran::common::LanguageFeature::XOROperator, 250 arg->getOption().matches(clang::driver::options::OPT_fxor_operator)); 251 } 252 if (args.hasArg( 253 clang::driver::options::OPT_falternative_parameter_statement)) { 254 opts.features_.Enable(Fortran::common::LanguageFeature::OldStyleParameter); 255 } 256 if (const llvm::opt::Arg *arg = 257 args.getLastArg(clang::driver::options::OPT_finput_charset_EQ)) { 258 llvm::StringRef argValue = arg->getValue(); 259 if (argValue == "utf-8") { 260 opts.encoding_ = Fortran::parser::Encoding::UTF_8; 261 } else if (argValue == "latin-1") { 262 opts.encoding_ = Fortran::parser::Encoding::LATIN_1; 263 } else { 264 diags.Report(clang::diag::err_drv_invalid_value) 265 << arg->getAsString(args) << argValue; 266 } 267 } 268 return dashX; 269 } 270 271 /// Parses all preprocessor input arguments and populates the preprocessor 272 /// options accordingly. 273 /// 274 /// \param [in] opts The preprocessor options instance 275 /// \param [out] args The list of input arguments 276 static void parsePreprocessorArgs( 277 Fortran::frontend::PreprocessorOptions &opts, llvm::opt::ArgList &args) { 278 // Add macros from the command line. 279 for (const auto *currentArg : args.filtered( 280 clang::driver::options::OPT_D, clang::driver::options::OPT_U)) { 281 if (currentArg->getOption().matches(clang::driver::options::OPT_D)) { 282 opts.addMacroDef(currentArg->getValue()); 283 } else { 284 opts.addMacroUndef(currentArg->getValue()); 285 } 286 } 287 288 // Add the ordered list of -I's. 289 for (const auto *currentArg : args.filtered(clang::driver::options::OPT_I)) 290 opts.searchDirectoriesFromDashI.emplace_back(currentArg->getValue()); 291 } 292 293 /// Parses all semantic related arguments and populates the variables 294 /// options accordingly. 295 static void parseSemaArgs(std::string &moduleDir, llvm::opt::ArgList &args, 296 clang::DiagnosticsEngine &diags) { 297 298 auto moduleDirList = 299 args.getAllArgValues(clang::driver::options::OPT_module_dir); 300 // User can only specify -J/-module-dir once 301 // https://gcc.gnu.org/onlinedocs/gfortran/Directory-Options.html 302 if (moduleDirList.size() > 1) { 303 const unsigned diagID = 304 diags.getCustomDiagID(clang::DiagnosticsEngine::Error, 305 "Only one '-module-dir/-J' option allowed"); 306 diags.Report(diagID); 307 } 308 if (moduleDirList.size() == 1) 309 moduleDir = moduleDirList[0]; 310 } 311 312 bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res, 313 llvm::ArrayRef<const char *> commandLineArgs, 314 clang::DiagnosticsEngine &diags) { 315 316 bool success = true; 317 318 // Parse the arguments 319 const llvm::opt::OptTable &opts = clang::driver::getDriverOptTable(); 320 const unsigned includedFlagsBitmask = clang::driver::options::FC1Option; 321 unsigned missingArgIndex, missingArgCount; 322 llvm::opt::InputArgList args = opts.ParseArgs( 323 commandLineArgs, missingArgIndex, missingArgCount, includedFlagsBitmask); 324 325 // Issue errors on unknown arguments 326 for (const auto *a : args.filtered(clang::driver::options::OPT_UNKNOWN)) { 327 auto argString = a->getAsString(args); 328 std::string nearest; 329 if (opts.findNearest(argString, nearest, includedFlagsBitmask) > 1) 330 diags.Report(clang::diag::err_drv_unknown_argument) << argString; 331 else 332 diags.Report(clang::diag::err_drv_unknown_argument_with_suggestion) 333 << argString << nearest; 334 success = false; 335 } 336 337 // Parse the frontend args 338 ParseFrontendArgs(res.frontendOpts(), args, diags); 339 // Parse the preprocessor args 340 parsePreprocessorArgs(res.preprocessorOpts(), args); 341 // Parse semantic args 342 parseSemaArgs(res.moduleDir(), args, diags); 343 344 return success; 345 } 346 347 /// Collect the macro definitions provided by the given preprocessor 348 /// options into the parser options. 349 /// 350 /// \param [in] ppOpts The preprocessor options 351 /// \param [out] opts The fortran options 352 static void collectMacroDefinitions( 353 const PreprocessorOptions &ppOpts, Fortran::parser::Options &opts) { 354 for (unsigned i = 0, n = ppOpts.macros.size(); i != n; ++i) { 355 llvm::StringRef macro = ppOpts.macros[i].first; 356 bool isUndef = ppOpts.macros[i].second; 357 358 std::pair<llvm::StringRef, llvm::StringRef> macroPair = macro.split('='); 359 llvm::StringRef macroName = macroPair.first; 360 llvm::StringRef macroBody = macroPair.second; 361 362 // For an #undef'd macro, we only care about the name. 363 if (isUndef) { 364 opts.predefinitions.emplace_back( 365 macroName.str(), std::optional<std::string>{}); 366 continue; 367 } 368 369 // For a #define'd macro, figure out the actual definition. 370 if (macroName.size() == macro.size()) 371 macroBody = "1"; 372 else { 373 // Note: GCC drops anything following an end-of-line character. 374 llvm::StringRef::size_type End = macroBody.find_first_of("\n\r"); 375 macroBody = macroBody.substr(0, End); 376 } 377 opts.predefinitions.emplace_back( 378 macroName, std::optional<std::string>(macroBody.str())); 379 } 380 } 381 382 void CompilerInvocation::SetDefaultFortranOpts() { 383 auto &fortranOptions = fortranOpts(); 384 385 // These defaults are based on the defaults in f18/f18.cpp. 386 std::vector<std::string> searchDirectories{"."s}; 387 fortranOptions.searchDirectories = searchDirectories; 388 fortranOptions.isFixedForm = false; 389 } 390 391 // TODO: When expanding this method, consider creating a dedicated API for 392 // this. Also at some point we will need to differentiate between different 393 // targets and add dedicated predefines for each. 394 void CompilerInvocation::setDefaultPredefinitions() { 395 auto &fortranOptions = fortranOpts(); 396 const auto &frontendOptions = frontendOpts(); 397 398 // Populate the macro list with version numbers and other predefinitions. 399 fortranOptions.predefinitions.emplace_back("__flang__", "1"); 400 fortranOptions.predefinitions.emplace_back( 401 "__flang_major__", FLANG_VERSION_MAJOR_STRING); 402 fortranOptions.predefinitions.emplace_back( 403 "__flang_minor__", FLANG_VERSION_MINOR_STRING); 404 fortranOptions.predefinitions.emplace_back( 405 "__flang_patchlevel__", FLANG_VERSION_PATCHLEVEL_STRING); 406 407 // Add predefinitions based on extensions enabled 408 if (frontendOptions.features_.IsEnabled( 409 Fortran::common::LanguageFeature::OpenACC)) { 410 fortranOptions.predefinitions.emplace_back("_OPENACC", "202011"); 411 } 412 if (frontendOptions.features_.IsEnabled( 413 Fortran::common::LanguageFeature::OpenMP)) { 414 fortranOptions.predefinitions.emplace_back("_OPENMP", "201511"); 415 } 416 } 417 418 void CompilerInvocation::setFortranOpts() { 419 auto &fortranOptions = fortranOpts(); 420 const auto &frontendOptions = frontendOpts(); 421 const auto &preprocessorOptions = preprocessorOpts(); 422 auto &moduleDirJ = moduleDir(); 423 424 if (frontendOptions.fortranForm_ != FortranForm::Unknown) { 425 fortranOptions.isFixedForm = 426 frontendOptions.fortranForm_ == FortranForm::FixedForm; 427 } 428 fortranOptions.fixedFormColumns = frontendOptions.fixedFormColumns_; 429 430 fortranOptions.features = frontendOptions.features_; 431 fortranOptions.encoding = frontendOptions.encoding_; 432 433 collectMacroDefinitions(preprocessorOptions, fortranOptions); 434 435 fortranOptions.searchDirectories.insert( 436 fortranOptions.searchDirectories.end(), 437 preprocessorOptions.searchDirectoriesFromDashI.begin(), 438 preprocessorOptions.searchDirectoriesFromDashI.end()); 439 440 // Add the directory supplied through -J/-module-dir to the list of search 441 // directories 442 if (moduleDirJ.compare(".") != 0) 443 fortranOptions.searchDirectories.emplace_back(moduleDirJ); 444 } 445 446 void CompilerInvocation::setSemanticsOpts( 447 Fortran::parser::AllCookedSources &allCookedSources) { 448 const auto &fortranOptions = fortranOpts(); 449 450 semanticsContext_ = std::make_unique<semantics::SemanticsContext>( 451 *(new Fortran::common::IntrinsicTypeDefaultKinds()), 452 fortranOptions.features, allCookedSources); 453 454 auto &moduleDirJ = moduleDir(); 455 semanticsContext_->set_moduleDirectory(moduleDirJ) 456 .set_searchDirectories(fortranOptions.searchDirectories); 457 } 458