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 "flang/Semantics/semantics.h"
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Basic/DiagnosticOptions.h"
17 #include "llvm/Option/ArgList.h"
18 #include <memory>
19 
20 namespace Fortran::frontend {
21 
22 /// Fill out Opts based on the options given in Args.
23 ///
24 /// When errors are encountered, return false and, if Diags is non-null,
25 /// report the error(s).
26 bool ParseDiagnosticArgs(clang::DiagnosticOptions &opts,
27     llvm::opt::ArgList &args, bool defaultDiagColor = true);
28 
29 class CompilerInvocationBase {
30 public:
31   /// Options controlling the diagnostic engine.
32   llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagnosticOpts_;
33   /// Options for the preprocessor.
34   std::shared_ptr<Fortran::frontend::PreprocessorOptions> preprocessorOpts_;
35 
36   CompilerInvocationBase();
37   CompilerInvocationBase(const CompilerInvocationBase &x);
38   ~CompilerInvocationBase();
39 
40   clang::DiagnosticOptions &GetDiagnosticOpts() {
41     return *diagnosticOpts_.get();
42   }
43   const clang::DiagnosticOptions &GetDiagnosticOpts() const {
44     return *diagnosticOpts_.get();
45   }
46 
47   PreprocessorOptions &preprocessorOpts() { return *preprocessorOpts_; }
48   const PreprocessorOptions &preprocessorOpts() const {
49     return *preprocessorOpts_;
50   }
51 };
52 
53 class CompilerInvocation : public CompilerInvocationBase {
54   /// Options for the frontend driver
55   // TODO: Merge with or translate to parserOpts_. We shouldn't need two sets of
56   // options.
57   FrontendOptions frontendOpts_;
58 
59   /// Options for Flang parser
60   // TODO: Merge with or translate to frontendOpts_. We shouldn't need two sets
61   // of options.
62   Fortran::parser::Options parserOpts_;
63 
64   // Semantics context
65   std::unique_ptr<Fortran::semantics::SemanticsContext> semanticsContext_;
66 
67   /// Semantic options
68   // TODO: Merge with or translate to frontendOpts_. We shouldn't need two sets
69   // of options.
70   std::string moduleDir_ = ".";
71 
72   bool debugModuleDir_ = false;
73 
74   // Fortran Dialect options
75   Fortran::common::IntrinsicTypeDefaultKinds defaultKinds_;
76 
77   bool EnableConformanceChecks_ = false;
78 
79 public:
80   CompilerInvocation() = default;
81 
82   FrontendOptions &frontendOpts() { return frontendOpts_; }
83   const FrontendOptions &frontendOpts() const { return frontendOpts_; }
84 
85   Fortran::parser::Options &fortranOpts() { return parserOpts_; }
86   const Fortran::parser::Options &fortranOpts() const { return parserOpts_; }
87 
88   Fortran::semantics::SemanticsContext &semanticsContext() {
89     return *semanticsContext_;
90   }
91   const Fortran::semantics::SemanticsContext &semanticsContext() const {
92     return *semanticsContext_;
93   }
94 
95   std::string &moduleDir() { return moduleDir_; }
96   const std::string &moduleDir() const { return moduleDir_; }
97 
98   bool &debugModuleDir() { return debugModuleDir_; }
99   const bool &debugModuleDir() const { return debugModuleDir_; }
100 
101   bool &enableConformanceChecks() { return EnableConformanceChecks_; }
102   const bool &enableConformanceChecks() const {
103     return EnableConformanceChecks_;
104   }
105 
106   Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds() {
107     return defaultKinds_;
108   }
109   const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds() const {
110     return defaultKinds_;
111   }
112 
113   /// Create a compiler invocation from a list of input options.
114   /// \returns true on success.
115   /// \returns false if an error was encountered while parsing the arguments
116   /// \param [out] res - The resulting invocation.
117   static bool CreateFromArgs(CompilerInvocation &res,
118       llvm::ArrayRef<const char *> commandLineArgs,
119       clang::DiagnosticsEngine &diags);
120 
121   // Enables the std=f2018 conformance check
122   void set_EnableConformanceChecks() { EnableConformanceChecks_ = true; }
123 
124   /// Useful setters
125   void SetModuleDir(std::string &moduleDir) { moduleDir_ = moduleDir; }
126 
127   void SetDebugModuleDir(bool flag) { debugModuleDir_ = flag; }
128 
129   /// Set the Fortran options to predifined defaults. These defaults are
130   /// consistend with f18/f18.cpp.
131   // TODO: We should map frontendOpts_ to parserOpts_ instead. For that, we
132   // need to extend frontendOpts_ first. Next, we need to add the corresponding
133   // compiler driver options in libclangDriver.
134   void SetDefaultFortranOpts();
135 
136   /// Set the default predefinitions.
137   void setDefaultPredefinitions();
138 
139   /// Set the Fortran options to user-specified values.
140   /// These values are found in the preprocessor options.
141   void setFortranOpts();
142 
143   /// Set the Semantic Options
144   void setSemanticsOpts(Fortran::parser::AllCookedSources &);
145 };
146 
147 } // end namespace Fortran::frontend
148 #endif // LLVM_FLANG_FRONTEND_COMPILERINVOCATION_H
149