1257b2971SCaroline Concatto //===- CompilerInvocation.cpp ---------------------------------------------===// 2257b2971SCaroline Concatto // 3257b2971SCaroline Concatto // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4257b2971SCaroline Concatto // See https://llvm.org/LICENSE.txt for license information. 5257b2971SCaroline Concatto // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6257b2971SCaroline Concatto // 7257b2971SCaroline Concatto //===----------------------------------------------------------------------===// 8257b2971SCaroline Concatto 9257b2971SCaroline Concatto #include "flang/Frontend/CompilerInvocation.h" 106d48a1a5SFaris Rehman #include "flang/Common/Fortran-features.h" 117809fa20SFaris Rehman #include "flang/Frontend/PreprocessorOptions.h" 126d48a1a5SFaris Rehman #include "flang/Semantics/semantics.h" 13197d9a55SFaris Rehman #include "flang/Version.inc" 14257b2971SCaroline Concatto #include "clang/Basic/AllDiagnostics.h" 15257b2971SCaroline Concatto #include "clang/Basic/DiagnosticDriver.h" 16257b2971SCaroline Concatto #include "clang/Basic/DiagnosticOptions.h" 17257b2971SCaroline Concatto #include "clang/Driver/DriverDiagnostic.h" 18257b2971SCaroline Concatto #include "clang/Driver/Options.h" 19257b2971SCaroline Concatto #include "llvm/ADT/StringRef.h" 20257b2971SCaroline Concatto #include "llvm/ADT/StringSwitch.h" 21257b2971SCaroline Concatto #include "llvm/Option/Arg.h" 22257b2971SCaroline Concatto #include "llvm/Option/ArgList.h" 23257b2971SCaroline Concatto #include "llvm/Option/OptTable.h" 248d51d37eSAndrzej Warzynski #include "llvm/Support/Process.h" 25257b2971SCaroline Concatto #include "llvm/Support/raw_ostream.h" 266d48a1a5SFaris Rehman #include <memory> 27257b2971SCaroline Concatto 28257b2971SCaroline Concatto using namespace Fortran::frontend; 29257b2971SCaroline Concatto 30257b2971SCaroline Concatto //===----------------------------------------------------------------------===// 31257b2971SCaroline Concatto // Initialization. 32257b2971SCaroline Concatto //===----------------------------------------------------------------------===// 33257b2971SCaroline Concatto CompilerInvocationBase::CompilerInvocationBase() 347809fa20SFaris Rehman : diagnosticOpts_(new clang::DiagnosticOptions()), 357809fa20SFaris Rehman preprocessorOpts_(new PreprocessorOptions()) {} 36257b2971SCaroline Concatto 37257b2971SCaroline Concatto CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &x) 387809fa20SFaris Rehman : diagnosticOpts_(new clang::DiagnosticOptions(x.GetDiagnosticOpts())), 397809fa20SFaris Rehman preprocessorOpts_(new PreprocessorOptions(x.preprocessorOpts())) {} 40257b2971SCaroline Concatto 41257b2971SCaroline Concatto CompilerInvocationBase::~CompilerInvocationBase() = default; 42257b2971SCaroline Concatto 43257b2971SCaroline Concatto //===----------------------------------------------------------------------===// 44257b2971SCaroline Concatto // Deserialization (from args) 45257b2971SCaroline Concatto //===----------------------------------------------------------------------===// 468d51d37eSAndrzej Warzynski static bool parseShowColorsArgs( 478d51d37eSAndrzej Warzynski const llvm::opt::ArgList &args, bool defaultColor) { 488d51d37eSAndrzej Warzynski // Color diagnostics default to auto ("on" if terminal supports) in the driver 498d51d37eSAndrzej Warzynski // but default to off in cc1, needing an explicit OPT_fdiagnostics_color. 508d51d37eSAndrzej Warzynski // Support both clang's -f[no-]color-diagnostics and gcc's 518d51d37eSAndrzej Warzynski // -f[no-]diagnostics-colors[=never|always|auto]. 528d51d37eSAndrzej Warzynski enum { 538d51d37eSAndrzej Warzynski Colors_On, 548d51d37eSAndrzej Warzynski Colors_Off, 558d51d37eSAndrzej Warzynski Colors_Auto 568d51d37eSAndrzej Warzynski } ShowColors = defaultColor ? Colors_Auto : Colors_Off; 578d51d37eSAndrzej Warzynski 588d51d37eSAndrzej Warzynski for (auto *a : args) { 598d51d37eSAndrzej Warzynski const llvm::opt::Option &O = a->getOption(); 608d51d37eSAndrzej Warzynski if (O.matches(clang::driver::options::OPT_fcolor_diagnostics) || 618d51d37eSAndrzej Warzynski O.matches(clang::driver::options::OPT_fdiagnostics_color)) { 628d51d37eSAndrzej Warzynski ShowColors = Colors_On; 638d51d37eSAndrzej Warzynski } else if (O.matches(clang::driver::options::OPT_fno_color_diagnostics) || 648d51d37eSAndrzej Warzynski O.matches(clang::driver::options::OPT_fno_diagnostics_color)) { 658d51d37eSAndrzej Warzynski ShowColors = Colors_Off; 668d51d37eSAndrzej Warzynski } else if (O.matches(clang::driver::options::OPT_fdiagnostics_color_EQ)) { 678d51d37eSAndrzej Warzynski llvm::StringRef value(a->getValue()); 688d51d37eSAndrzej Warzynski if (value == "always") 698d51d37eSAndrzej Warzynski ShowColors = Colors_On; 708d51d37eSAndrzej Warzynski else if (value == "never") 718d51d37eSAndrzej Warzynski ShowColors = Colors_Off; 728d51d37eSAndrzej Warzynski else if (value == "auto") 738d51d37eSAndrzej Warzynski ShowColors = Colors_Auto; 748d51d37eSAndrzej Warzynski } 758d51d37eSAndrzej Warzynski } 768d51d37eSAndrzej Warzynski 778d51d37eSAndrzej Warzynski return ShowColors == Colors_On || 788d51d37eSAndrzej Warzynski (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()); 798d51d37eSAndrzej Warzynski } 808d51d37eSAndrzej Warzynski 818d51d37eSAndrzej Warzynski bool Fortran::frontend::ParseDiagnosticArgs(clang::DiagnosticOptions &opts, 828d51d37eSAndrzej Warzynski llvm::opt::ArgList &args, bool defaultDiagColor) { 838d51d37eSAndrzej Warzynski opts.ShowColors = parseShowColorsArgs(args, defaultDiagColor); 848d51d37eSAndrzej Warzynski 858d51d37eSAndrzej Warzynski return true; 868d51d37eSAndrzej Warzynski } 878d51d37eSAndrzej Warzynski 88523d7bc6SAndrzej Warzynski // Tweak the frontend configuration based on the frontend action 89523d7bc6SAndrzej Warzynski static void setUpFrontendBasedOnAction(FrontendOptions &opts) { 90523d7bc6SAndrzej Warzynski assert(opts.programAction_ != Fortran::frontend::InvalidAction && 91523d7bc6SAndrzej Warzynski "Fortran frontend action not set!"); 92523d7bc6SAndrzej Warzynski 93523d7bc6SAndrzej Warzynski if (opts.programAction_ == DebugDumpParsingLog) 94523d7bc6SAndrzej Warzynski opts.instrumentedParse_ = true; 95523d7bc6SAndrzej Warzynski } 96523d7bc6SAndrzej Warzynski 97257b2971SCaroline Concatto static InputKind ParseFrontendArgs(FrontendOptions &opts, 98257b2971SCaroline Concatto llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) { 99760e6c4cSAndrzej Warzynski 100760e6c4cSAndrzej Warzynski // By default the frontend driver creates a ParseSyntaxOnly action. 101760e6c4cSAndrzej Warzynski opts.programAction_ = ParseSyntaxOnly; 102760e6c4cSAndrzej Warzynski 103257b2971SCaroline Concatto // Identify the action (i.e. opts.ProgramAction) 104257b2971SCaroline Concatto if (const llvm::opt::Arg *a = 105257b2971SCaroline Concatto args.getLastArg(clang::driver::options::OPT_Action_Group)) { 106257b2971SCaroline Concatto switch (a->getOption().getID()) { 107257b2971SCaroline Concatto default: { 108257b2971SCaroline Concatto llvm_unreachable("Invalid option in group!"); 109257b2971SCaroline Concatto } 1104c5906cfSCaroline Concatto case clang::driver::options::OPT_test_io: 1114c5906cfSCaroline Concatto opts.programAction_ = InputOutputTest; 1124c5906cfSCaroline Concatto break; 113d28de0d7SCaroline Concatto case clang::driver::options::OPT_E: 114d28de0d7SCaroline Concatto opts.programAction_ = PrintPreprocessedInput; 115d28de0d7SCaroline Concatto break; 1167d246cb1SAndrzej Warzynski case clang::driver::options::OPT_fsyntax_only: 1177d246cb1SAndrzej Warzynski opts.programAction_ = ParseSyntaxOnly; 1187d246cb1SAndrzej Warzynski break; 119e5cdb6c5SAndrzej Warzynski case clang::driver::options::OPT_emit_obj: 120e5cdb6c5SAndrzej Warzynski opts.programAction_ = EmitObj; 121e5cdb6c5SAndrzej Warzynski break; 12296d229c9SAndrzej Warzynski case clang::driver::options::OPT_fdebug_unparse: 12396d229c9SAndrzej Warzynski opts.programAction_ = DebugUnparse; 12496d229c9SAndrzej Warzynski break; 12596d229c9SAndrzej Warzynski case clang::driver::options::OPT_fdebug_unparse_with_symbols: 12696d229c9SAndrzej Warzynski opts.programAction_ = DebugUnparseWithSymbols; 12796d229c9SAndrzej Warzynski break; 1284bd08dabSFaris Rehman case clang::driver::options::OPT_fdebug_dump_symbols: 1294bd08dabSFaris Rehman opts.programAction_ = DebugDumpSymbols; 1304bd08dabSFaris Rehman break; 1314bd08dabSFaris Rehman case clang::driver::options::OPT_fdebug_dump_parse_tree: 1324bd08dabSFaris Rehman opts.programAction_ = DebugDumpParseTree; 1334bd08dabSFaris Rehman break; 1344bd08dabSFaris Rehman case clang::driver::options::OPT_fdebug_dump_provenance: 1354bd08dabSFaris Rehman opts.programAction_ = DebugDumpProvenance; 1364bd08dabSFaris Rehman break; 137523d7bc6SAndrzej Warzynski case clang::driver::options::OPT_fdebug_dump_parsing_log: 138523d7bc6SAndrzej Warzynski opts.programAction_ = DebugDumpParsingLog; 139523d7bc6SAndrzej Warzynski break; 140529f7181SFaris Rehman case clang::driver::options::OPT_fdebug_measure_parse_tree: 141529f7181SFaris Rehman opts.programAction_ = DebugMeasureParseTree; 142529f7181SFaris Rehman break; 143529f7181SFaris Rehman case clang::driver::options::OPT_fdebug_pre_fir_tree: 144529f7181SFaris Rehman opts.programAction_ = DebugPreFIRTree; 145529f7181SFaris Rehman break; 146d28de0d7SCaroline Concatto 147257b2971SCaroline Concatto // TODO: 148257b2971SCaroline Concatto // case calng::driver::options::OPT_emit_llvm: 149257b2971SCaroline Concatto // case clang::driver::options::OPT_emit_llvm_only: 150257b2971SCaroline Concatto // case clang::driver::options::OPT_emit_codegen_only: 151257b2971SCaroline Concatto // case clang::driver::options::OPT_emit_module: 152257b2971SCaroline Concatto // (...) 153257b2971SCaroline Concatto } 154257b2971SCaroline Concatto } 155257b2971SCaroline Concatto 1564c5906cfSCaroline Concatto opts.outputFile_ = args.getLastArgValue(clang::driver::options::OPT_o); 157257b2971SCaroline Concatto opts.showHelp_ = args.hasArg(clang::driver::options::OPT_help); 158257b2971SCaroline Concatto opts.showVersion_ = args.hasArg(clang::driver::options::OPT_version); 159257b2971SCaroline Concatto 160257b2971SCaroline Concatto // Get the input kind (from the value passed via `-x`) 161257b2971SCaroline Concatto InputKind dashX(Language::Unknown); 162257b2971SCaroline Concatto if (const llvm::opt::Arg *a = 163257b2971SCaroline Concatto args.getLastArg(clang::driver::options::OPT_x)) { 164257b2971SCaroline Concatto llvm::StringRef XValue = a->getValue(); 165257b2971SCaroline Concatto // Principal languages. 166257b2971SCaroline Concatto dashX = llvm::StringSwitch<InputKind>(XValue) 167257b2971SCaroline Concatto .Case("f90", Language::Fortran) 168257b2971SCaroline Concatto .Default(Language::Unknown); 169257b2971SCaroline Concatto 170257b2971SCaroline Concatto // Some special cases cannot be combined with suffixes. 171257b2971SCaroline Concatto if (dashX.IsUnknown()) 172257b2971SCaroline Concatto dashX = llvm::StringSwitch<InputKind>(XValue) 173257b2971SCaroline Concatto .Case("ir", Language::LLVM_IR) 174257b2971SCaroline Concatto .Default(Language::Unknown); 175257b2971SCaroline Concatto 176257b2971SCaroline Concatto if (dashX.IsUnknown()) 177257b2971SCaroline Concatto diags.Report(clang::diag::err_drv_invalid_value) 178257b2971SCaroline Concatto << a->getAsString(args) << a->getValue(); 179257b2971SCaroline Concatto } 180257b2971SCaroline Concatto 1814c5906cfSCaroline Concatto // Collect the input files and save them in our instance of FrontendOptions. 1824c5906cfSCaroline Concatto std::vector<std::string> inputs = 1834c5906cfSCaroline Concatto args.getAllArgValues(clang::driver::options::OPT_INPUT); 1844c5906cfSCaroline Concatto opts.inputs_.clear(); 1854c5906cfSCaroline Concatto if (inputs.empty()) 1864c5906cfSCaroline Concatto // '-' is the default input if none is given. 1874c5906cfSCaroline Concatto inputs.push_back("-"); 1884c5906cfSCaroline Concatto for (unsigned i = 0, e = inputs.size(); i != e; ++i) { 1894c5906cfSCaroline Concatto InputKind ik = dashX; 1904c5906cfSCaroline Concatto if (ik.IsUnknown()) { 1914c5906cfSCaroline Concatto ik = FrontendOptions::GetInputKindForExtension( 1924c5906cfSCaroline Concatto llvm::StringRef(inputs[i]).rsplit('.').second); 1934c5906cfSCaroline Concatto if (ik.IsUnknown()) 1944c5906cfSCaroline Concatto ik = Language::Unknown; 1954c5906cfSCaroline Concatto if (i == 0) 1964c5906cfSCaroline Concatto dashX = ik; 1974c5906cfSCaroline Concatto } 1984c5906cfSCaroline Concatto 1994c5906cfSCaroline Concatto opts.inputs_.emplace_back(std::move(inputs[i]), ik); 2004c5906cfSCaroline Concatto } 2013a1513c1SFaris Rehman 2023a1513c1SFaris Rehman // Set fortranForm_ based on options -ffree-form and -ffixed-form. 2033a1513c1SFaris Rehman if (const auto *arg = args.getLastArg(clang::driver::options::OPT_ffixed_form, 2043a1513c1SFaris Rehman clang::driver::options::OPT_ffree_form)) { 2053a1513c1SFaris Rehman opts.fortranForm_ = 2063a1513c1SFaris Rehman arg->getOption().matches(clang::driver::options::OPT_ffixed_form) 2073a1513c1SFaris Rehman ? FortranForm::FixedForm 2083a1513c1SFaris Rehman : FortranForm::FreeForm; 2093a1513c1SFaris Rehman } 2103a1513c1SFaris Rehman 2113a1513c1SFaris Rehman // Set fixedFormColumns_ based on -ffixed-line-length=<value> 2123a1513c1SFaris Rehman if (const auto *arg = 2133a1513c1SFaris Rehman args.getLastArg(clang::driver::options::OPT_ffixed_line_length_EQ)) { 2143a1513c1SFaris Rehman llvm::StringRef argValue = llvm::StringRef(arg->getValue()); 2153a1513c1SFaris Rehman std::int64_t columns = -1; 2163a1513c1SFaris Rehman if (argValue == "none") { 2173a1513c1SFaris Rehman columns = 0; 2183a1513c1SFaris Rehman } else if (argValue.getAsInteger(/*Radix=*/10, columns)) { 2193a1513c1SFaris Rehman columns = -1; 2203a1513c1SFaris Rehman } 2213a1513c1SFaris Rehman if (columns < 0) { 2223a1513c1SFaris Rehman diags.Report(clang::diag::err_drv_invalid_value_with_suggestion) 2233a1513c1SFaris Rehman << arg->getOption().getName() << arg->getValue() 2243a1513c1SFaris Rehman << "value must be 'none' or a non-negative integer"; 2253a1513c1SFaris Rehman } else if (columns == 0) { 2263a1513c1SFaris Rehman opts.fixedFormColumns_ = 1000000; 2273a1513c1SFaris Rehman } else if (columns < 7) { 2283a1513c1SFaris Rehman diags.Report(clang::diag::err_drv_invalid_value_with_suggestion) 2293a1513c1SFaris Rehman << arg->getOption().getName() << arg->getValue() 2303a1513c1SFaris Rehman << "value must be at least seven"; 2313a1513c1SFaris Rehman } else { 2323a1513c1SFaris Rehman opts.fixedFormColumns_ = columns; 2333a1513c1SFaris Rehman } 2343a1513c1SFaris Rehman } 2356d48a1a5SFaris Rehman 23610826ea7SFaris Rehman if (const llvm::opt::Arg *arg = 23710826ea7SFaris Rehman args.getLastArg(clang::driver::options::OPT_fimplicit_none, 23810826ea7SFaris Rehman clang::driver::options::OPT_fno_implicit_none)) { 23910826ea7SFaris Rehman opts.features_.Enable( 24010826ea7SFaris Rehman Fortran::common::LanguageFeature::ImplicitNoneTypeAlways, 24110826ea7SFaris Rehman arg->getOption().matches(clang::driver::options::OPT_fimplicit_none)); 24210826ea7SFaris Rehman } 24310826ea7SFaris Rehman if (const llvm::opt::Arg *arg = 24410826ea7SFaris Rehman args.getLastArg(clang::driver::options::OPT_fbackslash, 24510826ea7SFaris Rehman clang::driver::options::OPT_fno_backslash)) { 24610826ea7SFaris Rehman opts.features_.Enable(Fortran::common::LanguageFeature::BackslashEscapes, 24710826ea7SFaris Rehman arg->getOption().matches(clang::driver::options::OPT_fbackslash)); 24810826ea7SFaris Rehman } 24910826ea7SFaris Rehman if (const llvm::opt::Arg *arg = 25010826ea7SFaris Rehman args.getLastArg(clang::driver::options::OPT_flogical_abbreviations, 25110826ea7SFaris Rehman clang::driver::options::OPT_fno_logical_abbreviations)) { 25210826ea7SFaris Rehman opts.features_.Enable( 25310826ea7SFaris Rehman Fortran::common::LanguageFeature::LogicalAbbreviations, 25410826ea7SFaris Rehman arg->getOption().matches( 25510826ea7SFaris Rehman clang::driver::options::OPT_flogical_abbreviations)); 25610826ea7SFaris Rehman } 25710826ea7SFaris Rehman if (const llvm::opt::Arg *arg = 25810826ea7SFaris Rehman args.getLastArg(clang::driver::options::OPT_fxor_operator, 25910826ea7SFaris Rehman clang::driver::options::OPT_fno_xor_operator)) { 26010826ea7SFaris Rehman opts.features_.Enable(Fortran::common::LanguageFeature::XOROperator, 26110826ea7SFaris Rehman arg->getOption().matches(clang::driver::options::OPT_fxor_operator)); 26210826ea7SFaris Rehman } 26310826ea7SFaris Rehman if (args.hasArg( 26410826ea7SFaris Rehman clang::driver::options::OPT_falternative_parameter_statement)) { 26510826ea7SFaris Rehman opts.features_.Enable(Fortran::common::LanguageFeature::OldStyleParameter); 26610826ea7SFaris Rehman } 26710826ea7SFaris Rehman if (const llvm::opt::Arg *arg = 26810826ea7SFaris Rehman args.getLastArg(clang::driver::options::OPT_finput_charset_EQ)) { 26910826ea7SFaris Rehman llvm::StringRef argValue = arg->getValue(); 27010826ea7SFaris Rehman if (argValue == "utf-8") { 27110826ea7SFaris Rehman opts.encoding_ = Fortran::parser::Encoding::UTF_8; 27210826ea7SFaris Rehman } else if (argValue == "latin-1") { 27310826ea7SFaris Rehman opts.encoding_ = Fortran::parser::Encoding::LATIN_1; 27410826ea7SFaris Rehman } else { 27510826ea7SFaris Rehman diags.Report(clang::diag::err_drv_invalid_value) 27610826ea7SFaris Rehman << arg->getAsString(args) << argValue; 27710826ea7SFaris Rehman } 27810826ea7SFaris Rehman } 279523d7bc6SAndrzej Warzynski 280523d7bc6SAndrzej Warzynski setUpFrontendBasedOnAction(opts); 281523d7bc6SAndrzej Warzynski 282257b2971SCaroline Concatto return dashX; 283257b2971SCaroline Concatto } 284257b2971SCaroline Concatto 2857809fa20SFaris Rehman /// Parses all preprocessor input arguments and populates the preprocessor 2867809fa20SFaris Rehman /// options accordingly. 2877809fa20SFaris Rehman /// 2887809fa20SFaris Rehman /// \param [in] opts The preprocessor options instance 2897809fa20SFaris Rehman /// \param [out] args The list of input arguments 2907809fa20SFaris Rehman static void parsePreprocessorArgs( 2917809fa20SFaris Rehman Fortran::frontend::PreprocessorOptions &opts, llvm::opt::ArgList &args) { 2927809fa20SFaris Rehman // Add macros from the command line. 2937809fa20SFaris Rehman for (const auto *currentArg : args.filtered( 2947809fa20SFaris Rehman clang::driver::options::OPT_D, clang::driver::options::OPT_U)) { 2957809fa20SFaris Rehman if (currentArg->getOption().matches(clang::driver::options::OPT_D)) { 2967809fa20SFaris Rehman opts.addMacroDef(currentArg->getValue()); 2977809fa20SFaris Rehman } else { 2987809fa20SFaris Rehman opts.addMacroUndef(currentArg->getValue()); 2997809fa20SFaris Rehman } 3007809fa20SFaris Rehman } 30187dfd5e0SFaris Rehman 30287dfd5e0SFaris Rehman // Add the ordered list of -I's. 30387dfd5e0SFaris Rehman for (const auto *currentArg : args.filtered(clang::driver::options::OPT_I)) 30487dfd5e0SFaris Rehman opts.searchDirectoriesFromDashI.emplace_back(currentArg->getValue()); 3057809fa20SFaris Rehman } 3067809fa20SFaris Rehman 307985a42fdSArnamoy Bhattacharyya /// Parses all semantic related arguments and populates the variables 308985a42fdSArnamoy Bhattacharyya /// options accordingly. 309*1fd4beecSArnamoy Bhattacharyya static void parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args, 310985a42fdSArnamoy Bhattacharyya clang::DiagnosticsEngine &diags) { 311985a42fdSArnamoy Bhattacharyya 312*1fd4beecSArnamoy Bhattacharyya // -J/module-dir option 313985a42fdSArnamoy Bhattacharyya auto moduleDirList = 314985a42fdSArnamoy Bhattacharyya args.getAllArgValues(clang::driver::options::OPT_module_dir); 315985a42fdSArnamoy Bhattacharyya // User can only specify -J/-module-dir once 316985a42fdSArnamoy Bhattacharyya // https://gcc.gnu.org/onlinedocs/gfortran/Directory-Options.html 317985a42fdSArnamoy Bhattacharyya if (moduleDirList.size() > 1) { 318985a42fdSArnamoy Bhattacharyya const unsigned diagID = 319985a42fdSArnamoy Bhattacharyya diags.getCustomDiagID(clang::DiagnosticsEngine::Error, 320985a42fdSArnamoy Bhattacharyya "Only one '-module-dir/-J' option allowed"); 321985a42fdSArnamoy Bhattacharyya diags.Report(diagID); 322985a42fdSArnamoy Bhattacharyya } 323985a42fdSArnamoy Bhattacharyya if (moduleDirList.size() == 1) 324*1fd4beecSArnamoy Bhattacharyya res.SetModuleDir(moduleDirList[0]); 325*1fd4beecSArnamoy Bhattacharyya 326*1fd4beecSArnamoy Bhattacharyya // -fdebug-module-writer option 327*1fd4beecSArnamoy Bhattacharyya if (args.hasArg(clang::driver::options::OPT_fdebug_module_writer)) { 328*1fd4beecSArnamoy Bhattacharyya res.SetDebugModuleDir(true); 329*1fd4beecSArnamoy Bhattacharyya } 330985a42fdSArnamoy Bhattacharyya } 331985a42fdSArnamoy Bhattacharyya 332ab971c29SArnamoy Bhattacharyya /// Parses all Dialect related arguments and populates the variables 333ab971c29SArnamoy Bhattacharyya /// options accordingly. 334ab971c29SArnamoy Bhattacharyya static void parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args, 335ab971c29SArnamoy Bhattacharyya clang::DiagnosticsEngine &diags) { 336ab971c29SArnamoy Bhattacharyya 337ab971c29SArnamoy Bhattacharyya // -fdefault* family 338ab971c29SArnamoy Bhattacharyya if (args.hasArg(clang::driver::options::OPT_fdefault_real_8)) { 339ab971c29SArnamoy Bhattacharyya res.defaultKinds().set_defaultRealKind(8); 340ab971c29SArnamoy Bhattacharyya res.defaultKinds().set_doublePrecisionKind(16); 341ab971c29SArnamoy Bhattacharyya } 342ab971c29SArnamoy Bhattacharyya if (args.hasArg(clang::driver::options::OPT_fdefault_integer_8)) { 343ab971c29SArnamoy Bhattacharyya res.defaultKinds().set_defaultIntegerKind(8); 344ab971c29SArnamoy Bhattacharyya res.defaultKinds().set_subscriptIntegerKind(8); 345ab971c29SArnamoy Bhattacharyya res.defaultKinds().set_sizeIntegerKind(8); 346ab971c29SArnamoy Bhattacharyya } 347ab971c29SArnamoy Bhattacharyya if (args.hasArg(clang::driver::options::OPT_fdefault_double_8)) { 348ab971c29SArnamoy Bhattacharyya if (!args.hasArg(clang::driver::options::OPT_fdefault_real_8)) { 349ab971c29SArnamoy Bhattacharyya // -fdefault-double-8 has to be used with -fdefault-real-8 350ab971c29SArnamoy Bhattacharyya // to be compatible with gfortran 351ab971c29SArnamoy Bhattacharyya const unsigned diagID = 352ab971c29SArnamoy Bhattacharyya diags.getCustomDiagID(clang::DiagnosticsEngine::Error, 353ab971c29SArnamoy Bhattacharyya "Use of `-fdefault-double-8` requires `-fdefault-real-8`"); 354ab971c29SArnamoy Bhattacharyya diags.Report(diagID); 355ab971c29SArnamoy Bhattacharyya } 356ab971c29SArnamoy Bhattacharyya // https://gcc.gnu.org/onlinedocs/gfortran/Fortran-Dialect-Options.html 357ab971c29SArnamoy Bhattacharyya res.defaultKinds().set_doublePrecisionKind(8); 358ab971c29SArnamoy Bhattacharyya } 359ab971c29SArnamoy Bhattacharyya if (args.hasArg(clang::driver::options::OPT_flarge_sizes)) 360ab971c29SArnamoy Bhattacharyya res.defaultKinds().set_sizeIntegerKind(8); 361ab971c29SArnamoy Bhattacharyya 362ab971c29SArnamoy Bhattacharyya // -fopenmp and -fopenacc 363ab971c29SArnamoy Bhattacharyya if (args.hasArg(clang::driver::options::OPT_fopenacc)) { 364ab971c29SArnamoy Bhattacharyya res.frontendOpts().features_.Enable( 365ab971c29SArnamoy Bhattacharyya Fortran::common::LanguageFeature::OpenACC); 366ab971c29SArnamoy Bhattacharyya } 367ab971c29SArnamoy Bhattacharyya if (args.hasArg(clang::driver::options::OPT_fopenmp)) { 368ab971c29SArnamoy Bhattacharyya res.frontendOpts().features_.Enable( 369ab971c29SArnamoy Bhattacharyya Fortran::common::LanguageFeature::OpenMP); 370ab971c29SArnamoy Bhattacharyya } 371ab971c29SArnamoy Bhattacharyya return; 372ab971c29SArnamoy Bhattacharyya } 373ab971c29SArnamoy Bhattacharyya 374257b2971SCaroline Concatto bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res, 375257b2971SCaroline Concatto llvm::ArrayRef<const char *> commandLineArgs, 376257b2971SCaroline Concatto clang::DiagnosticsEngine &diags) { 377257b2971SCaroline Concatto 378257b2971SCaroline Concatto bool success = true; 379257b2971SCaroline Concatto 380257b2971SCaroline Concatto // Parse the arguments 381257b2971SCaroline Concatto const llvm::opt::OptTable &opts = clang::driver::getDriverOptTable(); 3828e3adda8SFaris Rehman const unsigned includedFlagsBitmask = clang::driver::options::FC1Option; 383257b2971SCaroline Concatto unsigned missingArgIndex, missingArgCount; 384257b2971SCaroline Concatto llvm::opt::InputArgList args = opts.ParseArgs( 385257b2971SCaroline Concatto commandLineArgs, missingArgIndex, missingArgCount, includedFlagsBitmask); 386257b2971SCaroline Concatto 387257b2971SCaroline Concatto // Issue errors on unknown arguments 388257b2971SCaroline Concatto for (const auto *a : args.filtered(clang::driver::options::OPT_UNKNOWN)) { 389257b2971SCaroline Concatto auto argString = a->getAsString(args); 390257b2971SCaroline Concatto std::string nearest; 391257b2971SCaroline Concatto if (opts.findNearest(argString, nearest, includedFlagsBitmask) > 1) 392257b2971SCaroline Concatto diags.Report(clang::diag::err_drv_unknown_argument) << argString; 393257b2971SCaroline Concatto else 394257b2971SCaroline Concatto diags.Report(clang::diag::err_drv_unknown_argument_with_suggestion) 395257b2971SCaroline Concatto << argString << nearest; 396257b2971SCaroline Concatto success = false; 397257b2971SCaroline Concatto } 398257b2971SCaroline Concatto 399257b2971SCaroline Concatto // Parse the frontend args 4009ffb5b04SAndrzej Warzynski ParseFrontendArgs(res.frontendOpts(), args, diags); 4017809fa20SFaris Rehman // Parse the preprocessor args 4027809fa20SFaris Rehman parsePreprocessorArgs(res.preprocessorOpts(), args); 403985a42fdSArnamoy Bhattacharyya // Parse semantic args 404*1fd4beecSArnamoy Bhattacharyya parseSemaArgs(res, args, diags); 405ab971c29SArnamoy Bhattacharyya // Parse dialect arguments 406ab971c29SArnamoy Bhattacharyya parseDialectArgs(res, args, diags); 407257b2971SCaroline Concatto 408257b2971SCaroline Concatto return success; 409257b2971SCaroline Concatto } 410d28de0d7SCaroline Concatto 4117809fa20SFaris Rehman /// Collect the macro definitions provided by the given preprocessor 4127809fa20SFaris Rehman /// options into the parser options. 4137809fa20SFaris Rehman /// 4147809fa20SFaris Rehman /// \param [in] ppOpts The preprocessor options 4157809fa20SFaris Rehman /// \param [out] opts The fortran options 4167809fa20SFaris Rehman static void collectMacroDefinitions( 4177809fa20SFaris Rehman const PreprocessorOptions &ppOpts, Fortran::parser::Options &opts) { 4187809fa20SFaris Rehman for (unsigned i = 0, n = ppOpts.macros.size(); i != n; ++i) { 4197809fa20SFaris Rehman llvm::StringRef macro = ppOpts.macros[i].first; 4207809fa20SFaris Rehman bool isUndef = ppOpts.macros[i].second; 4217809fa20SFaris Rehman 4227809fa20SFaris Rehman std::pair<llvm::StringRef, llvm::StringRef> macroPair = macro.split('='); 4237809fa20SFaris Rehman llvm::StringRef macroName = macroPair.first; 4247809fa20SFaris Rehman llvm::StringRef macroBody = macroPair.second; 4257809fa20SFaris Rehman 4267809fa20SFaris Rehman // For an #undef'd macro, we only care about the name. 4277809fa20SFaris Rehman if (isUndef) { 4287809fa20SFaris Rehman opts.predefinitions.emplace_back( 4297809fa20SFaris Rehman macroName.str(), std::optional<std::string>{}); 4307809fa20SFaris Rehman continue; 4317809fa20SFaris Rehman } 4327809fa20SFaris Rehman 4337809fa20SFaris Rehman // For a #define'd macro, figure out the actual definition. 4347809fa20SFaris Rehman if (macroName.size() == macro.size()) 4357809fa20SFaris Rehman macroBody = "1"; 4367809fa20SFaris Rehman else { 4377809fa20SFaris Rehman // Note: GCC drops anything following an end-of-line character. 4387809fa20SFaris Rehman llvm::StringRef::size_type End = macroBody.find_first_of("\n\r"); 4397809fa20SFaris Rehman macroBody = macroBody.substr(0, End); 4407809fa20SFaris Rehman } 4417809fa20SFaris Rehman opts.predefinitions.emplace_back( 4427809fa20SFaris Rehman macroName, std::optional<std::string>(macroBody.str())); 4437809fa20SFaris Rehman } 4447809fa20SFaris Rehman } 4457809fa20SFaris Rehman 446d28de0d7SCaroline Concatto void CompilerInvocation::SetDefaultFortranOpts() { 4476bbbe4a5SAndrzej Warzynski auto &fortranOptions = fortranOpts(); 448d28de0d7SCaroline Concatto 449d28de0d7SCaroline Concatto // These defaults are based on the defaults in f18/f18.cpp. 450d28de0d7SCaroline Concatto std::vector<std::string> searchDirectories{"."s}; 451d28de0d7SCaroline Concatto fortranOptions.searchDirectories = searchDirectories; 452d28de0d7SCaroline Concatto fortranOptions.isFixedForm = false; 4530feff71eSAndrzej Warzynski } 4540feff71eSAndrzej Warzynski 4550feff71eSAndrzej Warzynski // TODO: When expanding this method, consider creating a dedicated API for 4560feff71eSAndrzej Warzynski // this. Also at some point we will need to differentiate between different 4570feff71eSAndrzej Warzynski // targets and add dedicated predefines for each. 4580feff71eSAndrzej Warzynski void CompilerInvocation::setDefaultPredefinitions() { 4590feff71eSAndrzej Warzynski auto &fortranOptions = fortranOpts(); 4600feff71eSAndrzej Warzynski const auto &frontendOptions = frontendOpts(); 461197d9a55SFaris Rehman 462197d9a55SFaris Rehman // Populate the macro list with version numbers and other predefinitions. 463197d9a55SFaris Rehman fortranOptions.predefinitions.emplace_back("__flang__", "1"); 464197d9a55SFaris Rehman fortranOptions.predefinitions.emplace_back( 465197d9a55SFaris Rehman "__flang_major__", FLANG_VERSION_MAJOR_STRING); 466197d9a55SFaris Rehman fortranOptions.predefinitions.emplace_back( 467197d9a55SFaris Rehman "__flang_minor__", FLANG_VERSION_MINOR_STRING); 468197d9a55SFaris Rehman fortranOptions.predefinitions.emplace_back( 469197d9a55SFaris Rehman "__flang_patchlevel__", FLANG_VERSION_PATCHLEVEL_STRING); 4706d48a1a5SFaris Rehman 4716d48a1a5SFaris Rehman // Add predefinitions based on extensions enabled 4726d48a1a5SFaris Rehman if (frontendOptions.features_.IsEnabled( 4736d48a1a5SFaris Rehman Fortran::common::LanguageFeature::OpenACC)) { 4746d48a1a5SFaris Rehman fortranOptions.predefinitions.emplace_back("_OPENACC", "202011"); 4756d48a1a5SFaris Rehman } 4766d48a1a5SFaris Rehman if (frontendOptions.features_.IsEnabled( 4776d48a1a5SFaris Rehman Fortran::common::LanguageFeature::OpenMP)) { 4786d48a1a5SFaris Rehman fortranOptions.predefinitions.emplace_back("_OPENMP", "201511"); 4796d48a1a5SFaris Rehman } 4806d48a1a5SFaris Rehman } 4816d48a1a5SFaris Rehman 4827809fa20SFaris Rehman void CompilerInvocation::setFortranOpts() { 4837809fa20SFaris Rehman auto &fortranOptions = fortranOpts(); 4843a1513c1SFaris Rehman const auto &frontendOptions = frontendOpts(); 4857809fa20SFaris Rehman const auto &preprocessorOptions = preprocessorOpts(); 486985a42fdSArnamoy Bhattacharyya auto &moduleDirJ = moduleDir(); 4877809fa20SFaris Rehman 4883a1513c1SFaris Rehman if (frontendOptions.fortranForm_ != FortranForm::Unknown) { 4893a1513c1SFaris Rehman fortranOptions.isFixedForm = 4903a1513c1SFaris Rehman frontendOptions.fortranForm_ == FortranForm::FixedForm; 4913a1513c1SFaris Rehman } 4923a1513c1SFaris Rehman fortranOptions.fixedFormColumns = frontendOptions.fixedFormColumns_; 4933a1513c1SFaris Rehman 4946d48a1a5SFaris Rehman fortranOptions.features = frontendOptions.features_; 49510826ea7SFaris Rehman fortranOptions.encoding = frontendOptions.encoding_; 4966d48a1a5SFaris Rehman 4977809fa20SFaris Rehman collectMacroDefinitions(preprocessorOptions, fortranOptions); 49887dfd5e0SFaris Rehman 49987dfd5e0SFaris Rehman fortranOptions.searchDirectories.insert( 50087dfd5e0SFaris Rehman fortranOptions.searchDirectories.end(), 50187dfd5e0SFaris Rehman preprocessorOptions.searchDirectoriesFromDashI.begin(), 50287dfd5e0SFaris Rehman preprocessorOptions.searchDirectoriesFromDashI.end()); 503985a42fdSArnamoy Bhattacharyya 504985a42fdSArnamoy Bhattacharyya // Add the directory supplied through -J/-module-dir to the list of search 505985a42fdSArnamoy Bhattacharyya // directories 506985a42fdSArnamoy Bhattacharyya if (moduleDirJ.compare(".") != 0) 507985a42fdSArnamoy Bhattacharyya fortranOptions.searchDirectories.emplace_back(moduleDirJ); 508523d7bc6SAndrzej Warzynski 509523d7bc6SAndrzej Warzynski if (frontendOptions.instrumentedParse_) 510523d7bc6SAndrzej Warzynski fortranOptions.instrumentedParse = true; 511985a42fdSArnamoy Bhattacharyya } 512985a42fdSArnamoy Bhattacharyya 513985a42fdSArnamoy Bhattacharyya void CompilerInvocation::setSemanticsOpts( 5146d48a1a5SFaris Rehman Fortran::parser::AllCookedSources &allCookedSources) { 5156d48a1a5SFaris Rehman const auto &fortranOptions = fortranOpts(); 5166d48a1a5SFaris Rehman 5176d48a1a5SFaris Rehman semanticsContext_ = std::make_unique<semantics::SemanticsContext>( 518ab971c29SArnamoy Bhattacharyya defaultKinds(), fortranOptions.features, allCookedSources); 5196d48a1a5SFaris Rehman 520*1fd4beecSArnamoy Bhattacharyya semanticsContext_->set_moduleDirectory(moduleDir()) 521985a42fdSArnamoy Bhattacharyya .set_searchDirectories(fortranOptions.searchDirectories); 5227809fa20SFaris Rehman } 523