1 //===- CompilerInvocation.h - Compiler Invocation Helper Data ---*- C -*-===//
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 #ifndef LLVM_FLANG_FRONTEND_COMPILERINVOCATION_H
9 #define LLVM_FLANG_FRONTEND_COMPILERINVOCATION_H
10 
11 #include "flang/Frontend/FrontendOptions.h"
12 #include "flang/Frontend/PreprocessorOptions.h"
13 #include "flang/Parser/parsing.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/DiagnosticOptions.h"
16 #include "llvm/Option/ArgList.h"
17 #include <memory>
18 
19 namespace Fortran::frontend {
20 
21 /// Fill out Opts based on the options given in Args.
22 ///
23 /// When errors are encountered, return false and, if Diags is non-null,
24 /// report the error(s).
25 bool ParseDiagnosticArgs(clang::DiagnosticOptions &opts,
26     llvm::opt::ArgList &args, bool defaultDiagColor = true);
27 
28 class CompilerInvocationBase {
29 public:
30   /// Options controlling the diagnostic engine.
31   llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagnosticOpts_;
32   /// Options for the preprocessor.
33   std::shared_ptr<Fortran::frontend::PreprocessorOptions> preprocessorOpts_;
34 
35   CompilerInvocationBase();
36   CompilerInvocationBase(const CompilerInvocationBase &x);
37   ~CompilerInvocationBase();
38 
39   clang::DiagnosticOptions &GetDiagnosticOpts() {
40     return *diagnosticOpts_.get();
41   }
42   const clang::DiagnosticOptions &GetDiagnosticOpts() const {
43     return *diagnosticOpts_.get();
44   }
45 
46   PreprocessorOptions &preprocessorOpts() { return *preprocessorOpts_; }
47   const PreprocessorOptions &preprocessorOpts() const {
48     return *preprocessorOpts_;
49   }
50 };
51 
52 class CompilerInvocation : public CompilerInvocationBase {
53   /// Options for the frontend driver
54   // TODO: Merge with or translate to parserOpts_. We shouldn't need two sets of
55   // options.
56   FrontendOptions frontendOpts_;
57 
58   /// Options for Flang parser
59   // TODO: Merge with or translate to frontendOpts_. We shouldn't need two sets
60   // of options.
61   Fortran::parser::Options parserOpts_;
62 
63 public:
64   CompilerInvocation() = default;
65 
66   FrontendOptions &frontendOpts() { return frontendOpts_; }
67   const FrontendOptions &frontendOpts() const { return frontendOpts_; }
68 
69   Fortran::parser::Options &fortranOpts() { return parserOpts_; }
70   const Fortran::parser::Options &fortranOpts() const { return parserOpts_; }
71 
72   /// Create a compiler invocation from a list of input options.
73   /// \returns true on success.
74   /// \returns false if an error was encountered while parsing the arguments
75   /// \param [out] res - The resulting invocation.
76   static bool CreateFromArgs(CompilerInvocation &res,
77       llvm::ArrayRef<const char *> commandLineArgs,
78       clang::DiagnosticsEngine &diags);
79 
80   /// Set the Fortran options to predifined defaults. These defaults are
81   /// consistend with f18/f18.cpp.
82   // TODO: We should map frontendOpts_ to parserOpts_ instead. For that, we
83   // need to extend frontendOpts_ first. Next, we need to add the corresponding
84   // compiler driver options in libclangDriver.
85   void SetDefaultFortranOpts();
86 
87   /// Set the Fortran options to user-specified values.
88   /// These values are found in the preprocessor options.
89   void setFortranOpts();
90 };
91 
92 } // end namespace Fortran::frontend
93 #endif // LLVM_FLANG_FRONTEND_COMPILERINVOCATION_H
94