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/LangStandard.h"
20 #include "clang/Basic/ObjCRuntime.h"
21 #include "clang/Basic/Sanitizers.h"
22 #include "clang/Basic/TargetCXXABI.h"
23 #include "clang/Basic/Visibility.h"
24 #include "llvm/ADT/FloatingPointMode.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/TargetParser/Triple.h"
27 #include <optional>
28 #include <string>
29 #include <vector>
30
31 namespace clang {
32
33 /// In the Microsoft ABI, this controls the placement of virtual displacement
34 /// members used to implement virtual inheritance.
35 enum class MSVtorDispMode { Never, ForVBaseOverride, ForVFTable };
36
37 /// Shader programs run in specific pipeline stages.
38 /// The order of these values matters, and must be kept in sync with the
39 /// Triple Environment enum in llvm::Triple. The ordering is enforced in
40 /// static_asserts in Triple.cpp and in clang/Basic/HLSLRuntime.h.
41 enum class ShaderStage {
42 Pixel = 0,
43 Vertex,
44 Geometry,
45 Hull,
46 Domain,
47 Compute,
48 Library,
49 RayGeneration,
50 Intersection,
51 AnyHit,
52 ClosestHit,
53 Miss,
54 Callable,
55 Mesh,
56 Amplification,
57 Invalid,
58 };
59
60 enum class PointerAuthenticationMode : unsigned {
61 None,
62 Strip,
63 SignAndStrip,
64 SignAndAuth
65 };
66
67 /// Bitfields of LangOptions, split out from LangOptions in order to ensure that
68 /// this large collection of bitfields is a trivial class type.
69 class LangOptionsBase {
70 friend class CompilerInvocation;
71 friend class CompilerInvocationBase;
72
73 public:
74 using Visibility = clang::Visibility;
75 using RoundingMode = llvm::RoundingMode;
76
77 enum GCMode { NonGC, GCOnly, HybridGC };
78 enum StackProtectorMode { SSPOff, SSPOn, SSPStrong, SSPReq };
79
80 // Automatic variables live on the stack, and when trivial they're usually
81 // uninitialized because it's undefined behavior to use them without
82 // initializing them.
83 enum class TrivialAutoVarInitKind { Uninitialized, Zero, Pattern };
84
85 enum SignedOverflowBehaviorTy {
86 // Default C standard behavior.
87 SOB_Undefined,
88
89 // -fwrapv
90 SOB_Defined,
91
92 // -ftrapv
93 SOB_Trapping
94 };
95
96 // FIXME: Unify with TUKind.
97 enum CompilingModuleKind {
98 /// Not compiling a module interface at all.
99 CMK_None,
100
101 /// Compiling a module from a module map.
102 CMK_ModuleMap,
103
104 /// Compiling a module header unit.
105 CMK_HeaderUnit,
106
107 /// Compiling a C++ modules interface unit.
108 CMK_ModuleInterface,
109 };
110
111 enum PragmaMSPointersToMembersKind {
112 PPTMK_BestCase,
113 PPTMK_FullGeneralitySingleInheritance,
114 PPTMK_FullGeneralityMultipleInheritance,
115 PPTMK_FullGeneralityVirtualInheritance
116 };
117
118 using MSVtorDispMode = clang::MSVtorDispMode;
119
120 enum DefaultCallingConvention {
121 DCC_None,
122 DCC_CDecl,
123 DCC_FastCall,
124 DCC_StdCall,
125 DCC_VectorCall,
126 DCC_RegCall,
127 DCC_RtdCall
128 };
129
130 enum AddrSpaceMapMangling { ASMM_Target, ASMM_On, ASMM_Off };
131
132 // Corresponds to _MSC_VER
133 enum MSVCMajorVersion {
134 MSVC2010 = 1600,
135 MSVC2012 = 1700,
136 MSVC2013 = 1800,
137 MSVC2015 = 1900,
138 MSVC2017 = 1910,
139 MSVC2017_5 = 1912,
140 MSVC2017_7 = 1914,
141 MSVC2019 = 1920,
142 MSVC2019_5 = 1925,
143 MSVC2019_8 = 1928,
144 MSVC2022_3 = 1933,
145 };
146
147 enum SYCLMajorVersion {
148 SYCL_None,
149 SYCL_2017,
150 SYCL_2020,
151 // The "default" SYCL version to be used when none is specified on the
152 // frontend command line.
153 SYCL_Default = SYCL_2020
154 };
155
156 enum HLSLLangStd {
157 HLSL_Unset = 0,
158 HLSL_2015 = 2015,
159 HLSL_2016 = 2016,
160 HLSL_2017 = 2017,
161 HLSL_2018 = 2018,
162 HLSL_2021 = 2021,
163 HLSL_202x = 2029,
164 };
165
166 /// Clang versions with different platform ABI conformance.
167 enum class ClangABI {
168 /// Attempt to be ABI-compatible with code generated by Clang 3.8.x
169 /// (SVN r257626). This causes <1 x long long> to be passed in an
170 /// integer register instead of an SSE register on x64_64.
171 Ver3_8,
172
173 /// Attempt to be ABI-compatible with code generated by Clang 4.0.x
174 /// (SVN r291814). This causes move operations to be ignored when
175 /// determining whether a class type can be passed or returned directly.
176 Ver4,
177
178 /// Attempt to be ABI-compatible with code generated by Clang 6.0.x
179 /// (SVN r321711). This causes determination of whether a type is
180 /// standard-layout to ignore collisions between empty base classes
181 /// and between base classes and member subobjects, which affects
182 /// whether we reuse base class tail padding in some ABIs.
183 Ver6,
184
185 /// Attempt to be ABI-compatible with code generated by Clang 7.0.x
186 /// (SVN r338536). This causes alignof (C++) and _Alignof (C11) to be
187 /// compatible with __alignof (i.e., return the preferred alignment)
188 /// rather than returning the required alignment.
189 Ver7,
190
191 /// Attempt to be ABI-compatible with code generated by Clang 9.0.x
192 /// (SVN r351319). This causes vectors of __int128 to be passed in memory
193 /// instead of passing in multiple scalar registers on x86_64 on Linux and
194 /// NetBSD.
195 Ver9,
196
197 /// Attempt to be ABI-compatible with code generated by Clang 11.0.x
198 /// (git 2e10b7a39b93). This causes clang to pass unions with a 256-bit
199 /// vector member on the stack instead of using registers, to not properly
200 /// mangle substitutions for template names in some cases, and to mangle
201 /// declaration template arguments without a cast to the parameter type
202 /// even when that can lead to mangling collisions.
203 Ver11,
204
205 /// Attempt to be ABI-compatible with code generated by Clang 12.0.x
206 /// (git 8e464dd76bef). This causes clang to mangle lambdas within
207 /// global-scope inline variables incorrectly.
208 Ver12,
209
210 /// Attempt to be ABI-compatible with code generated by Clang 14.0.x.
211 /// This causes clang to:
212 /// - mangle dependent nested names incorrectly.
213 /// - make trivial only those defaulted copy constructors with a
214 /// parameter-type-list equivalent to the parameter-type-list of an
215 /// implicit declaration.
216 Ver14,
217
218 /// Attempt to be ABI-compatible with code generated by Clang 15.0.x.
219 /// This causes clang to:
220 /// - Reverse the implementation for DR692, DR1395 and DR1432.
221 /// - pack non-POD members of packed structs.
222 /// - consider classes with defaulted special member functions non-pod.
223 Ver15,
224
225 /// Attempt to be ABI-compatible with code generated by Clang 17.0.x.
226 /// This causes clang to revert some fixes to its implementation of the
227 /// Itanium name mangling scheme, with the consequence that overloaded
228 /// function templates are mangled the same if they differ only by:
229 /// - constraints
230 /// - whether a non-type template parameter has a deduced type
231 /// - the parameter list of a template template parameter
232 Ver17,
233
234 /// Attempt to be ABI-compatible with code generated by Clang 18.0.x.
235 /// This causes clang to revert some fixes to the mangling of lambdas
236 /// in the initializers of members of local classes.
237 Ver18,
238
239 /// Conform to the underlying platform's C and C++ ABIs as closely
240 /// as we can.
241 Latest
242 };
243
244 enum class CoreFoundationABI {
245 /// No interoperability ABI has been specified
246 Unspecified,
247 /// CoreFoundation does not have any language interoperability
248 Standalone,
249 /// Interoperability with the ObjectiveC runtime
250 ObjectiveC,
251 /// Interoperability with the latest known version of the Swift runtime
252 Swift,
253 /// Interoperability with the Swift 5.0 runtime
254 Swift5_0,
255 /// Interoperability with the Swift 4.2 runtime
256 Swift4_2,
257 /// Interoperability with the Swift 4.1 runtime
258 Swift4_1,
259 };
260
261 enum FPModeKind {
262 // Disable the floating point pragma
263 FPM_Off,
264
265 // Enable the floating point pragma
266 FPM_On,
267
268 // Aggressively fuse FP ops (E.g. FMA) disregarding pragmas.
269 FPM_Fast,
270
271 // Aggressively fuse FP ops and honor pragmas.
272 FPM_FastHonorPragmas
273 };
274
275 /// Possible floating point exception behavior.
276 enum FPExceptionModeKind {
277 /// Assume that floating-point exceptions are masked.
278 FPE_Ignore,
279 /// Transformations do not cause new exceptions but may hide some.
280 FPE_MayTrap,
281 /// Strictly preserve the floating-point exception semantics.
282 FPE_Strict,
283 /// Used internally to represent initial unspecified value.
284 FPE_Default
285 };
286
287 /// Possible float expression evaluation method choices.
288 enum FPEvalMethodKind {
289 /// The evaluation method cannot be determined or is inconsistent for this
290 /// target.
291 FEM_Indeterminable = -1,
292 /// Use the declared type for fp arithmetic.
293 FEM_Source = 0,
294 /// Use the type double for fp arithmetic.
295 FEM_Double = 1,
296 /// Use extended type for fp arithmetic.
297 FEM_Extended = 2,
298 /// Used only for FE option processing; this is only used to indicate that
299 /// the user did not specify an explicit evaluation method on the command
300 /// line and so the target should be queried for its default evaluation
301 /// method instead.
302 FEM_UnsetOnCommandLine = 3
303 };
304
305 enum ExcessPrecisionKind { FPP_Standard, FPP_Fast, FPP_None };
306
307 /// Possible exception handling behavior.
308 enum class ExceptionHandlingKind { None, SjLj, WinEH, DwarfCFI, Wasm };
309
310 enum class LaxVectorConversionKind {
311 /// Permit no implicit vector bitcasts.
312 None,
313 /// Permit vector bitcasts between integer vectors with different numbers
314 /// of elements but the same total bit-width.
315 Integer,
316 /// Permit vector bitcasts between all vectors with the same total
317 /// bit-width.
318 All,
319 };
320
321 enum class AltivecSrcCompatKind {
322 // All vector compares produce scalars except vector pixel and vector bool.
323 // The types vector pixel and vector bool return vector results.
324 Mixed,
325 // All vector compares produce vector results as in GCC.
326 GCC,
327 // All vector compares produce scalars as in XL.
328 XL,
329 // Default clang behaviour.
330 Default = Mixed,
331 };
332
333 enum class SignReturnAddressScopeKind {
334 /// No signing for any function.
335 None,
336 /// Sign the return address of functions that spill LR.
337 NonLeaf,
338 /// Sign the return address of all functions,
339 All
340 };
341
342 enum class SignReturnAddressKeyKind {
343 /// Return address signing uses APIA key.
344 AKey,
345 /// Return address signing uses APIB key.
346 BKey
347 };
348
349 enum class ThreadModelKind {
350 /// POSIX Threads.
351 POSIX,
352 /// Single Threaded Environment.
353 Single
354 };
355
356 enum class ExtendArgsKind {
357 /// Integer arguments are sign or zero extended to 32/64 bits
358 /// during default argument promotions.
359 ExtendTo32,
360 ExtendTo64
361 };
362
363 enum class GPUDefaultStreamKind {
364 /// Legacy default stream
365 Legacy,
366 /// Per-thread default stream
367 PerThread,
368 };
369
370 enum class DefaultVisiblityExportMapping {
371 None,
372 /// map only explicit default visibilities to exported
373 Explicit,
374 /// map all default visibilities to exported
375 All,
376 };
377
378 enum class VisibilityForcedKinds {
379 /// Force hidden visibility
380 ForceHidden,
381 /// Force protected visibility
382 ForceProtected,
383 /// Force default visibility
384 ForceDefault,
385 /// Don't alter the visibility
386 Source,
387 };
388
389 enum class VisibilityFromDLLStorageClassKinds {
390 /// Keep the IR-gen assigned visibility.
391 Keep,
392 /// Override the IR-gen assigned visibility with default visibility.
393 Default,
394 /// Override the IR-gen assigned visibility with hidden visibility.
395 Hidden,
396 /// Override the IR-gen assigned visibility with protected visibility.
397 Protected,
398 };
399
400 enum class StrictFlexArraysLevelKind {
401 /// Any trailing array member is a FAM.
402 Default = 0,
403 /// Any trailing array member of undefined, 0, or 1 size is a FAM.
404 OneZeroOrIncomplete = 1,
405 /// Any trailing array member of undefined or 0 size is a FAM.
406 ZeroOrIncomplete = 2,
407 /// Any trailing array member of undefined size is a FAM.
408 IncompleteOnly = 3,
409 };
410
411 /// Controls the various implementations for complex multiplication and
412 // division.
413 enum ComplexRangeKind {
414 /// Implementation of complex division and multiplication using a call to
415 /// runtime library functions(generally the case, but the BE might
416 /// sometimes replace the library call if it knows enough about the
417 /// potential range of the inputs). Overflow and non-finite values are
418 /// handled by the library implementation. This is the default value.
419 CX_Full,
420
421 /// Implementation of complex division offering an improved handling
422 /// for overflow in intermediate calculations with no special handling for
423 /// NaN and infinite values.
424 CX_Improved,
425
426 /// Implementation of complex division using algebraic formulas at
427 /// higher precision. Overflow is handled. Non-finite values are handled in
428 /// some cases. If the target hardware does not have native support for a
429 /// higher precision data type, an implementation for the complex operation
430 /// will be used to provide improved guards against intermediate overflow,
431 /// but overflow and underflow may still occur in some cases. NaN and
432 /// infinite values are not handled.
433 CX_Promoted,
434
435 /// Implementation of complex division and multiplication using
436 /// algebraic formulas at source precision. No special handling to avoid
437 /// overflow. NaN and infinite values are not handled.
438 CX_Basic,
439
440 /// No range rule is enabled.
441 CX_None
442 };
443
444 // Define simple language options (with no accessors).
445 #define LANGOPT(Name, Bits, Default, Description) unsigned Name : Bits;
446 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)
447 #include "clang/Basic/LangOptions.def"
448
449 protected:
450 // Define language options of enumeration type. These are private, and will
451 // have accessors (below).
452 #define LANGOPT(Name, Bits, Default, Description)
453 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
454 LLVM_PREFERRED_TYPE(Type) \
455 unsigned Name : Bits;
456 #include "clang/Basic/LangOptions.def"
457 };
458
459 /// Keeps track of the various options that can be
460 /// enabled, which controls the dialect of C or C++ that is accepted.
461 class LangOptions : public LangOptionsBase {
462 public:
463 /// The used language standard.
464 LangStandard::Kind LangStd;
465
466 /// Set of enabled sanitizers.
467 SanitizerSet Sanitize;
468 /// Is at least one coverage instrumentation type enabled.
469 bool SanitizeCoverage = false;
470
471 /// Paths to files specifying which objects
472 /// (files, functions, variables) should not be instrumented.
473 std::vector<std::string> NoSanitizeFiles;
474
475 /// Paths to the XRay "always instrument" files specifying which
476 /// objects (files, functions, variables) should be imbued with the XRay
477 /// "always instrument" attribute.
478 /// WARNING: This is a deprecated field and will go away in the future.
479 std::vector<std::string> XRayAlwaysInstrumentFiles;
480
481 /// Paths to the XRay "never instrument" files specifying which
482 /// objects (files, functions, variables) should be imbued with the XRay
483 /// "never instrument" attribute.
484 /// WARNING: This is a deprecated field and will go away in the future.
485 std::vector<std::string> XRayNeverInstrumentFiles;
486
487 /// Paths to the XRay attribute list files, specifying which objects
488 /// (files, functions, variables) should be imbued with the appropriate XRay
489 /// attribute(s).
490 std::vector<std::string> XRayAttrListFiles;
491
492 /// Paths to special case list files specifying which entities
493 /// (files, functions) should or should not be instrumented.
494 std::vector<std::string> ProfileListFiles;
495
496 clang::ObjCRuntime ObjCRuntime;
497
498 CoreFoundationABI CFRuntime = CoreFoundationABI::Unspecified;
499
500 std::string ObjCConstantStringClass;
501
502 /// The name of the handler function to be called when -ftrapv is
503 /// specified.
504 ///
505 /// If none is specified, abort (GCC-compatible behaviour).
506 std::string OverflowHandler;
507
508 /// The module currently being compiled as specified by -fmodule-name.
509 std::string ModuleName;
510
511 /// The name of the current module, of which the main source file
512 /// is a part. If CompilingModule is set, we are compiling the interface
513 /// of this module, otherwise we are compiling an implementation file of
514 /// it. This starts as ModuleName in case -fmodule-name is provided and
515 /// changes during compilation to reflect the current module.
516 std::string CurrentModule;
517
518 /// The names of any features to enable in module 'requires' decls
519 /// in addition to the hard-coded list in Module.cpp and the target features.
520 ///
521 /// This list is sorted.
522 std::vector<std::string> ModuleFeatures;
523
524 /// Options for parsing comments.
525 CommentOptions CommentOpts;
526
527 /// A list of all -fno-builtin-* function names (e.g., memset).
528 std::vector<std::string> NoBuiltinFuncs;
529
530 /// A prefix map for __FILE__, __BASE_FILE__ and __builtin_FILE().
531 std::map<std::string, std::string, std::greater<std::string>> MacroPrefixMap;
532
533 /// Triples of the OpenMP targets that the host code codegen should
534 /// take into account in order to generate accurate offloading descriptors.
535 std::vector<llvm::Triple> OMPTargetTriples;
536
537 /// Name of the IR file that contains the result of the OpenMP target
538 /// host code generation.
539 std::string OMPHostIRFile;
540
541 /// The user provided compilation unit ID, if non-empty. This is used to
542 /// externalize static variables which is needed to support accessing static
543 /// device variables in host code for single source offloading languages
544 /// like CUDA/HIP.
545 std::string CUID;
546
547 /// C++ ABI to compile with, if specified by the frontend through -fc++-abi=.
548 /// This overrides the default ABI used by the target.
549 std::optional<TargetCXXABI::Kind> CXXABI;
550
551 /// Indicates whether the front-end is explicitly told that the
552 /// input is a header file (i.e. -x c-header).
553 bool IsHeaderFile = false;
554
555 /// The default stream kind used for HIP kernel launching.
556 GPUDefaultStreamKind GPUDefaultStream;
557
558 /// The seed used by the randomize structure layout feature.
559 std::string RandstructSeed;
560
561 /// Indicates whether to use target's platform-specific file separator when
562 /// __FILE__ macro is used and when concatenating filename with directory or
563 /// to use build environment environment's platform-specific file separator.
564 ///
565 /// The plaform-specific path separator is the backslash(\) for Windows and
566 /// forward slash (/) elsewhere.
567 bool UseTargetPathSeparator = false;
568
569 // Indicates whether we should keep all nullptr checks for pointers
570 // received as a result of a standard operator new (-fcheck-new)
571 bool CheckNew = false;
572
573 // In OpenACC mode, contains a user provided override for the _OPENACC macro.
574 // This exists so that we can override the macro value and test our incomplete
575 // implementation on real-world examples.
576 std::string OpenACCMacroOverride;
577
578 // Indicates if the wasm-opt binary must be ignored in the case of a
579 // WebAssembly target.
580 bool NoWasmOpt = false;
581
582 LangOptions();
583
584 /// Set language defaults for the given input language and
585 /// language standard in the given LangOptions object.
586 ///
587 /// \param Opts - The LangOptions object to set up.
588 /// \param Lang - The input language.
589 /// \param T - The target triple.
590 /// \param Includes - If the language requires extra headers to be implicitly
591 /// included, they will be appended to this list.
592 /// \param LangStd - The input language standard.
593 static void
594 setLangDefaults(LangOptions &Opts, Language Lang, const llvm::Triple &T,
595 std::vector<std::string> &Includes,
596 LangStandard::Kind LangStd = LangStandard::lang_unspecified);
597
598 // Define accessors/mutators for language options of enumeration type.
599 #define LANGOPT(Name, Bits, Default, Description)
600 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
601 Type get##Name() const { return static_cast<Type>(Name); } \
602 void set##Name(Type Value) { Name = static_cast<unsigned>(Value); }
603 #include "clang/Basic/LangOptions.def"
604
605 /// Are we compiling a module?
isCompilingModule()606 bool isCompilingModule() const {
607 return getCompilingModule() != CMK_None;
608 }
609
610 /// Are we compiling a module implementation?
isCompilingModuleImplementation()611 bool isCompilingModuleImplementation() const {
612 return !isCompilingModule() && !ModuleName.empty();
613 }
614
615 /// Do we need to track the owning module for a local declaration?
trackLocalOwningModule()616 bool trackLocalOwningModule() const {
617 return isCompilingModule() || ModulesLocalVisibility;
618 }
619
isSignedOverflowDefined()620 bool isSignedOverflowDefined() const {
621 return getSignedOverflowBehavior() == SOB_Defined;
622 }
623
isSubscriptPointerArithmetic()624 bool isSubscriptPointerArithmetic() const {
625 return ObjCRuntime.isSubscriptPointerArithmetic() &&
626 !ObjCSubscriptingLegacyRuntime;
627 }
628
isCompatibleWithMSVC(MSVCMajorVersion MajorVersion)629 bool isCompatibleWithMSVC(MSVCMajorVersion MajorVersion) const {
630 return MSCompatibilityVersion >= MajorVersion * 100000U;
631 }
632
633 /// Reset all of the options that are not considered when building a
634 /// module.
635 void resetNonModularOptions();
636
637 /// Is this a libc/libm function that is no longer recognized as a
638 /// builtin because a -fno-builtin-* option has been specified?
639 bool isNoBuiltinFunc(StringRef Name) const;
640
641 /// True if any ObjC types may have non-trivial lifetime qualifiers.
allowsNonTrivialObjCLifetimeQualifiers()642 bool allowsNonTrivialObjCLifetimeQualifiers() const {
643 return ObjCAutoRefCount || ObjCWeak;
644 }
645
assumeFunctionsAreConvergent()646 bool assumeFunctionsAreConvergent() const {
647 return ConvergentFunctions;
648 }
649
650 /// Return the OpenCL C or C++ version as a VersionTuple.
651 VersionTuple getOpenCLVersionTuple() const;
652
653 /// Return the OpenCL version that kernel language is compatible with
654 unsigned getOpenCLCompatibleVersion() const;
655
656 /// Return the OpenCL C or C++ for OpenCL language name and version
657 /// as a string.
658 std::string getOpenCLVersionString() const;
659
660 /// Returns true if functions without prototypes or functions with an
661 /// identifier list (aka K&R C functions) are not allowed.
requiresStrictPrototypes()662 bool requiresStrictPrototypes() const {
663 return CPlusPlus || C23 || DisableKNRFunctions;
664 }
665
666 /// Returns true if implicit function declarations are allowed in the current
667 /// language mode.
implicitFunctionsAllowed()668 bool implicitFunctionsAllowed() const {
669 return !requiresStrictPrototypes() && !OpenCL;
670 }
671
672 /// Returns true if the language supports calling the 'atexit' function.
hasAtExit()673 bool hasAtExit() const { return !(OpenMP && OpenMPIsTargetDevice); }
674
675 /// Returns true if implicit int is part of the language requirements.
isImplicitIntRequired()676 bool isImplicitIntRequired() const { return !CPlusPlus && !C99; }
677
678 /// Returns true if implicit int is supported at all.
isImplicitIntAllowed()679 bool isImplicitIntAllowed() const { return !CPlusPlus && !C23; }
680
681 /// Check if return address signing is enabled.
hasSignReturnAddress()682 bool hasSignReturnAddress() const {
683 return getSignReturnAddressScope() != SignReturnAddressScopeKind::None;
684 }
685
686 /// Check if return address signing uses AKey.
isSignReturnAddressWithAKey()687 bool isSignReturnAddressWithAKey() const {
688 return getSignReturnAddressKey() == SignReturnAddressKeyKind::AKey;
689 }
690
691 /// Check if leaf functions are also signed.
isSignReturnAddressScopeAll()692 bool isSignReturnAddressScopeAll() const {
693 return getSignReturnAddressScope() == SignReturnAddressScopeKind::All;
694 }
695
hasSjLjExceptions()696 bool hasSjLjExceptions() const {
697 return getExceptionHandling() == ExceptionHandlingKind::SjLj;
698 }
699
hasSEHExceptions()700 bool hasSEHExceptions() const {
701 return getExceptionHandling() == ExceptionHandlingKind::WinEH;
702 }
703
hasDWARFExceptions()704 bool hasDWARFExceptions() const {
705 return getExceptionHandling() == ExceptionHandlingKind::DwarfCFI;
706 }
707
hasWasmExceptions()708 bool hasWasmExceptions() const {
709 return getExceptionHandling() == ExceptionHandlingKind::Wasm;
710 }
711
isSYCL()712 bool isSYCL() const { return SYCLIsDevice || SYCLIsHost; }
713
hasDefaultVisibilityExportMapping()714 bool hasDefaultVisibilityExportMapping() const {
715 return getDefaultVisibilityExportMapping() !=
716 DefaultVisiblityExportMapping::None;
717 }
718
isExplicitDefaultVisibilityExportMapping()719 bool isExplicitDefaultVisibilityExportMapping() const {
720 return getDefaultVisibilityExportMapping() ==
721 DefaultVisiblityExportMapping::Explicit;
722 }
723
isAllDefaultVisibilityExportMapping()724 bool isAllDefaultVisibilityExportMapping() const {
725 return getDefaultVisibilityExportMapping() ==
726 DefaultVisiblityExportMapping::All;
727 }
728
hasGlobalAllocationFunctionVisibility()729 bool hasGlobalAllocationFunctionVisibility() const {
730 return getGlobalAllocationFunctionVisibility() !=
731 VisibilityForcedKinds::Source;
732 }
733
hasDefaultGlobalAllocationFunctionVisibility()734 bool hasDefaultGlobalAllocationFunctionVisibility() const {
735 return getGlobalAllocationFunctionVisibility() ==
736 VisibilityForcedKinds::ForceDefault;
737 }
738
hasProtectedGlobalAllocationFunctionVisibility()739 bool hasProtectedGlobalAllocationFunctionVisibility() const {
740 return getGlobalAllocationFunctionVisibility() ==
741 VisibilityForcedKinds::ForceProtected;
742 }
743
hasHiddenGlobalAllocationFunctionVisibility()744 bool hasHiddenGlobalAllocationFunctionVisibility() const {
745 return getGlobalAllocationFunctionVisibility() ==
746 VisibilityForcedKinds::ForceHidden;
747 }
748
749 /// Remap path prefix according to -fmacro-prefix-path option.
750 void remapPathPrefix(SmallVectorImpl<char> &Path) const;
751
getDefaultRoundingMode()752 RoundingMode getDefaultRoundingMode() const {
753 return RoundingMath ? RoundingMode::Dynamic
754 : RoundingMode::NearestTiesToEven;
755 }
756
getDefaultExceptionMode()757 FPExceptionModeKind getDefaultExceptionMode() const {
758 FPExceptionModeKind EM = getFPExceptionMode();
759 if (EM == FPExceptionModeKind::FPE_Default)
760 return FPExceptionModeKind::FPE_Ignore;
761 return EM;
762 }
763 };
764
765 /// Floating point control options
766 class FPOptionsOverride;
767 class FPOptions {
768 public:
769 // We start by defining the layout.
770 using storage_type = uint32_t;
771
772 using RoundingMode = llvm::RoundingMode;
773
774 static constexpr unsigned StorageBitSize = 8 * sizeof(storage_type);
775
776 // Define a fake option named "First" so that we have a PREVIOUS even for the
777 // real first option.
778 static constexpr storage_type FirstShift = 0, FirstWidth = 0;
779 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
780 static constexpr storage_type NAME##Shift = \
781 PREVIOUS##Shift + PREVIOUS##Width; \
782 static constexpr storage_type NAME##Width = WIDTH; \
783 static constexpr storage_type NAME##Mask = ((1 << NAME##Width) - 1) \
784 << NAME##Shift;
785 #include "clang/Basic/FPOptions.def"
786
787 static constexpr storage_type TotalWidth = 0
788 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS) +WIDTH
789 #include "clang/Basic/FPOptions.def"
790 ;
791 static_assert(TotalWidth <= StorageBitSize, "Too short type for FPOptions");
792
793 private:
794 storage_type Value;
795
796 FPOptionsOverride getChangesSlow(const FPOptions &Base) const;
797
798 public:
FPOptions()799 FPOptions() : Value(0) {
800 setFPContractMode(LangOptions::FPM_Off);
801 setConstRoundingMode(RoundingMode::Dynamic);
802 setSpecifiedExceptionMode(LangOptions::FPE_Default);
803 }
FPOptions(const LangOptions & LO)804 explicit FPOptions(const LangOptions &LO) {
805 Value = 0;
806 // The language fp contract option FPM_FastHonorPragmas has the same effect
807 // as FPM_Fast in frontend. For simplicity, use FPM_Fast uniformly in
808 // frontend.
809 auto LangOptContractMode = LO.getDefaultFPContractMode();
810 if (LangOptContractMode == LangOptions::FPM_FastHonorPragmas)
811 LangOptContractMode = LangOptions::FPM_Fast;
812 setFPContractMode(LangOptContractMode);
813 setRoundingMath(LO.RoundingMath);
814 setConstRoundingMode(LangOptions::RoundingMode::Dynamic);
815 setSpecifiedExceptionMode(LO.getFPExceptionMode());
816 setAllowFPReassociate(LO.AllowFPReassoc);
817 setNoHonorNaNs(LO.NoHonorNaNs);
818 setNoHonorInfs(LO.NoHonorInfs);
819 setNoSignedZero(LO.NoSignedZero);
820 setAllowReciprocal(LO.AllowRecip);
821 setAllowApproxFunc(LO.ApproxFunc);
822 if (getFPContractMode() == LangOptions::FPM_On &&
823 getRoundingMode() == llvm::RoundingMode::Dynamic &&
824 getExceptionMode() == LangOptions::FPE_Strict)
825 // If the FP settings are set to the "strict" model, then
826 // FENV access is set to true. (ffp-model=strict)
827 setAllowFEnvAccess(true);
828 else
829 setAllowFEnvAccess(LangOptions::FPM_Off);
830 setComplexRange(LO.getComplexRange());
831 }
832
allowFPContractWithinStatement()833 bool allowFPContractWithinStatement() const {
834 return getFPContractMode() == LangOptions::FPM_On;
835 }
setAllowFPContractWithinStatement()836 void setAllowFPContractWithinStatement() {
837 setFPContractMode(LangOptions::FPM_On);
838 }
839
allowFPContractAcrossStatement()840 bool allowFPContractAcrossStatement() const {
841 return getFPContractMode() == LangOptions::FPM_Fast;
842 }
setAllowFPContractAcrossStatement()843 void setAllowFPContractAcrossStatement() {
844 setFPContractMode(LangOptions::FPM_Fast);
845 }
846
isFPConstrained()847 bool isFPConstrained() const {
848 return getRoundingMode() != llvm::RoundingMode::NearestTiesToEven ||
849 getExceptionMode() != LangOptions::FPE_Ignore ||
850 getAllowFEnvAccess();
851 }
852
getRoundingMode()853 RoundingMode getRoundingMode() const {
854 RoundingMode RM = getConstRoundingMode();
855 if (RM == RoundingMode::Dynamic) {
856 // C23: 7.6.2p3 If the FE_DYNAMIC mode is specified and FENV_ACCESS is
857 // "off", the translator may assume that the default rounding mode is in
858 // effect.
859 if (!getAllowFEnvAccess() && !getRoundingMath())
860 RM = RoundingMode::NearestTiesToEven;
861 }
862 return RM;
863 }
864
getExceptionMode()865 LangOptions::FPExceptionModeKind getExceptionMode() const {
866 LangOptions::FPExceptionModeKind EM = getSpecifiedExceptionMode();
867 if (EM == LangOptions::FPExceptionModeKind::FPE_Default) {
868 if (getAllowFEnvAccess())
869 return LangOptions::FPExceptionModeKind::FPE_Strict;
870 else
871 return LangOptions::FPExceptionModeKind::FPE_Ignore;
872 }
873 return EM;
874 }
875
876 bool operator==(FPOptions other) const { return Value == other.Value; }
877
878 /// Return the default value of FPOptions that's used when trailing
879 /// storage isn't required.
880 static FPOptions defaultWithoutTrailingStorage(const LangOptions &LO);
881
getAsOpaqueInt()882 storage_type getAsOpaqueInt() const { return Value; }
getFromOpaqueInt(storage_type Value)883 static FPOptions getFromOpaqueInt(storage_type Value) {
884 FPOptions Opts;
885 Opts.Value = Value;
886 return Opts;
887 }
888
889 /// Return difference with the given option set.
890 FPOptionsOverride getChangesFrom(const FPOptions &Base) const;
891
892 void applyChanges(FPOptionsOverride FPO);
893
894 // We can define most of the accessors automatically:
895 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
896 TYPE get##NAME() const { \
897 return static_cast<TYPE>((Value & NAME##Mask) >> NAME##Shift); \
898 } \
899 void set##NAME(TYPE value) { \
900 Value = (Value & ~NAME##Mask) | (storage_type(value) << NAME##Shift); \
901 }
902 #include "clang/Basic/FPOptions.def"
903 LLVM_DUMP_METHOD void dump();
904 };
905
906 /// Represents difference between two FPOptions values.
907 ///
908 /// The effect of language constructs changing the set of floating point options
909 /// is usually a change of some FP properties while leaving others intact. This
910 /// class describes such changes by keeping information about what FP options
911 /// are overridden.
912 ///
913 /// The integral set of FP options, described by the class FPOptions, may be
914 /// represented as a default FP option set, defined by language standard and
915 /// command line options, with the overrides introduced by pragmas.
916 ///
917 /// The is implemented as a value of the new FPOptions plus a mask showing which
918 /// fields are actually set in it.
919 class FPOptionsOverride {
920 FPOptions Options = FPOptions::getFromOpaqueInt(0);
921 FPOptions::storage_type OverrideMask = 0;
922
923 public:
924 using RoundingMode = llvm::RoundingMode;
925
926 /// The type suitable for storing values of FPOptionsOverride. Must be twice
927 /// as wide as bit size of FPOption.
928 using storage_type = uint64_t;
929 static_assert(sizeof(storage_type) >= 2 * sizeof(FPOptions::storage_type),
930 "Too short type for FPOptionsOverride");
931
932 /// Bit mask selecting bits of OverrideMask in serialized representation of
933 /// FPOptionsOverride.
934 static constexpr storage_type OverrideMaskBits =
935 (static_cast<storage_type>(1) << FPOptions::StorageBitSize) - 1;
936
FPOptionsOverride()937 FPOptionsOverride() {}
FPOptionsOverride(const LangOptions & LO)938 FPOptionsOverride(const LangOptions &LO)
939 : Options(LO), OverrideMask(OverrideMaskBits) {}
FPOptionsOverride(FPOptions FPO)940 FPOptionsOverride(FPOptions FPO)
941 : Options(FPO), OverrideMask(OverrideMaskBits) {}
FPOptionsOverride(FPOptions FPO,FPOptions::storage_type Mask)942 FPOptionsOverride(FPOptions FPO, FPOptions::storage_type Mask)
943 : Options(FPO), OverrideMask(Mask) {}
944
requiresTrailingStorage()945 bool requiresTrailingStorage() const { return OverrideMask != 0; }
946
setAllowFPContractWithinStatement()947 void setAllowFPContractWithinStatement() {
948 setFPContractModeOverride(LangOptions::FPM_On);
949 }
950
setAllowFPContractAcrossStatement()951 void setAllowFPContractAcrossStatement() {
952 setFPContractModeOverride(LangOptions::FPM_Fast);
953 }
954
setDisallowFPContract()955 void setDisallowFPContract() {
956 setFPContractModeOverride(LangOptions::FPM_Off);
957 }
958
setFPPreciseEnabled(bool Value)959 void setFPPreciseEnabled(bool Value) {
960 setAllowFPReassociateOverride(!Value);
961 setNoHonorNaNsOverride(!Value);
962 setNoHonorInfsOverride(!Value);
963 setNoSignedZeroOverride(!Value);
964 setAllowReciprocalOverride(!Value);
965 setAllowApproxFuncOverride(!Value);
966 setMathErrnoOverride(Value);
967 if (Value)
968 /* Precise mode implies fp_contract=on and disables ffast-math */
969 setAllowFPContractWithinStatement();
970 else
971 /* Precise mode disabled sets fp_contract=fast and enables ffast-math */
972 setAllowFPContractAcrossStatement();
973 }
974
setDisallowOptimizations()975 void setDisallowOptimizations() { setFPPreciseEnabled(true); }
976
getAsOpaqueInt()977 storage_type getAsOpaqueInt() const {
978 return (static_cast<storage_type>(Options.getAsOpaqueInt())
979 << FPOptions::StorageBitSize) |
980 OverrideMask;
981 }
getFromOpaqueInt(storage_type I)982 static FPOptionsOverride getFromOpaqueInt(storage_type I) {
983 FPOptionsOverride Opts;
984 Opts.OverrideMask = I & OverrideMaskBits;
985 Opts.Options = FPOptions::getFromOpaqueInt(I >> FPOptions::StorageBitSize);
986 return Opts;
987 }
988
applyOverrides(FPOptions Base)989 FPOptions applyOverrides(FPOptions Base) {
990 FPOptions Result =
991 FPOptions::getFromOpaqueInt((Base.getAsOpaqueInt() & ~OverrideMask) |
992 (Options.getAsOpaqueInt() & OverrideMask));
993 return Result;
994 }
995
applyOverrides(const LangOptions & LO)996 FPOptions applyOverrides(const LangOptions &LO) {
997 return applyOverrides(FPOptions(LO));
998 }
999
1000 bool operator==(FPOptionsOverride other) const {
1001 return Options == other.Options && OverrideMask == other.OverrideMask;
1002 }
1003 bool operator!=(FPOptionsOverride other) const { return !(*this == other); }
1004
1005 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
1006 bool has##NAME##Override() const { \
1007 return OverrideMask & FPOptions::NAME##Mask; \
1008 } \
1009 TYPE get##NAME##Override() const { \
1010 assert(has##NAME##Override()); \
1011 return Options.get##NAME(); \
1012 } \
1013 void clear##NAME##Override() { \
1014 /* Clear the actual value so that we don't have spurious differences when \
1015 * testing equality. */ \
1016 Options.set##NAME(TYPE(0)); \
1017 OverrideMask &= ~FPOptions::NAME##Mask; \
1018 } \
1019 void set##NAME##Override(TYPE value) { \
1020 Options.set##NAME(value); \
1021 OverrideMask |= FPOptions::NAME##Mask; \
1022 }
1023 #include "clang/Basic/FPOptions.def"
1024 LLVM_DUMP_METHOD void dump();
1025 };
1026
getChangesFrom(const FPOptions & Base)1027 inline FPOptionsOverride FPOptions::getChangesFrom(const FPOptions &Base) const {
1028 if (Value == Base.Value)
1029 return FPOptionsOverride();
1030 return getChangesSlow(Base);
1031 }
1032
applyChanges(FPOptionsOverride FPO)1033 inline void FPOptions::applyChanges(FPOptionsOverride FPO) {
1034 *this = FPO.applyOverrides(*this);
1035 }
1036
1037 /// Describes the kind of translation unit being processed.
1038 enum TranslationUnitKind {
1039 /// The translation unit is a complete translation unit.
1040 TU_Complete,
1041
1042 /// The translation unit is a prefix to a translation unit, and is
1043 /// not complete.
1044 TU_Prefix,
1045
1046 /// The translation unit is a clang module.
1047 TU_ClangModule,
1048
1049 /// The translation unit is a is a complete translation unit that we might
1050 /// incrementally extend later.
1051 TU_Incremental
1052 };
1053
1054 } // namespace clang
1055
1056 #endif // LLVM_CLANG_BASIC_LANGOPTIONS_H
1057