xref: /llvm-project/flang/lib/Frontend/CompilerInvocation.cpp (revision 523d7bc6f427f9ae32e54dbf1764826cfb269d21)
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(std::string &moduleDir, llvm::opt::ArgList &args,
310     clang::DiagnosticsEngine &diags) {
311 
312   auto moduleDirList =
313       args.getAllArgValues(clang::driver::options::OPT_module_dir);
314   // User can only specify -J/-module-dir once
315   // https://gcc.gnu.org/onlinedocs/gfortran/Directory-Options.html
316   if (moduleDirList.size() > 1) {
317     const unsigned diagID =
318         diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
319             "Only one '-module-dir/-J' option allowed");
320     diags.Report(diagID);
321   }
322   if (moduleDirList.size() == 1)
323     moduleDir = moduleDirList[0];
324 }
325 
326 /// Parses all Dialect related arguments and populates the variables
327 /// options accordingly.
328 static void parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
329     clang::DiagnosticsEngine &diags) {
330 
331   // -fdefault* family
332   if (args.hasArg(clang::driver::options::OPT_fdefault_real_8)) {
333     res.defaultKinds().set_defaultRealKind(8);
334     res.defaultKinds().set_doublePrecisionKind(16);
335   }
336   if (args.hasArg(clang::driver::options::OPT_fdefault_integer_8)) {
337     res.defaultKinds().set_defaultIntegerKind(8);
338     res.defaultKinds().set_subscriptIntegerKind(8);
339     res.defaultKinds().set_sizeIntegerKind(8);
340   }
341   if (args.hasArg(clang::driver::options::OPT_fdefault_double_8)) {
342     if (!args.hasArg(clang::driver::options::OPT_fdefault_real_8)) {
343       // -fdefault-double-8 has to be used with -fdefault-real-8
344       // to be compatible with gfortran
345       const unsigned diagID =
346           diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
347               "Use of `-fdefault-double-8` requires `-fdefault-real-8`");
348       diags.Report(diagID);
349     }
350     // https://gcc.gnu.org/onlinedocs/gfortran/Fortran-Dialect-Options.html
351     res.defaultKinds().set_doublePrecisionKind(8);
352   }
353   if (args.hasArg(clang::driver::options::OPT_flarge_sizes))
354     res.defaultKinds().set_sizeIntegerKind(8);
355 
356   // -fopenmp and -fopenacc
357   if (args.hasArg(clang::driver::options::OPT_fopenacc)) {
358     res.frontendOpts().features_.Enable(
359         Fortran::common::LanguageFeature::OpenACC);
360   }
361   if (args.hasArg(clang::driver::options::OPT_fopenmp)) {
362     res.frontendOpts().features_.Enable(
363         Fortran::common::LanguageFeature::OpenMP);
364   }
365   return;
366 }
367 
368 bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
369     llvm::ArrayRef<const char *> commandLineArgs,
370     clang::DiagnosticsEngine &diags) {
371 
372   bool success = true;
373 
374   // Parse the arguments
375   const llvm::opt::OptTable &opts = clang::driver::getDriverOptTable();
376   const unsigned includedFlagsBitmask = clang::driver::options::FC1Option;
377   unsigned missingArgIndex, missingArgCount;
378   llvm::opt::InputArgList args = opts.ParseArgs(
379       commandLineArgs, missingArgIndex, missingArgCount, includedFlagsBitmask);
380 
381   // Issue errors on unknown arguments
382   for (const auto *a : args.filtered(clang::driver::options::OPT_UNKNOWN)) {
383     auto argString = a->getAsString(args);
384     std::string nearest;
385     if (opts.findNearest(argString, nearest, includedFlagsBitmask) > 1)
386       diags.Report(clang::diag::err_drv_unknown_argument) << argString;
387     else
388       diags.Report(clang::diag::err_drv_unknown_argument_with_suggestion)
389           << argString << nearest;
390     success = false;
391   }
392 
393   // Parse the frontend args
394   ParseFrontendArgs(res.frontendOpts(), args, diags);
395   // Parse the preprocessor args
396   parsePreprocessorArgs(res.preprocessorOpts(), args);
397   // Parse semantic args
398   parseSemaArgs(res.moduleDir(), args, diags);
399   // Parse dialect arguments
400   parseDialectArgs(res, args, diags);
401 
402   return success;
403 }
404 
405 /// Collect the macro definitions provided by the given preprocessor
406 /// options into the parser options.
407 ///
408 /// \param [in] ppOpts The preprocessor options
409 /// \param [out] opts The fortran options
410 static void collectMacroDefinitions(
411     const PreprocessorOptions &ppOpts, Fortran::parser::Options &opts) {
412   for (unsigned i = 0, n = ppOpts.macros.size(); i != n; ++i) {
413     llvm::StringRef macro = ppOpts.macros[i].first;
414     bool isUndef = ppOpts.macros[i].second;
415 
416     std::pair<llvm::StringRef, llvm::StringRef> macroPair = macro.split('=');
417     llvm::StringRef macroName = macroPair.first;
418     llvm::StringRef macroBody = macroPair.second;
419 
420     // For an #undef'd macro, we only care about the name.
421     if (isUndef) {
422       opts.predefinitions.emplace_back(
423           macroName.str(), std::optional<std::string>{});
424       continue;
425     }
426 
427     // For a #define'd macro, figure out the actual definition.
428     if (macroName.size() == macro.size())
429       macroBody = "1";
430     else {
431       // Note: GCC drops anything following an end-of-line character.
432       llvm::StringRef::size_type End = macroBody.find_first_of("\n\r");
433       macroBody = macroBody.substr(0, End);
434     }
435     opts.predefinitions.emplace_back(
436         macroName, std::optional<std::string>(macroBody.str()));
437   }
438 }
439 
440 void CompilerInvocation::SetDefaultFortranOpts() {
441   auto &fortranOptions = fortranOpts();
442 
443   // These defaults are based on the defaults in f18/f18.cpp.
444   std::vector<std::string> searchDirectories{"."s};
445   fortranOptions.searchDirectories = searchDirectories;
446   fortranOptions.isFixedForm = false;
447 }
448 
449 // TODO: When expanding this method, consider creating a dedicated API for
450 // this. Also at some point we will need to differentiate between different
451 // targets and add dedicated predefines for each.
452 void CompilerInvocation::setDefaultPredefinitions() {
453   auto &fortranOptions = fortranOpts();
454   const auto &frontendOptions = frontendOpts();
455 
456   // Populate the macro list with version numbers and other predefinitions.
457   fortranOptions.predefinitions.emplace_back("__flang__", "1");
458   fortranOptions.predefinitions.emplace_back(
459       "__flang_major__", FLANG_VERSION_MAJOR_STRING);
460   fortranOptions.predefinitions.emplace_back(
461       "__flang_minor__", FLANG_VERSION_MINOR_STRING);
462   fortranOptions.predefinitions.emplace_back(
463       "__flang_patchlevel__", FLANG_VERSION_PATCHLEVEL_STRING);
464 
465   // Add predefinitions based on extensions enabled
466   if (frontendOptions.features_.IsEnabled(
467           Fortran::common::LanguageFeature::OpenACC)) {
468     fortranOptions.predefinitions.emplace_back("_OPENACC", "202011");
469   }
470   if (frontendOptions.features_.IsEnabled(
471           Fortran::common::LanguageFeature::OpenMP)) {
472     fortranOptions.predefinitions.emplace_back("_OPENMP", "201511");
473   }
474 }
475 
476 void CompilerInvocation::setFortranOpts() {
477   auto &fortranOptions = fortranOpts();
478   const auto &frontendOptions = frontendOpts();
479   const auto &preprocessorOptions = preprocessorOpts();
480   auto &moduleDirJ = moduleDir();
481 
482   if (frontendOptions.fortranForm_ != FortranForm::Unknown) {
483     fortranOptions.isFixedForm =
484         frontendOptions.fortranForm_ == FortranForm::FixedForm;
485   }
486   fortranOptions.fixedFormColumns = frontendOptions.fixedFormColumns_;
487 
488   fortranOptions.features = frontendOptions.features_;
489   fortranOptions.encoding = frontendOptions.encoding_;
490 
491   collectMacroDefinitions(preprocessorOptions, fortranOptions);
492 
493   fortranOptions.searchDirectories.insert(
494       fortranOptions.searchDirectories.end(),
495       preprocessorOptions.searchDirectoriesFromDashI.begin(),
496       preprocessorOptions.searchDirectoriesFromDashI.end());
497 
498   // Add the directory supplied through -J/-module-dir to the list of search
499   // directories
500   if (moduleDirJ.compare(".") != 0)
501     fortranOptions.searchDirectories.emplace_back(moduleDirJ);
502 
503   if (frontendOptions.instrumentedParse_)
504     fortranOptions.instrumentedParse = true;
505 }
506 
507 void CompilerInvocation::setSemanticsOpts(
508     Fortran::parser::AllCookedSources &allCookedSources) {
509   const auto &fortranOptions = fortranOpts();
510 
511   semanticsContext_ = std::make_unique<semantics::SemanticsContext>(
512       defaultKinds(), fortranOptions.features, allCookedSources);
513 
514   auto &moduleDirJ = moduleDir();
515   semanticsContext_->set_moduleDirectory(moduleDirJ)
516       .set_searchDirectories(fortranOptions.searchDirectories);
517 }
518