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 // Tweak the frontend configuration based on the frontend action
89 static void setUpFrontendBasedOnAction(FrontendOptions &opts) {
90   assert(opts.programAction_ != Fortran::frontend::InvalidAction &&
91       "Fortran frontend action not set!");
92 
93   if (opts.programAction_ == DebugDumpParsingLog)
94     opts.instrumentedParse_ = true;
95 }
96 
97 static InputKind ParseFrontendArgs(FrontendOptions &opts,
98     llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) {
99 
100   // By default the frontend driver creates a ParseSyntaxOnly action.
101   opts.programAction_ = ParseSyntaxOnly;
102 
103   // Identify the action (i.e. opts.ProgramAction)
104   if (const llvm::opt::Arg *a =
105           args.getLastArg(clang::driver::options::OPT_Action_Group)) {
106     switch (a->getOption().getID()) {
107     default: {
108       llvm_unreachable("Invalid option in group!");
109     }
110     case clang::driver::options::OPT_test_io:
111       opts.programAction_ = InputOutputTest;
112       break;
113     case clang::driver::options::OPT_E:
114       opts.programAction_ = PrintPreprocessedInput;
115       break;
116     case clang::driver::options::OPT_fsyntax_only:
117       opts.programAction_ = ParseSyntaxOnly;
118       break;
119     case clang::driver::options::OPT_emit_obj:
120       opts.programAction_ = EmitObj;
121       break;
122     case clang::driver::options::OPT_fdebug_unparse:
123       opts.programAction_ = DebugUnparse;
124       break;
125     case clang::driver::options::OPT_fdebug_unparse_with_symbols:
126       opts.programAction_ = DebugUnparseWithSymbols;
127       break;
128     case clang::driver::options::OPT_fdebug_dump_symbols:
129       opts.programAction_ = DebugDumpSymbols;
130       break;
131     case clang::driver::options::OPT_fdebug_dump_parse_tree:
132       opts.programAction_ = DebugDumpParseTree;
133       break;
134     case clang::driver::options::OPT_fdebug_dump_provenance:
135       opts.programAction_ = DebugDumpProvenance;
136       break;
137     case clang::driver::options::OPT_fdebug_dump_parsing_log:
138       opts.programAction_ = DebugDumpParsingLog;
139       break;
140     case clang::driver::options::OPT_fdebug_measure_parse_tree:
141       opts.programAction_ = DebugMeasureParseTree;
142       break;
143     case clang::driver::options::OPT_fdebug_pre_fir_tree:
144       opts.programAction_ = DebugPreFIRTree;
145       break;
146 
147       // TODO:
148       // case calng::driver::options::OPT_emit_llvm:
149       // case clang::driver::options::OPT_emit_llvm_only:
150       // case clang::driver::options::OPT_emit_codegen_only:
151       // case clang::driver::options::OPT_emit_module:
152       // (...)
153     }
154   }
155 
156   opts.outputFile_ = args.getLastArgValue(clang::driver::options::OPT_o);
157   opts.showHelp_ = args.hasArg(clang::driver::options::OPT_help);
158   opts.showVersion_ = args.hasArg(clang::driver::options::OPT_version);
159 
160   // Get the input kind (from the value passed via `-x`)
161   InputKind dashX(Language::Unknown);
162   if (const llvm::opt::Arg *a =
163           args.getLastArg(clang::driver::options::OPT_x)) {
164     llvm::StringRef XValue = a->getValue();
165     // Principal languages.
166     dashX = llvm::StringSwitch<InputKind>(XValue)
167                 .Case("f90", Language::Fortran)
168                 .Default(Language::Unknown);
169 
170     // Some special cases cannot be combined with suffixes.
171     if (dashX.IsUnknown())
172       dashX = llvm::StringSwitch<InputKind>(XValue)
173                   .Case("ir", Language::LLVM_IR)
174                   .Default(Language::Unknown);
175 
176     if (dashX.IsUnknown())
177       diags.Report(clang::diag::err_drv_invalid_value)
178           << a->getAsString(args) << a->getValue();
179   }
180 
181   // Collect the input files and save them in our instance of FrontendOptions.
182   std::vector<std::string> inputs =
183       args.getAllArgValues(clang::driver::options::OPT_INPUT);
184   opts.inputs_.clear();
185   if (inputs.empty())
186     // '-' is the default input if none is given.
187     inputs.push_back("-");
188   for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
189     InputKind ik = dashX;
190     if (ik.IsUnknown()) {
191       ik = FrontendOptions::GetInputKindForExtension(
192           llvm::StringRef(inputs[i]).rsplit('.').second);
193       if (ik.IsUnknown())
194         ik = Language::Unknown;
195       if (i == 0)
196         dashX = ik;
197     }
198 
199     opts.inputs_.emplace_back(std::move(inputs[i]), ik);
200   }
201 
202   // Set fortranForm_ based on options -ffree-form and -ffixed-form.
203   if (const auto *arg = args.getLastArg(clang::driver::options::OPT_ffixed_form,
204           clang::driver::options::OPT_ffree_form)) {
205     opts.fortranForm_ =
206         arg->getOption().matches(clang::driver::options::OPT_ffixed_form)
207         ? FortranForm::FixedForm
208         : FortranForm::FreeForm;
209   }
210 
211   // Set fixedFormColumns_ based on -ffixed-line-length=<value>
212   if (const auto *arg =
213           args.getLastArg(clang::driver::options::OPT_ffixed_line_length_EQ)) {
214     llvm::StringRef argValue = llvm::StringRef(arg->getValue());
215     std::int64_t columns = -1;
216     if (argValue == "none") {
217       columns = 0;
218     } else if (argValue.getAsInteger(/*Radix=*/10, columns)) {
219       columns = -1;
220     }
221     if (columns < 0) {
222       diags.Report(clang::diag::err_drv_invalid_value_with_suggestion)
223           << arg->getOption().getName() << arg->getValue()
224           << "value must be 'none' or a non-negative integer";
225     } else if (columns == 0) {
226       opts.fixedFormColumns_ = 1000000;
227     } else if (columns < 7) {
228       diags.Report(clang::diag::err_drv_invalid_value_with_suggestion)
229           << arg->getOption().getName() << arg->getValue()
230           << "value must be at least seven";
231     } else {
232       opts.fixedFormColumns_ = columns;
233     }
234   }
235 
236   if (const llvm::opt::Arg *arg =
237           args.getLastArg(clang::driver::options::OPT_fimplicit_none,
238               clang::driver::options::OPT_fno_implicit_none)) {
239     opts.features_.Enable(
240         Fortran::common::LanguageFeature::ImplicitNoneTypeAlways,
241         arg->getOption().matches(clang::driver::options::OPT_fimplicit_none));
242   }
243   if (const llvm::opt::Arg *arg =
244           args.getLastArg(clang::driver::options::OPT_fbackslash,
245               clang::driver::options::OPT_fno_backslash)) {
246     opts.features_.Enable(Fortran::common::LanguageFeature::BackslashEscapes,
247         arg->getOption().matches(clang::driver::options::OPT_fbackslash));
248   }
249   if (const llvm::opt::Arg *arg =
250           args.getLastArg(clang::driver::options::OPT_flogical_abbreviations,
251               clang::driver::options::OPT_fno_logical_abbreviations)) {
252     opts.features_.Enable(
253         Fortran::common::LanguageFeature::LogicalAbbreviations,
254         arg->getOption().matches(
255             clang::driver::options::OPT_flogical_abbreviations));
256   }
257   if (const llvm::opt::Arg *arg =
258           args.getLastArg(clang::driver::options::OPT_fxor_operator,
259               clang::driver::options::OPT_fno_xor_operator)) {
260     opts.features_.Enable(Fortran::common::LanguageFeature::XOROperator,
261         arg->getOption().matches(clang::driver::options::OPT_fxor_operator));
262   }
263   if (args.hasArg(
264           clang::driver::options::OPT_falternative_parameter_statement)) {
265     opts.features_.Enable(Fortran::common::LanguageFeature::OldStyleParameter);
266   }
267   if (const llvm::opt::Arg *arg =
268           args.getLastArg(clang::driver::options::OPT_finput_charset_EQ)) {
269     llvm::StringRef argValue = arg->getValue();
270     if (argValue == "utf-8") {
271       opts.encoding_ = Fortran::parser::Encoding::UTF_8;
272     } else if (argValue == "latin-1") {
273       opts.encoding_ = Fortran::parser::Encoding::LATIN_1;
274     } else {
275       diags.Report(clang::diag::err_drv_invalid_value)
276           << arg->getAsString(args) << argValue;
277     }
278   }
279 
280   setUpFrontendBasedOnAction(opts);
281 
282   return dashX;
283 }
284 
285 /// Parses all preprocessor input arguments and populates the preprocessor
286 /// options accordingly.
287 ///
288 /// \param [in] opts The preprocessor options instance
289 /// \param [out] args The list of input arguments
290 static void parsePreprocessorArgs(
291     Fortran::frontend::PreprocessorOptions &opts, llvm::opt::ArgList &args) {
292   // Add macros from the command line.
293   for (const auto *currentArg : args.filtered(
294            clang::driver::options::OPT_D, clang::driver::options::OPT_U)) {
295     if (currentArg->getOption().matches(clang::driver::options::OPT_D)) {
296       opts.addMacroDef(currentArg->getValue());
297     } else {
298       opts.addMacroUndef(currentArg->getValue());
299     }
300   }
301 
302   // Add the ordered list of -I's.
303   for (const auto *currentArg : args.filtered(clang::driver::options::OPT_I))
304     opts.searchDirectoriesFromDashI.emplace_back(currentArg->getValue());
305 }
306 
307 /// Parses all semantic related arguments and populates the variables
308 /// options accordingly.
309 static void parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
310     clang::DiagnosticsEngine &diags) {
311 
312   // -J/module-dir option
313   auto moduleDirList =
314       args.getAllArgValues(clang::driver::options::OPT_module_dir);
315   // User can only specify -J/-module-dir once
316   // https://gcc.gnu.org/onlinedocs/gfortran/Directory-Options.html
317   if (moduleDirList.size() > 1) {
318     const unsigned diagID =
319         diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
320             "Only one '-module-dir/-J' option allowed");
321     diags.Report(diagID);
322   }
323   if (moduleDirList.size() == 1)
324     res.SetModuleDir(moduleDirList[0]);
325 
326   // -fdebug-module-writer option
327   if (args.hasArg(clang::driver::options::OPT_fdebug_module_writer)) {
328     res.SetDebugModuleDir(true);
329   }
330 }
331 
332 /// Parses all Dialect related arguments and populates the variables
333 /// options accordingly.
334 static void parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
335     clang::DiagnosticsEngine &diags) {
336 
337   // -fdefault* family
338   if (args.hasArg(clang::driver::options::OPT_fdefault_real_8)) {
339     res.defaultKinds().set_defaultRealKind(8);
340     res.defaultKinds().set_doublePrecisionKind(16);
341   }
342   if (args.hasArg(clang::driver::options::OPT_fdefault_integer_8)) {
343     res.defaultKinds().set_defaultIntegerKind(8);
344     res.defaultKinds().set_subscriptIntegerKind(8);
345     res.defaultKinds().set_sizeIntegerKind(8);
346   }
347   if (args.hasArg(clang::driver::options::OPT_fdefault_double_8)) {
348     if (!args.hasArg(clang::driver::options::OPT_fdefault_real_8)) {
349       // -fdefault-double-8 has to be used with -fdefault-real-8
350       // to be compatible with gfortran
351       const unsigned diagID =
352           diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
353               "Use of `-fdefault-double-8` requires `-fdefault-real-8`");
354       diags.Report(diagID);
355     }
356     // https://gcc.gnu.org/onlinedocs/gfortran/Fortran-Dialect-Options.html
357     res.defaultKinds().set_doublePrecisionKind(8);
358   }
359   if (args.hasArg(clang::driver::options::OPT_flarge_sizes))
360     res.defaultKinds().set_sizeIntegerKind(8);
361 
362   // -fopenmp and -fopenacc
363   if (args.hasArg(clang::driver::options::OPT_fopenacc)) {
364     res.frontendOpts().features_.Enable(
365         Fortran::common::LanguageFeature::OpenACC);
366   }
367   if (args.hasArg(clang::driver::options::OPT_fopenmp)) {
368     res.frontendOpts().features_.Enable(
369         Fortran::common::LanguageFeature::OpenMP);
370   }
371   return;
372 }
373 
374 bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
375     llvm::ArrayRef<const char *> commandLineArgs,
376     clang::DiagnosticsEngine &diags) {
377 
378   bool success = true;
379 
380   // Parse the arguments
381   const llvm::opt::OptTable &opts = clang::driver::getDriverOptTable();
382   const unsigned includedFlagsBitmask = clang::driver::options::FC1Option;
383   unsigned missingArgIndex, missingArgCount;
384   llvm::opt::InputArgList args = opts.ParseArgs(
385       commandLineArgs, missingArgIndex, missingArgCount, includedFlagsBitmask);
386 
387   // Issue errors on unknown arguments
388   for (const auto *a : args.filtered(clang::driver::options::OPT_UNKNOWN)) {
389     auto argString = a->getAsString(args);
390     std::string nearest;
391     if (opts.findNearest(argString, nearest, includedFlagsBitmask) > 1)
392       diags.Report(clang::diag::err_drv_unknown_argument) << argString;
393     else
394       diags.Report(clang::diag::err_drv_unknown_argument_with_suggestion)
395           << argString << nearest;
396     success = false;
397   }
398 
399   // Parse the frontend args
400   ParseFrontendArgs(res.frontendOpts(), args, diags);
401   // Parse the preprocessor args
402   parsePreprocessorArgs(res.preprocessorOpts(), args);
403   // Parse semantic args
404   parseSemaArgs(res, args, diags);
405   // Parse dialect arguments
406   parseDialectArgs(res, args, diags);
407 
408   return success;
409 }
410 
411 /// Collect the macro definitions provided by the given preprocessor
412 /// options into the parser options.
413 ///
414 /// \param [in] ppOpts The preprocessor options
415 /// \param [out] opts The fortran options
416 static void collectMacroDefinitions(
417     const PreprocessorOptions &ppOpts, Fortran::parser::Options &opts) {
418   for (unsigned i = 0, n = ppOpts.macros.size(); i != n; ++i) {
419     llvm::StringRef macro = ppOpts.macros[i].first;
420     bool isUndef = ppOpts.macros[i].second;
421 
422     std::pair<llvm::StringRef, llvm::StringRef> macroPair = macro.split('=');
423     llvm::StringRef macroName = macroPair.first;
424     llvm::StringRef macroBody = macroPair.second;
425 
426     // For an #undef'd macro, we only care about the name.
427     if (isUndef) {
428       opts.predefinitions.emplace_back(
429           macroName.str(), std::optional<std::string>{});
430       continue;
431     }
432 
433     // For a #define'd macro, figure out the actual definition.
434     if (macroName.size() == macro.size())
435       macroBody = "1";
436     else {
437       // Note: GCC drops anything following an end-of-line character.
438       llvm::StringRef::size_type End = macroBody.find_first_of("\n\r");
439       macroBody = macroBody.substr(0, End);
440     }
441     opts.predefinitions.emplace_back(
442         macroName, std::optional<std::string>(macroBody.str()));
443   }
444 }
445 
446 void CompilerInvocation::SetDefaultFortranOpts() {
447   auto &fortranOptions = fortranOpts();
448 
449   // These defaults are based on the defaults in f18/f18.cpp.
450   std::vector<std::string> searchDirectories{"."s};
451   fortranOptions.searchDirectories = searchDirectories;
452   fortranOptions.isFixedForm = false;
453 }
454 
455 // TODO: When expanding this method, consider creating a dedicated API for
456 // this. Also at some point we will need to differentiate between different
457 // targets and add dedicated predefines for each.
458 void CompilerInvocation::setDefaultPredefinitions() {
459   auto &fortranOptions = fortranOpts();
460   const auto &frontendOptions = frontendOpts();
461 
462   // Populate the macro list with version numbers and other predefinitions.
463   fortranOptions.predefinitions.emplace_back("__flang__", "1");
464   fortranOptions.predefinitions.emplace_back(
465       "__flang_major__", FLANG_VERSION_MAJOR_STRING);
466   fortranOptions.predefinitions.emplace_back(
467       "__flang_minor__", FLANG_VERSION_MINOR_STRING);
468   fortranOptions.predefinitions.emplace_back(
469       "__flang_patchlevel__", FLANG_VERSION_PATCHLEVEL_STRING);
470 
471   // Add predefinitions based on extensions enabled
472   if (frontendOptions.features_.IsEnabled(
473           Fortran::common::LanguageFeature::OpenACC)) {
474     fortranOptions.predefinitions.emplace_back("_OPENACC", "202011");
475   }
476   if (frontendOptions.features_.IsEnabled(
477           Fortran::common::LanguageFeature::OpenMP)) {
478     fortranOptions.predefinitions.emplace_back("_OPENMP", "201511");
479   }
480 }
481 
482 void CompilerInvocation::setFortranOpts() {
483   auto &fortranOptions = fortranOpts();
484   const auto &frontendOptions = frontendOpts();
485   const auto &preprocessorOptions = preprocessorOpts();
486   auto &moduleDirJ = moduleDir();
487 
488   if (frontendOptions.fortranForm_ != FortranForm::Unknown) {
489     fortranOptions.isFixedForm =
490         frontendOptions.fortranForm_ == FortranForm::FixedForm;
491   }
492   fortranOptions.fixedFormColumns = frontendOptions.fixedFormColumns_;
493 
494   fortranOptions.features = frontendOptions.features_;
495   fortranOptions.encoding = frontendOptions.encoding_;
496 
497   collectMacroDefinitions(preprocessorOptions, fortranOptions);
498 
499   fortranOptions.searchDirectories.insert(
500       fortranOptions.searchDirectories.end(),
501       preprocessorOptions.searchDirectoriesFromDashI.begin(),
502       preprocessorOptions.searchDirectoriesFromDashI.end());
503 
504   // Add the directory supplied through -J/-module-dir to the list of search
505   // directories
506   if (moduleDirJ.compare(".") != 0)
507     fortranOptions.searchDirectories.emplace_back(moduleDirJ);
508 
509   if (frontendOptions.instrumentedParse_)
510     fortranOptions.instrumentedParse = true;
511 }
512 
513 void CompilerInvocation::setSemanticsOpts(
514     Fortran::parser::AllCookedSources &allCookedSources) {
515   const auto &fortranOptions = fortranOpts();
516 
517   semanticsContext_ = std::make_unique<semantics::SemanticsContext>(
518       defaultKinds(), fortranOptions.features, allCookedSources);
519 
520   semanticsContext_->set_moduleDirectory(moduleDir())
521       .set_searchDirectories(fortranOptions.searchDirectories);
522 }
523