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/FileSystem.h" 25 #include "llvm/Support/FileUtilities.h" 26 #include "llvm/Support/Process.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include <memory> 29 30 using namespace Fortran::frontend; 31 32 //===----------------------------------------------------------------------===// 33 // Initialization. 34 //===----------------------------------------------------------------------===// 35 CompilerInvocationBase::CompilerInvocationBase() 36 : diagnosticOpts_(new clang::DiagnosticOptions()), 37 preprocessorOpts_(new PreprocessorOptions()) {} 38 39 CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &x) 40 : diagnosticOpts_(new clang::DiagnosticOptions(x.GetDiagnosticOpts())), 41 preprocessorOpts_(new PreprocessorOptions(x.preprocessorOpts())) {} 42 43 CompilerInvocationBase::~CompilerInvocationBase() = default; 44 45 //===----------------------------------------------------------------------===// 46 // Deserialization (from args) 47 //===----------------------------------------------------------------------===// 48 static bool parseShowColorsArgs( 49 const llvm::opt::ArgList &args, bool defaultColor) { 50 // Color diagnostics default to auto ("on" if terminal supports) in the driver 51 // but default to off in cc1, needing an explicit OPT_fdiagnostics_color. 52 // Support both clang's -f[no-]color-diagnostics and gcc's 53 // -f[no-]diagnostics-colors[=never|always|auto]. 54 enum { 55 Colors_On, 56 Colors_Off, 57 Colors_Auto 58 } ShowColors = defaultColor ? Colors_Auto : Colors_Off; 59 60 for (auto *a : args) { 61 const llvm::opt::Option &O = a->getOption(); 62 if (O.matches(clang::driver::options::OPT_fcolor_diagnostics) || 63 O.matches(clang::driver::options::OPT_fdiagnostics_color)) { 64 ShowColors = Colors_On; 65 } else if (O.matches(clang::driver::options::OPT_fno_color_diagnostics) || 66 O.matches(clang::driver::options::OPT_fno_diagnostics_color)) { 67 ShowColors = Colors_Off; 68 } else if (O.matches(clang::driver::options::OPT_fdiagnostics_color_EQ)) { 69 llvm::StringRef value(a->getValue()); 70 if (value == "always") 71 ShowColors = Colors_On; 72 else if (value == "never") 73 ShowColors = Colors_Off; 74 else if (value == "auto") 75 ShowColors = Colors_Auto; 76 } 77 } 78 79 return ShowColors == Colors_On || 80 (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()); 81 } 82 83 bool Fortran::frontend::ParseDiagnosticArgs(clang::DiagnosticOptions &opts, 84 llvm::opt::ArgList &args, bool defaultDiagColor) { 85 opts.ShowColors = parseShowColorsArgs(args, defaultDiagColor); 86 87 return true; 88 } 89 90 // Tweak the frontend configuration based on the frontend action 91 static void setUpFrontendBasedOnAction(FrontendOptions &opts) { 92 assert(opts.programAction_ != Fortran::frontend::InvalidAction && 93 "Fortran frontend action not set!"); 94 95 if (opts.programAction_ == DebugDumpParsingLog) 96 opts.instrumentedParse_ = true; 97 98 if (opts.programAction_ == DebugDumpProvenance || 99 opts.programAction_ == Fortran::frontend::GetDefinition) 100 opts.needProvenanceRangeToCharBlockMappings_ = true; 101 } 102 103 static bool ParseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args, 104 clang::DiagnosticsEngine &diags) { 105 unsigned numErrorsBefore = diags.getNumErrors(); 106 107 // By default the frontend driver creates a ParseSyntaxOnly action. 108 opts.programAction_ = ParseSyntaxOnly; 109 110 // Identify the action (i.e. opts.ProgramAction) 111 if (const llvm::opt::Arg *a = 112 args.getLastArg(clang::driver::options::OPT_Action_Group)) { 113 switch (a->getOption().getID()) { 114 default: { 115 llvm_unreachable("Invalid option in group!"); 116 } 117 case clang::driver::options::OPT_test_io: 118 opts.programAction_ = InputOutputTest; 119 break; 120 case clang::driver::options::OPT_E: 121 opts.programAction_ = PrintPreprocessedInput; 122 break; 123 case clang::driver::options::OPT_fsyntax_only: 124 opts.programAction_ = ParseSyntaxOnly; 125 break; 126 case clang::driver::options::OPT_emit_obj: 127 opts.programAction_ = EmitObj; 128 break; 129 case clang::driver::options::OPT_fdebug_unparse: 130 opts.programAction_ = DebugUnparse; 131 break; 132 case clang::driver::options::OPT_fdebug_unparse_no_sema: 133 opts.programAction_ = DebugUnparseNoSema; 134 break; 135 case clang::driver::options::OPT_fdebug_unparse_with_symbols: 136 opts.programAction_ = DebugUnparseWithSymbols; 137 break; 138 case clang::driver::options::OPT_fdebug_dump_symbols: 139 opts.programAction_ = DebugDumpSymbols; 140 break; 141 case clang::driver::options::OPT_fdebug_dump_parse_tree: 142 opts.programAction_ = DebugDumpParseTree; 143 break; 144 case clang::driver::options::OPT_fdebug_dump_parse_tree_no_sema: 145 opts.programAction_ = DebugDumpParseTreeNoSema; 146 break; 147 case clang::driver::options::OPT_fdebug_dump_provenance: 148 opts.programAction_ = DebugDumpProvenance; 149 break; 150 case clang::driver::options::OPT_fdebug_dump_parsing_log: 151 opts.programAction_ = DebugDumpParsingLog; 152 break; 153 case clang::driver::options::OPT_fdebug_measure_parse_tree: 154 opts.programAction_ = DebugMeasureParseTree; 155 break; 156 case clang::driver::options::OPT_fdebug_pre_fir_tree: 157 opts.programAction_ = DebugPreFIRTree; 158 break; 159 case clang::driver::options::OPT_fget_symbols_sources: 160 opts.programAction_ = GetSymbolsSources; 161 break; 162 case clang::driver::options::OPT_fget_definition: 163 opts.programAction_ = GetDefinition; 164 break; 165 166 // TODO: 167 // case calng::driver::options::OPT_emit_llvm: 168 // case clang::driver::options::OPT_emit_llvm_only: 169 // case clang::driver::options::OPT_emit_codegen_only: 170 // case clang::driver::options::OPT_emit_module: 171 // (...) 172 } 173 174 // Parse the values provided with `-fget-definition` (there should be 3 175 // integers) 176 if (llvm::opt::OptSpecifier(a->getOption().getID()) == 177 clang::driver::options::OPT_fget_definition) { 178 unsigned optVals[3] = {0, 0, 0}; 179 180 for (unsigned i = 0; i < 3; i++) { 181 llvm::StringRef val = a->getValue(i); 182 183 if (val.getAsInteger(10, optVals[i])) { 184 // A non-integer was encountered - that's an error. 185 diags.Report(clang::diag::err_drv_invalid_value) 186 << a->getOption().getName() << val; 187 break; 188 } 189 } 190 opts.getDefVals_.line = optVals[0]; 191 opts.getDefVals_.startColumn = optVals[1]; 192 opts.getDefVals_.endColumn = optVals[2]; 193 } 194 } 195 196 opts.outputFile_ = args.getLastArgValue(clang::driver::options::OPT_o); 197 opts.showHelp_ = args.hasArg(clang::driver::options::OPT_help); 198 opts.showVersion_ = args.hasArg(clang::driver::options::OPT_version); 199 200 // Get the input kind (from the value passed via `-x`) 201 InputKind dashX(Language::Unknown); 202 if (const llvm::opt::Arg *a = 203 args.getLastArg(clang::driver::options::OPT_x)) { 204 llvm::StringRef XValue = a->getValue(); 205 // Principal languages. 206 dashX = llvm::StringSwitch<InputKind>(XValue) 207 .Case("f90", Language::Fortran) 208 .Default(Language::Unknown); 209 210 // Some special cases cannot be combined with suffixes. 211 if (dashX.IsUnknown()) 212 dashX = llvm::StringSwitch<InputKind>(XValue) 213 .Case("ir", Language::LLVM_IR) 214 .Default(Language::Unknown); 215 216 if (dashX.IsUnknown()) 217 diags.Report(clang::diag::err_drv_invalid_value) 218 << a->getAsString(args) << a->getValue(); 219 } 220 221 // Collect the input files and save them in our instance of FrontendOptions. 222 std::vector<std::string> inputs = 223 args.getAllArgValues(clang::driver::options::OPT_INPUT); 224 opts.inputs_.clear(); 225 if (inputs.empty()) 226 // '-' is the default input if none is given. 227 inputs.push_back("-"); 228 for (unsigned i = 0, e = inputs.size(); i != e; ++i) { 229 InputKind ik = dashX; 230 if (ik.IsUnknown()) { 231 ik = FrontendOptions::GetInputKindForExtension( 232 llvm::StringRef(inputs[i]).rsplit('.').second); 233 if (ik.IsUnknown()) 234 ik = Language::Unknown; 235 if (i == 0) 236 dashX = ik; 237 } 238 239 opts.inputs_.emplace_back(std::move(inputs[i]), ik); 240 } 241 242 // Set fortranForm_ based on options -ffree-form and -ffixed-form. 243 if (const auto *arg = args.getLastArg(clang::driver::options::OPT_ffixed_form, 244 clang::driver::options::OPT_ffree_form)) { 245 opts.fortranForm_ = 246 arg->getOption().matches(clang::driver::options::OPT_ffixed_form) 247 ? FortranForm::FixedForm 248 : FortranForm::FreeForm; 249 } 250 251 // Set fixedFormColumns_ based on -ffixed-line-length=<value> 252 if (const auto *arg = 253 args.getLastArg(clang::driver::options::OPT_ffixed_line_length_EQ)) { 254 llvm::StringRef argValue = llvm::StringRef(arg->getValue()); 255 std::int64_t columns = -1; 256 if (argValue == "none") { 257 columns = 0; 258 } else if (argValue.getAsInteger(/*Radix=*/10, columns)) { 259 columns = -1; 260 } 261 if (columns < 0) { 262 diags.Report(clang::diag::err_drv_negative_columns) 263 << arg->getOption().getName() << arg->getValue(); 264 } else if (columns == 0) { 265 opts.fixedFormColumns_ = 1000000; 266 } else if (columns < 7) { 267 diags.Report(clang::diag::err_drv_small_columns) 268 << arg->getOption().getName() << arg->getValue() << "7"; 269 } else { 270 opts.fixedFormColumns_ = columns; 271 } 272 } 273 274 if (const llvm::opt::Arg *arg = 275 args.getLastArg(clang::driver::options::OPT_fimplicit_none, 276 clang::driver::options::OPT_fno_implicit_none)) { 277 opts.features_.Enable( 278 Fortran::common::LanguageFeature::ImplicitNoneTypeAlways, 279 arg->getOption().matches(clang::driver::options::OPT_fimplicit_none)); 280 } 281 if (const llvm::opt::Arg *arg = 282 args.getLastArg(clang::driver::options::OPT_fbackslash, 283 clang::driver::options::OPT_fno_backslash)) { 284 opts.features_.Enable(Fortran::common::LanguageFeature::BackslashEscapes, 285 arg->getOption().matches(clang::driver::options::OPT_fbackslash)); 286 } 287 if (const llvm::opt::Arg *arg = 288 args.getLastArg(clang::driver::options::OPT_flogical_abbreviations, 289 clang::driver::options::OPT_fno_logical_abbreviations)) { 290 opts.features_.Enable( 291 Fortran::common::LanguageFeature::LogicalAbbreviations, 292 arg->getOption().matches( 293 clang::driver::options::OPT_flogical_abbreviations)); 294 } 295 if (const llvm::opt::Arg *arg = 296 args.getLastArg(clang::driver::options::OPT_fxor_operator, 297 clang::driver::options::OPT_fno_xor_operator)) { 298 opts.features_.Enable(Fortran::common::LanguageFeature::XOROperator, 299 arg->getOption().matches(clang::driver::options::OPT_fxor_operator)); 300 } 301 if (args.hasArg( 302 clang::driver::options::OPT_falternative_parameter_statement)) { 303 opts.features_.Enable(Fortran::common::LanguageFeature::OldStyleParameter); 304 } 305 if (const llvm::opt::Arg *arg = 306 args.getLastArg(clang::driver::options::OPT_finput_charset_EQ)) { 307 llvm::StringRef argValue = arg->getValue(); 308 if (argValue == "utf-8") { 309 opts.encoding_ = Fortran::parser::Encoding::UTF_8; 310 } else if (argValue == "latin-1") { 311 opts.encoding_ = Fortran::parser::Encoding::LATIN_1; 312 } else { 313 diags.Report(clang::diag::err_drv_invalid_value) 314 << arg->getAsString(args) << argValue; 315 } 316 } 317 318 setUpFrontendBasedOnAction(opts); 319 opts.dashX_ = dashX; 320 321 return diags.getNumErrors() == numErrorsBefore; 322 } 323 324 // Generate the path to look for intrinsic modules 325 static std::string getIntrinsicDir() { 326 // TODO: Find a system independent API 327 llvm::SmallString<128> driverPath; 328 driverPath.assign(llvm::sys::fs::getMainExecutable(nullptr, nullptr)); 329 llvm::sys::path::remove_filename(driverPath); 330 driverPath.append("/../include/flang/"); 331 return std::string(driverPath); 332 } 333 334 /// Parses all preprocessor input arguments and populates the preprocessor 335 /// options accordingly. 336 /// 337 /// \param [in] opts The preprocessor options instance 338 /// \param [out] args The list of input arguments 339 static void parsePreprocessorArgs( 340 Fortran::frontend::PreprocessorOptions &opts, llvm::opt::ArgList &args) { 341 // Add macros from the command line. 342 for (const auto *currentArg : args.filtered( 343 clang::driver::options::OPT_D, clang::driver::options::OPT_U)) { 344 if (currentArg->getOption().matches(clang::driver::options::OPT_D)) { 345 opts.addMacroDef(currentArg->getValue()); 346 } else { 347 opts.addMacroUndef(currentArg->getValue()); 348 } 349 } 350 351 // Add the ordered list of -I's. 352 for (const auto *currentArg : args.filtered(clang::driver::options::OPT_I)) 353 opts.searchDirectoriesFromDashI.emplace_back(currentArg->getValue()); 354 355 // Prepend the ordered list of -intrinsic-modules-path 356 // to the default location to search. 357 for (const auto *currentArg : 358 args.filtered(clang::driver::options::OPT_fintrinsic_modules_path)) 359 opts.searchDirectoriesFromIntrModPath.emplace_back(currentArg->getValue()); 360 361 // -cpp/-nocpp 362 if (const auto *currentArg = args.getLastArg( 363 clang::driver::options::OPT_cpp, clang::driver::options::OPT_nocpp)) 364 opts.macrosFlag_ = 365 (currentArg->getOption().matches(clang::driver::options::OPT_cpp)) 366 ? PPMacrosFlag::Include 367 : PPMacrosFlag::Exclude; 368 } 369 370 /// Parses all semantic related arguments and populates the variables 371 /// options accordingly. Returns false if new errors are generated. 372 static bool parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args, 373 clang::DiagnosticsEngine &diags) { 374 unsigned numErrorsBefore = diags.getNumErrors(); 375 376 // -J/module-dir option 377 auto moduleDirList = 378 args.getAllArgValues(clang::driver::options::OPT_module_dir); 379 // User can only specify -J/-module-dir once 380 // https://gcc.gnu.org/onlinedocs/gfortran/Directory-Options.html 381 if (moduleDirList.size() > 1) { 382 const unsigned diagID = 383 diags.getCustomDiagID(clang::DiagnosticsEngine::Error, 384 "Only one '-module-dir/-J' option allowed"); 385 diags.Report(diagID); 386 } 387 if (moduleDirList.size() == 1) 388 res.SetModuleDir(moduleDirList[0]); 389 390 // -fdebug-module-writer option 391 if (args.hasArg(clang::driver::options::OPT_fdebug_module_writer)) { 392 res.SetDebugModuleDir(true); 393 } 394 395 // -module-suffix 396 if (const auto *moduleSuffix = 397 args.getLastArg(clang::driver::options::OPT_module_suffix)) { 398 res.SetModuleFileSuffix(moduleSuffix->getValue()); 399 } 400 401 return diags.getNumErrors() == numErrorsBefore; 402 } 403 404 /// Parses all diagnostics related arguments and populates the variables 405 /// options accordingly. Returns false if new errors are generated. 406 static bool parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args, 407 clang::DiagnosticsEngine &diags) { 408 unsigned numErrorsBefore = diags.getNumErrors(); 409 410 // -Werror option 411 // TODO: Currently throws a Diagnostic for anything other than -W<error>, 412 // this has to change when other -W<opt>'s are supported. 413 if (args.hasArg(clang::driver::options::OPT_W_Joined)) { 414 if (args.getLastArgValue(clang::driver::options::OPT_W_Joined) 415 .equals("error")) { 416 res.SetWarnAsErr(true); 417 } else { 418 const unsigned diagID = 419 diags.getCustomDiagID(clang::DiagnosticsEngine::Error, 420 "Only `-Werror` is supported currently."); 421 diags.Report(diagID); 422 } 423 } 424 425 return diags.getNumErrors() == numErrorsBefore; 426 } 427 428 /// Parses all Dialect related arguments and populates the variables 429 /// options accordingly. Returns false if new errors are generated. 430 static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args, 431 clang::DiagnosticsEngine &diags) { 432 unsigned numErrorsBefore = diags.getNumErrors(); 433 434 // -fdefault* family 435 if (args.hasArg(clang::driver::options::OPT_fdefault_real_8)) { 436 res.defaultKinds().set_defaultRealKind(8); 437 res.defaultKinds().set_doublePrecisionKind(16); 438 } 439 if (args.hasArg(clang::driver::options::OPT_fdefault_integer_8)) { 440 res.defaultKinds().set_defaultIntegerKind(8); 441 res.defaultKinds().set_subscriptIntegerKind(8); 442 res.defaultKinds().set_sizeIntegerKind(8); 443 } 444 if (args.hasArg(clang::driver::options::OPT_fdefault_double_8)) { 445 if (!args.hasArg(clang::driver::options::OPT_fdefault_real_8)) { 446 // -fdefault-double-8 has to be used with -fdefault-real-8 447 // to be compatible with gfortran 448 const unsigned diagID = 449 diags.getCustomDiagID(clang::DiagnosticsEngine::Error, 450 "Use of `-fdefault-double-8` requires `-fdefault-real-8`"); 451 diags.Report(diagID); 452 } 453 // https://gcc.gnu.org/onlinedocs/gfortran/Fortran-Dialect-Options.html 454 res.defaultKinds().set_doublePrecisionKind(8); 455 } 456 if (args.hasArg(clang::driver::options::OPT_flarge_sizes)) 457 res.defaultKinds().set_sizeIntegerKind(8); 458 459 // -fopenmp and -fopenacc 460 if (args.hasArg(clang::driver::options::OPT_fopenacc)) { 461 res.frontendOpts().features_.Enable( 462 Fortran::common::LanguageFeature::OpenACC); 463 } 464 if (args.hasArg(clang::driver::options::OPT_fopenmp)) { 465 res.frontendOpts().features_.Enable( 466 Fortran::common::LanguageFeature::OpenMP); 467 } 468 469 // -pedantic 470 if (args.hasArg(clang::driver::options::OPT_pedantic)) { 471 res.set_EnableConformanceChecks(); 472 } 473 // -std=f2018 (currently this implies -pedantic) 474 // TODO: Set proper options when more fortran standards 475 // are supported. 476 if (args.hasArg(clang::driver::options::OPT_std_EQ)) { 477 auto standard = args.getLastArgValue(clang::driver::options::OPT_std_EQ); 478 // We only allow f2018 as the given standard 479 if (standard.equals("f2018")) { 480 res.set_EnableConformanceChecks(); 481 } else { 482 const unsigned diagID = 483 diags.getCustomDiagID(clang::DiagnosticsEngine::Error, 484 "Only -std=f2018 is allowed currently."); 485 diags.Report(diagID); 486 } 487 } 488 return diags.getNumErrors() == numErrorsBefore; 489 } 490 491 bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res, 492 llvm::ArrayRef<const char *> commandLineArgs, 493 clang::DiagnosticsEngine &diags) { 494 495 bool success = true; 496 497 // Parse the arguments 498 const llvm::opt::OptTable &opts = clang::driver::getDriverOptTable(); 499 const unsigned includedFlagsBitmask = clang::driver::options::FC1Option; 500 unsigned missingArgIndex, missingArgCount; 501 llvm::opt::InputArgList args = opts.ParseArgs( 502 commandLineArgs, missingArgIndex, missingArgCount, includedFlagsBitmask); 503 504 // Check for missing argument error. 505 if (missingArgCount) { 506 diags.Report(clang::diag::err_drv_missing_argument) 507 << args.getArgString(missingArgIndex) << missingArgCount; 508 success = false; 509 } 510 511 // Issue errors on unknown arguments 512 for (const auto *a : args.filtered(clang::driver::options::OPT_UNKNOWN)) { 513 auto argString = a->getAsString(args); 514 std::string nearest; 515 if (opts.findNearest(argString, nearest, includedFlagsBitmask) > 1) 516 diags.Report(clang::diag::err_drv_unknown_argument) << argString; 517 else 518 diags.Report(clang::diag::err_drv_unknown_argument_with_suggestion) 519 << argString << nearest; 520 success = false; 521 } 522 523 success &= ParseFrontendArgs(res.frontendOpts(), args, diags); 524 parsePreprocessorArgs(res.preprocessorOpts(), args); 525 success &= parseSemaArgs(res, args, diags); 526 success &= parseDialectArgs(res, args, diags); 527 success &= parseDiagArgs(res, args, diags); 528 529 return success; 530 } 531 532 void CompilerInvocation::collectMacroDefinitions() { 533 auto &ppOpts = this->preprocessorOpts(); 534 535 for (unsigned i = 0, n = ppOpts.macros.size(); i != n; ++i) { 536 llvm::StringRef macro = ppOpts.macros[i].first; 537 bool isUndef = ppOpts.macros[i].second; 538 539 std::pair<llvm::StringRef, llvm::StringRef> macroPair = macro.split('='); 540 llvm::StringRef macroName = macroPair.first; 541 llvm::StringRef macroBody = macroPair.second; 542 543 // For an #undef'd macro, we only care about the name. 544 if (isUndef) { 545 parserOpts_.predefinitions.emplace_back( 546 macroName.str(), std::optional<std::string>{}); 547 continue; 548 } 549 550 // For a #define'd macro, figure out the actual definition. 551 if (macroName.size() == macro.size()) 552 macroBody = "1"; 553 else { 554 // Note: GCC drops anything following an end-of-line character. 555 llvm::StringRef::size_type End = macroBody.find_first_of("\n\r"); 556 macroBody = macroBody.substr(0, End); 557 } 558 parserOpts_.predefinitions.emplace_back( 559 macroName, std::optional<std::string>(macroBody.str())); 560 } 561 } 562 563 void CompilerInvocation::SetDefaultFortranOpts() { 564 auto &fortranOptions = fortranOpts(); 565 566 // These defaults are based on the defaults in f18/f18.cpp. 567 std::vector<std::string> searchDirectories{"."s}; 568 fortranOptions.searchDirectories = searchDirectories; 569 fortranOptions.isFixedForm = false; 570 } 571 572 // TODO: When expanding this method, consider creating a dedicated API for 573 // this. Also at some point we will need to differentiate between different 574 // targets and add dedicated predefines for each. 575 void CompilerInvocation::setDefaultPredefinitions() { 576 auto &fortranOptions = fortranOpts(); 577 const auto &frontendOptions = frontendOpts(); 578 579 // Populate the macro list with version numbers and other predefinitions. 580 fortranOptions.predefinitions.emplace_back("__flang__", "1"); 581 fortranOptions.predefinitions.emplace_back( 582 "__flang_major__", FLANG_VERSION_MAJOR_STRING); 583 fortranOptions.predefinitions.emplace_back( 584 "__flang_minor__", FLANG_VERSION_MINOR_STRING); 585 fortranOptions.predefinitions.emplace_back( 586 "__flang_patchlevel__", FLANG_VERSION_PATCHLEVEL_STRING); 587 588 // Add predefinitions based on extensions enabled 589 if (frontendOptions.features_.IsEnabled( 590 Fortran::common::LanguageFeature::OpenACC)) { 591 fortranOptions.predefinitions.emplace_back("_OPENACC", "202011"); 592 } 593 if (frontendOptions.features_.IsEnabled( 594 Fortran::common::LanguageFeature::OpenMP)) { 595 fortranOptions.predefinitions.emplace_back("_OPENMP", "201511"); 596 } 597 } 598 599 void CompilerInvocation::setFortranOpts() { 600 auto &fortranOptions = fortranOpts(); 601 const auto &frontendOptions = frontendOpts(); 602 const auto &preprocessorOptions = preprocessorOpts(); 603 auto &moduleDirJ = moduleDir(); 604 605 if (frontendOptions.fortranForm_ != FortranForm::Unknown) { 606 fortranOptions.isFixedForm = 607 frontendOptions.fortranForm_ == FortranForm::FixedForm; 608 } 609 fortranOptions.fixedFormColumns = frontendOptions.fixedFormColumns_; 610 611 fortranOptions.features = frontendOptions.features_; 612 fortranOptions.encoding = frontendOptions.encoding_; 613 614 // Adding search directories specified by -I 615 fortranOptions.searchDirectories.insert( 616 fortranOptions.searchDirectories.end(), 617 preprocessorOptions.searchDirectoriesFromDashI.begin(), 618 preprocessorOptions.searchDirectoriesFromDashI.end()); 619 620 // Add the ordered list of -intrinsic-modules-path 621 fortranOptions.searchDirectories.insert( 622 fortranOptions.searchDirectories.end(), 623 preprocessorOptions.searchDirectoriesFromIntrModPath.begin(), 624 preprocessorOptions.searchDirectoriesFromIntrModPath.end()); 625 626 // Add the default intrinsic module directory at the end 627 fortranOptions.searchDirectories.emplace_back(getIntrinsicDir()); 628 629 // Add the directory supplied through -J/-module-dir to the list of search 630 // directories 631 if (moduleDirJ.compare(".") != 0) 632 fortranOptions.searchDirectories.emplace_back(moduleDirJ); 633 634 if (frontendOptions.instrumentedParse_) 635 fortranOptions.instrumentedParse = true; 636 637 if (frontendOptions.needProvenanceRangeToCharBlockMappings_) 638 fortranOptions.needProvenanceRangeToCharBlockMappings = true; 639 640 if (enableConformanceChecks()) { 641 fortranOptions.features.WarnOnAllNonstandard(); 642 } 643 } 644 645 void CompilerInvocation::setSemanticsOpts( 646 Fortran::parser::AllCookedSources &allCookedSources) { 647 const auto &fortranOptions = fortranOpts(); 648 649 semanticsContext_ = std::make_unique<semantics::SemanticsContext>( 650 defaultKinds(), fortranOptions.features, allCookedSources); 651 652 semanticsContext_->set_moduleDirectory(moduleDir()) 653 .set_searchDirectories(fortranOptions.searchDirectories) 654 .set_warnOnNonstandardUsage(enableConformanceChecks()) 655 .set_warningsAreErrors(warnAsErr()) 656 .set_moduleFileSuffix(moduleFileSuffix()); 657 } 658