Warning: This file is not a C or C++ file. It does not have highlighting.

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//
9// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_FRONTEND_COMPILERINVOCATION_H
14#define FORTRAN_FRONTEND_COMPILERINVOCATION_H
15
16#include "flang/Frontend/CodeGenOptions.h"
17#include "flang/Frontend/FrontendOptions.h"
18#include "flang/Frontend/LangOptions.h"
19#include "flang/Frontend/PreprocessorOptions.h"
20#include "flang/Frontend/TargetOptions.h"
21#include "flang/Lower/LoweringOptions.h"
22#include "flang/Parser/parsing.h"
23#include "flang/Semantics/semantics.h"
24#include "clang/Basic/Diagnostic.h"
25#include "clang/Basic/DiagnosticOptions.h"
26#include "llvm/Option/ArgList.h"
27#include <memory>
28
29namespace llvm {
30class TargetMachine;
31}
32
33namespace Fortran::frontend {
34
35/// Fill out Opts based on the options given in Args.
36///
37/// When errors are encountered, return false and, if Diags is non-null,
38/// report the error(s).
39bool parseDiagnosticArgs(clang::DiagnosticOptions &opts,
40 llvm::opt::ArgList &args);
41
42class CompilerInvocationBase {
43public:
44 /// Options controlling the diagnostic engine.
45 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagnosticOpts;
46 /// Options for the preprocessor.
47 std::shared_ptr<Fortran::frontend::PreprocessorOptions> preprocessorOpts;
48
49 CompilerInvocationBase();
50 CompilerInvocationBase(const CompilerInvocationBase &x);
51 ~CompilerInvocationBase();
52
53 clang::DiagnosticOptions &getDiagnosticOpts() {
54 return *diagnosticOpts.get();
55 }
56 const clang::DiagnosticOptions &getDiagnosticOpts() const {
57 return *diagnosticOpts.get();
58 }
59
60 PreprocessorOptions &getPreprocessorOpts() { return *preprocessorOpts; }
61 const PreprocessorOptions &getPreprocessorOpts() const {
62 return *preprocessorOpts;
63 }
64};
65
66class CompilerInvocation : public CompilerInvocationBase {
67 /// Options for the frontend driver
68 // TODO: Merge with or translate to parserOpts_. We shouldn't need two sets of
69 // options.
70 FrontendOptions frontendOpts;
71
72 /// Options for Flang parser
73 // TODO: Merge with or translate to frontendOpts. We shouldn't need two sets
74 // of options.
75 Fortran::parser::Options parserOpts;
76
77 /// Options controlling lowering.
78 Fortran::lower::LoweringOptions loweringOpts;
79
80 /// Options controlling the target.
81 Fortran::frontend::TargetOptions targetOpts;
82
83 /// Options controlling IRgen and the backend.
84 Fortran::frontend::CodeGenOptions codeGenOpts;
85
86 /// Options controlling language dialect.
87 Fortran::frontend::LangOptions langOpts;
88
89 // The original invocation of the compiler driver.
90 // This string will be set as the return value from the COMPILER_OPTIONS
91 // intrinsic of iso_fortran_env.
92 std::string allCompilerInvocOpts;
93
94 /// Semantic options
95 // TODO: Merge with or translate to frontendOpts. We shouldn't need two sets
96 // of options.
97 std::string moduleDir = ".";
98
99 std::string moduleFileSuffix = ".mod";
100
101 bool debugModuleDir = false;
102
103 bool warnAsErr = false;
104
105 // Executable name
106 const char *argv0;
107
108 /// This flag controls the unparsing and is used to decide whether to print
109 /// out the semantically analyzed version of an object or expression or the
110 /// plain version that does not include any information from semantic
111 /// analysis.
112 bool useAnalyzedObjectsForUnparse = true;
113
114 // Fortran Dialect options
115 Fortran::common::IntrinsicTypeDefaultKinds defaultKinds;
116
117 bool enableConformanceChecks = false;
118 bool enableUsageChecks = false;
119
120 /// Used in e.g. unparsing to dump the analyzed rather than the original
121 /// parse-tree objects.
122 Fortran::parser::AnalyzedObjectsAsFortran asFortran{
123 [](llvm::raw_ostream &o, const Fortran::evaluate::GenericExprWrapper &x) {
124 if (x.v) {
125 x.v->AsFortran(o);
126 } else {
127 o << "(bad expression)";
128 }
129 },
130 [](llvm::raw_ostream &o,
131 const Fortran::evaluate::GenericAssignmentWrapper &x) {
132 if (x.v) {
133 x.v->AsFortran(o);
134 } else {
135 o << "(bad assignment)";
136 }
137 },
138 [](llvm::raw_ostream &o, const Fortran::evaluate::ProcedureRef &x) {
139 x.AsFortran(o << "CALL ");
140 },
141 };
142
143public:
144 CompilerInvocation() = default;
145
146 FrontendOptions &getFrontendOpts() { return frontendOpts; }
147 const FrontendOptions &getFrontendOpts() const { return frontendOpts; }
148
149 Fortran::parser::Options &getFortranOpts() { return parserOpts; }
150 const Fortran::parser::Options &getFortranOpts() const { return parserOpts; }
151
152 TargetOptions &getTargetOpts() { return targetOpts; }
153 const TargetOptions &getTargetOpts() const { return targetOpts; }
154
155 CodeGenOptions &getCodeGenOpts() { return codeGenOpts; }
156 const CodeGenOptions &getCodeGenOpts() const { return codeGenOpts; }
157
158 LangOptions &getLangOpts() { return langOpts; }
159 const LangOptions &getLangOpts() const { return langOpts; }
160
161 Fortran::lower::LoweringOptions &getLoweringOpts() { return loweringOpts; }
162 const Fortran::lower::LoweringOptions &getLoweringOpts() const {
163 return loweringOpts;
164 }
165
166 /// Creates and configures semantics context based on the compilation flags.
167 std::unique_ptr<Fortran::semantics::SemanticsContext>
168 getSemanticsCtx(Fortran::parser::AllCookedSources &allCookedSources,
169 const llvm::TargetMachine &);
170
171 std::string &getModuleDir() { return moduleDir; }
172 const std::string &getModuleDir() const { return moduleDir; }
173
174 std::string &getModuleFileSuffix() { return moduleFileSuffix; }
175 const std::string &getModuleFileSuffix() const { return moduleFileSuffix; }
176
177 bool &getDebugModuleDir() { return debugModuleDir; }
178 const bool &getDebugModuleDir() const { return debugModuleDir; }
179
180 bool &getWarnAsErr() { return warnAsErr; }
181 const bool &getWarnAsErr() const { return warnAsErr; }
182
183 bool &getUseAnalyzedObjectsForUnparse() {
184 return useAnalyzedObjectsForUnparse;
185 }
186 const bool &getUseAnalyzedObjectsForUnparse() const {
187 return useAnalyzedObjectsForUnparse;
188 }
189
190 bool &getEnableConformanceChecks() { return enableConformanceChecks; }
191 const bool &getEnableConformanceChecks() const {
192 return enableConformanceChecks;
193 }
194
195 const char *getArgv0() { return argv0; }
196
197 bool &getEnableUsageChecks() { return enableUsageChecks; }
198 const bool &getEnableUsageChecks() const { return enableUsageChecks; }
199
200 Fortran::parser::AnalyzedObjectsAsFortran &getAsFortran() {
201 return asFortran;
202 }
203 const Fortran::parser::AnalyzedObjectsAsFortran &getAsFortran() const {
204 return asFortran;
205 }
206
207 Fortran::common::IntrinsicTypeDefaultKinds &getDefaultKinds() {
208 return defaultKinds;
209 }
210 const Fortran::common::IntrinsicTypeDefaultKinds &getDefaultKinds() const {
211 return defaultKinds;
212 }
213
214 /// Create a compiler invocation from a list of input options.
215 /// \returns true on success.
216 /// \returns false if an error was encountered while parsing the arguments
217 /// \param [out] res - The resulting invocation.
218 static bool createFromArgs(CompilerInvocation &res,
219 llvm::ArrayRef<const char *> commandLineArgs,
220 clang::DiagnosticsEngine &diags,
221 const char *argv0 = nullptr);
222
223 // Enables the std=f2018 conformance check
224 void setEnableConformanceChecks() { enableConformanceChecks = true; }
225
226 // Enables the usage checks
227 void setEnableUsageChecks() { enableUsageChecks = true; }
228
229 /// Useful setters
230 void setArgv0(const char *dir) { argv0 = dir; }
231
232 void setModuleDir(std::string &dir) { moduleDir = dir; }
233
234 void setModuleFileSuffix(const char *suffix) {
235 moduleFileSuffix = std::string(suffix);
236 }
237
238 void setDebugModuleDir(bool flag) { debugModuleDir = flag; }
239
240 void setWarnAsErr(bool flag) { warnAsErr = flag; }
241
242 void setUseAnalyzedObjectsForUnparse(bool flag) {
243 useAnalyzedObjectsForUnparse = flag;
244 }
245
246 /// Set the Fortran options to predefined defaults.
247 // TODO: We should map frontendOpts_ to parserOpts_ instead. For that, we
248 // need to extend frontendOpts_ first. Next, we need to add the corresponding
249 // compiler driver options in libclangDriver.
250 void setDefaultFortranOpts();
251
252 /// Set the default predefinitions.
253 void setDefaultPredefinitions();
254
255 /// Collect the macro definitions from preprocessorOpts_ and prepare them for
256 /// the parser (i.e. copy into parserOpts_)
257 void collectMacroDefinitions();
258
259 /// Set the Fortran options to user-specified values.
260 /// These values are found in the preprocessor options.
261 void setFortranOpts();
262
263 /// Set the Semantic Options
264 void setSemanticsOpts(Fortran::parser::AllCookedSources &);
265
266 /// Set \p loweringOptions controlling lowering behavior based
267 /// on the \p optimizationLevel.
268 void setLoweringOptions();
269};
270
271} // end namespace Fortran::frontend
272#endif // FORTRAN_FRONTEND_COMPILERINVOCATION_H
273

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of flang/include/flang/Frontend/CompilerInvocation.h