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

source code of clang/include/clang/Basic/LangOptions.h