1//===- LangOptions.h - C Language Family Language Options -------*- 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/// \file
10/// Defines the clang::LangOptions interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_LANGOPTIONS_H
15#define LLVM_CLANG_BASIC_LANGOPTIONS_H
16
17#include "clang/Basic/CommentOptions.h"
18#include "clang/Basic/LLVM.h"
19#include "clang/Basic/ObjCRuntime.h"
20#include "clang/Basic/Sanitizers.h"
21#include "clang/Basic/Visibility.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Triple.h"
24#include <string>
25#include <vector>
26
27namespace clang {
28
29/// Bitfields of LangOptions, split out from LangOptions in order to ensure that
30/// this large collection of bitfields is a trivial class type.
31class LangOptionsBase {
32public:
33 // Define simple language options (with no accessors).
34#define LANGOPT(Name, Bits, Default, Description) unsigned Name : Bits;
35#define ENUM_LANGOPT(Name, Type, Bits, Default, Description)
36#include "clang/Basic/LangOptions.def"
37
38protected:
39 // Define language options of enumeration type. These are private, and will
40 // have accessors (below).
41#define LANGOPT(Name, Bits, Default, Description)
42#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
43 unsigned Name : Bits;
44#include "clang/Basic/LangOptions.def"
45};
46
47/// Keeps track of the various options that can be
48/// enabled, which controls the dialect of C or C++ that is accepted.
49class LangOptions : public LangOptionsBase {
50public:
51 using Visibility = clang::Visibility;
52
53 enum GCMode { NonGC, GCOnly, HybridGC };
54 enum StackProtectorMode { SSPOff, SSPOn, SSPStrong, SSPReq };
55
56 // Automatic variables live on the stack, and when trivial they're usually
57 // uninitialized because it's undefined behavior to use them without
58 // initializing them.
59 enum class TrivialAutoVarInitKind { Uninitialized, Zero, Pattern };
60
61 enum SignedOverflowBehaviorTy {
62 // Default C standard behavior.
63 SOB_Undefined,
64
65 // -fwrapv
66 SOB_Defined,
67
68 // -ftrapv
69 SOB_Trapping
70 };
71
72 // FIXME: Unify with TUKind.
73 enum CompilingModuleKind {
74 /// Not compiling a module interface at all.
75 CMK_None,
76
77 /// Compiling a module from a module map.
78 CMK_ModuleMap,
79
80 /// Compiling a module from a list of header files.
81 CMK_HeaderModule,
82
83 /// Compiling a C++ modules TS module interface unit.
84 CMK_ModuleInterface,
85 };
86
87 enum PragmaMSPointersToMembersKind {
88 PPTMK_BestCase,
89 PPTMK_FullGeneralitySingleInheritance,
90 PPTMK_FullGeneralityMultipleInheritance,
91 PPTMK_FullGeneralityVirtualInheritance
92 };
93
94 enum DefaultCallingConvention {
95 DCC_None,
96 DCC_CDecl,
97 DCC_FastCall,
98 DCC_StdCall,
99 DCC_VectorCall,
100 DCC_RegCall
101 };
102
103 enum AddrSpaceMapMangling { ASMM_Target, ASMM_On, ASMM_Off };
104
105 // Corresponds to _MSC_VER
106 enum MSVCMajorVersion {
107 MSVC2010 = 1600,
108 MSVC2012 = 1700,
109 MSVC2013 = 1800,
110 MSVC2015 = 1900,
111 MSVC2017 = 1910,
112 MSVC2017_5 = 1912
113 };
114
115 /// Clang versions with different platform ABI conformance.
116 enum class ClangABI {
117 /// Attempt to be ABI-compatible with code generated by Clang 3.8.x
118 /// (SVN r257626). This causes <1 x long long> to be passed in an
119 /// integer register instead of an SSE register on x64_64.
120 Ver3_8,
121
122 /// Attempt to be ABI-compatible with code generated by Clang 4.0.x
123 /// (SVN r291814). This causes move operations to be ignored when
124 /// determining whether a class type can be passed or returned directly.
125 Ver4,
126
127 /// Attempt to be ABI-compatible with code generated by Clang 6.0.x
128 /// (SVN r321711). This causes determination of whether a type is
129 /// standard-layout to ignore collisions between empty base classes
130 /// and between base classes and member subobjects, which affects
131 /// whether we reuse base class tail padding in some ABIs.
132 Ver6,
133
134 /// Attempt to be ABI-compatible with code generated by Clang 7.0.x
135 /// (SVN r338536). This causes alignof (C++) and _Alignof (C11) to be
136 /// compatible with __alignof (i.e., return the preferred alignment)
137 /// rather than returning the required alignment.
138 Ver7,
139
140 /// Conform to the underlying platform's C and C++ ABIs as closely
141 /// as we can.
142 Latest
143 };
144
145 enum class CoreFoundationABI {
146 /// No interoperability ABI has been specified
147 Unspecified,
148 /// CoreFoundation does not have any language interoperability
149 Standalone,
150 /// Interoperability with the ObjectiveC runtime
151 ObjectiveC,
152 /// Interoperability with the latest known version of the Swift runtime
153 Swift,
154 /// Interoperability with the Swift 5.0 runtime
155 Swift5_0,
156 /// Interoperability with the Swift 4.2 runtime
157 Swift4_2,
158 /// Interoperability with the Swift 4.1 runtime
159 Swift4_1,
160 };
161
162 enum FPContractModeKind {
163 // Form fused FP ops only where result will not be affected.
164 FPC_Off,
165
166 // Form fused FP ops according to FP_CONTRACT rules.
167 FPC_On,
168
169 // Aggressively fuse FP ops (E.g. FMA).
170 FPC_Fast
171 };
172
173 // TODO: merge FEnvAccessModeKind and FPContractModeKind
174 enum FEnvAccessModeKind {
175 FEA_Off,
176
177 FEA_On
178 };
179
180
181public:
182 /// Set of enabled sanitizers.
183 SanitizerSet Sanitize;
184
185 /// Paths to blacklist files specifying which objects
186 /// (files, functions, variables) should not be instrumented.
187 std::vector<std::string> SanitizerBlacklistFiles;
188
189 /// Paths to the XRay "always instrument" files specifying which
190 /// objects (files, functions, variables) should be imbued with the XRay
191 /// "always instrument" attribute.
192 /// WARNING: This is a deprecated field and will go away in the future.
193 std::vector<std::string> XRayAlwaysInstrumentFiles;
194
195 /// Paths to the XRay "never instrument" files specifying which
196 /// objects (files, functions, variables) should be imbued with the XRay
197 /// "never instrument" attribute.
198 /// WARNING: This is a deprecated field and will go away in the future.
199 std::vector<std::string> XRayNeverInstrumentFiles;
200
201 /// Paths to the XRay attribute list files, specifying which objects
202 /// (files, functions, variables) should be imbued with the appropriate XRay
203 /// attribute(s).
204 std::vector<std::string> XRayAttrListFiles;
205
206 clang::ObjCRuntime ObjCRuntime;
207
208 CoreFoundationABI CFRuntime = CoreFoundationABI::Unspecified;
209
210 std::string ObjCConstantStringClass;
211
212 /// The name of the handler function to be called when -ftrapv is
213 /// specified.
214 ///
215 /// If none is specified, abort (GCC-compatible behaviour).
216 std::string OverflowHandler;
217
218 /// The module currently being compiled as specified by -fmodule-name.
219 std::string ModuleName;
220
221 /// The name of the current module, of which the main source file
222 /// is a part. If CompilingModule is set, we are compiling the interface
223 /// of this module, otherwise we are compiling an implementation file of
224 /// it. This starts as ModuleName in case -fmodule-name is provided and
225 /// changes during compilation to reflect the current module.
226 std::string CurrentModule;
227
228 /// The names of any features to enable in module 'requires' decls
229 /// in addition to the hard-coded list in Module.cpp and the target features.
230 ///
231 /// This list is sorted.
232 std::vector<std::string> ModuleFeatures;
233
234 /// Options for parsing comments.
235 CommentOptions CommentOpts;
236
237 /// A list of all -fno-builtin-* function names (e.g., memset).
238 std::vector<std::string> NoBuiltinFuncs;
239
240 /// Triples of the OpenMP targets that the host code codegen should
241 /// take into account in order to generate accurate offloading descriptors.
242 std::vector<llvm::Triple> OMPTargetTriples;
243
244 /// Name of the IR file that contains the result of the OpenMP target
245 /// host code generation.
246 std::string OMPHostIRFile;
247
248 /// Indicates whether the front-end is explicitly told that the
249 /// input is a header file (i.e. -x c-header).
250 bool IsHeaderFile = false;
251
252 LangOptions();
253
254 // Define accessors/mutators for language options of enumeration type.
255#define LANGOPT(Name, Bits, Default, Description)
256#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
257 Type get##Name() const { return static_cast<Type>(Name); } \
258 void set##Name(Type Value) { Name = static_cast<unsigned>(Value); }
259#include "clang/Basic/LangOptions.def"
260
261 /// Are we compiling a module interface (.cppm or module map)?
262 bool isCompilingModule() const {
263 return getCompilingModule() != CMK_None;
264 }
265
266 /// Do we need to track the owning module for a local declaration?
267 bool trackLocalOwningModule() const {
268 return isCompilingModule() || ModulesLocalVisibility || ModulesTS;
269 }
270
271 bool isSignedOverflowDefined() const {
272 return getSignedOverflowBehavior() == SOB_Defined;
273 }
274
275 bool isSubscriptPointerArithmetic() const {
276 return ObjCRuntime.isSubscriptPointerArithmetic() &&
277 !ObjCSubscriptingLegacyRuntime;
278 }
279
280 bool isCompatibleWithMSVC(MSVCMajorVersion MajorVersion) const {
281 return MSCompatibilityVersion >= MajorVersion * 100000U;
282 }
283
284 /// Reset all of the options that are not considered when building a
285 /// module.
286 void resetNonModularOptions();
287
288 /// Is this a libc/libm function that is no longer recognized as a
289 /// builtin because a -fno-builtin-* option has been specified?
290 bool isNoBuiltinFunc(StringRef Name) const;
291
292 /// True if any ObjC types may have non-trivial lifetime qualifiers.
293 bool allowsNonTrivialObjCLifetimeQualifiers() const {
294 return ObjCAutoRefCount || ObjCWeak;
295 }
296
297 bool assumeFunctionsAreConvergent() const {
298 return (CUDA && CUDAIsDevice) || OpenCL;
299 }
300
301 /// Return the OpenCL C or C++ version as a VersionTuple.
302 VersionTuple getOpenCLVersionTuple() const;
303};
304
305/// Floating point control options
306class FPOptions {
307public:
308 FPOptions() : fp_contract(LangOptions::FPC_Off),
309 fenv_access(LangOptions::FEA_Off) {}
310
311 // Used for serializing.
312 explicit FPOptions(unsigned I)
313 : fp_contract(static_cast<LangOptions::FPContractModeKind>(I & 3)),
314 fenv_access(static_cast<LangOptions::FEnvAccessModeKind>((I >> 2) & 1))
315 {}
316
317 explicit FPOptions(const LangOptions &LangOpts)
318 : fp_contract(LangOpts.getDefaultFPContractMode()),
319 fenv_access(LangOptions::FEA_Off) {}
320 // FIXME: Use getDefaultFEnvAccessMode() when available.
321
322 bool allowFPContractWithinStatement() const {
323 return fp_contract == LangOptions::FPC_On;
324 }
325
326 bool allowFPContractAcrossStatement() const {
327 return fp_contract == LangOptions::FPC_Fast;
328 }
329
330 void setAllowFPContractWithinStatement() {
331 fp_contract = LangOptions::FPC_On;
332 }
333
334 void setAllowFPContractAcrossStatement() {
335 fp_contract = LangOptions::FPC_Fast;
336 }
337
338 void setDisallowFPContract() { fp_contract = LangOptions::FPC_Off; }
339
340 bool allowFEnvAccess() const {
341 return fenv_access == LangOptions::FEA_On;
342 }
343
344 void setAllowFEnvAccess() {
345 fenv_access = LangOptions::FEA_On;
346 }
347
348 void setDisallowFEnvAccess() { fenv_access = LangOptions::FEA_Off; }
349
350 /// Used to serialize this.
351 unsigned getInt() const { return fp_contract | (fenv_access << 2); }
352
353private:
354 /// Adjust BinaryOperator::FPFeatures to match the total bit-field size
355 /// of these two.
356 unsigned fp_contract : 2;
357 unsigned fenv_access : 1;
358};
359
360/// Describes the kind of translation unit being processed.
361enum TranslationUnitKind {
362 /// The translation unit is a complete translation unit.
363 TU_Complete,
364
365 /// The translation unit is a prefix to a translation unit, and is
366 /// not complete.
367 TU_Prefix,
368
369 /// The translation unit is a module.
370 TU_Module
371};
372
373} // namespace clang
374
375#endif // LLVM_CLANG_BASIC_LANGOPTIONS_H
376