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 "clang/Basic/AllDiagnostics.h"
11 #include "clang/Basic/DiagnosticDriver.h"
12 #include "clang/Basic/DiagnosticOptions.h"
13 #include "clang/Driver/DriverDiagnostic.h"
14 #include "clang/Driver/Options.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/Option/Arg.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Option/OptTable.h"
20 #include "llvm/Support/raw_ostream.h"
21 
22 using namespace Fortran::frontend;
23 
24 //===----------------------------------------------------------------------===//
25 // Initialization.
26 //===----------------------------------------------------------------------===//
27 CompilerInvocationBase::CompilerInvocationBase()
28     : diagnosticOpts_(new clang::DiagnosticOptions()) {}
29 
30 CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &x)
31     : diagnosticOpts_(new clang::DiagnosticOptions(x.GetDiagnosticOpts())) {}
32 
33 CompilerInvocationBase::~CompilerInvocationBase() = default;
34 
35 //===----------------------------------------------------------------------===//
36 // Deserialization (from args)
37 //===----------------------------------------------------------------------===//
38 static InputKind ParseFrontendArgs(FrontendOptions &opts,
39     llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) {
40   // Identify the action (i.e. opts.ProgramAction)
41   if (const llvm::opt::Arg *a =
42           args.getLastArg(clang::driver::options::OPT_Action_Group)) {
43     switch (a->getOption().getID()) {
44     default: {
45       llvm_unreachable("Invalid option in group!");
46     }
47       // TODO:
48       // case clang::driver::options::OPT_E:
49       // case clang::driver::options::OPT_emit_obj:
50       // case calng::driver::options::OPT_emit_llvm:
51       // case clang::driver::options::OPT_emit_llvm_only:
52       // case clang::driver::options::OPT_emit_codegen_only:
53       // case clang::driver::options::OPT_emit_module:
54       // (...)
55     }
56   }
57 
58   opts.showHelp_ = args.hasArg(clang::driver::options::OPT_help);
59   opts.showVersion_ = args.hasArg(clang::driver::options::OPT_version);
60 
61   // Get the input kind (from the value passed via `-x`)
62   InputKind dashX(Language::Unknown);
63   if (const llvm::opt::Arg *a =
64           args.getLastArg(clang::driver::options::OPT_x)) {
65     llvm::StringRef XValue = a->getValue();
66     // Principal languages.
67     dashX = llvm::StringSwitch<InputKind>(XValue)
68                 .Case("f90", Language::Fortran)
69                 .Default(Language::Unknown);
70 
71     // Some special cases cannot be combined with suffixes.
72     if (dashX.IsUnknown())
73       dashX = llvm::StringSwitch<InputKind>(XValue)
74                   .Case("ir", Language::LLVM_IR)
75                   .Default(Language::Unknown);
76 
77     if (dashX.IsUnknown())
78       diags.Report(clang::diag::err_drv_invalid_value)
79           << a->getAsString(args) << a->getValue();
80   }
81 
82   return dashX;
83 }
84 
85 bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
86     llvm::ArrayRef<const char *> commandLineArgs,
87     clang::DiagnosticsEngine &diags) {
88 
89   bool success = true;
90 
91   // Parse the arguments
92   const llvm::opt::OptTable &opts = clang::driver::getDriverOptTable();
93   const unsigned includedFlagsBitmask =
94       clang::driver::options::FC1Option;
95   unsigned missingArgIndex, missingArgCount;
96   llvm::opt::InputArgList args = opts.ParseArgs(
97       commandLineArgs, missingArgIndex, missingArgCount, includedFlagsBitmask);
98 
99   // Issue errors on unknown arguments
100   for (const auto *a : args.filtered(clang::driver::options::OPT_UNKNOWN)) {
101     auto argString = a->getAsString(args);
102     std::string nearest;
103     if (opts.findNearest(argString, nearest, includedFlagsBitmask) > 1)
104       diags.Report(clang::diag::err_drv_unknown_argument) << argString;
105     else
106       diags.Report(clang::diag::err_drv_unknown_argument_with_suggestion)
107           << argString << nearest;
108     success = false;
109   }
110 
111   // Parse the frontend args
112   ParseFrontendArgs(res.GetFrontendOpts(), args, diags);
113 
114   return success;
115 }
116