xref: /freebsd/contrib/llvm-project/libcxx/include/__config (revision 7351d001fc7f5a77a18a102e12a3ca2cbfd6988c)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___CONFIG
11#define _LIBCPP___CONFIG
12
13#include <__config_site>
14#include <__configuration/abi.h>
15#include <__configuration/availability.h>
16#include <__configuration/compiler.h>
17#include <__configuration/language.h>
18#include <__configuration/platform.h>
19
20#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
21#  pragma GCC system_header
22#endif
23
24#ifdef __cplusplus
25
26// The attributes supported by clang are documented at https://clang.llvm.org/docs/AttributeReference.html
27
28// _LIBCPP_VERSION represents the version of libc++, which matches the version of LLVM.
29// Given a LLVM release LLVM XX.YY.ZZ (e.g. LLVM 17.0.1 == 17.00.01), _LIBCPP_VERSION is
30// defined to XXYYZZ.
31#  define _LIBCPP_VERSION 210108
32
33#  define _LIBCPP_CONCAT_IMPL(_X, _Y) _X##_Y
34#  define _LIBCPP_CONCAT(_X, _Y) _LIBCPP_CONCAT_IMPL(_X, _Y)
35#  define _LIBCPP_CONCAT3(X, Y, Z) _LIBCPP_CONCAT(X, _LIBCPP_CONCAT(Y, Z))
36
37#  if __STDC_HOSTED__ == 0
38#    define _LIBCPP_FREESTANDING
39#  endif
40
41// NOLINTNEXTLINE(libcpp-cpp-version-check)
42#  if __cplusplus < 201103L
43#    define _LIBCPP_CXX03_LANG
44#  endif
45
46#  if __has_feature(experimental_library)
47#    ifndef _LIBCPP_ENABLE_EXPERIMENTAL
48#      define _LIBCPP_ENABLE_EXPERIMENTAL
49#    endif
50#  endif
51
52// Incomplete features get their own specific disabling flags. This makes it
53// easier to grep for target specific flags once the feature is complete.
54#  if defined(_LIBCPP_ENABLE_EXPERIMENTAL) || defined(_LIBCPP_BUILDING_LIBRARY)
55#    define _LIBCPP_HAS_EXPERIMENTAL_LIBRARY 1
56#  else
57#    define _LIBCPP_HAS_EXPERIMENTAL_LIBRARY 0
58#  endif
59
60#  define _LIBCPP_HAS_EXPERIMENTAL_PSTL _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
61#  define _LIBCPP_HAS_EXPERIMENTAL_TZDB _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
62#  define _LIBCPP_HAS_EXPERIMENTAL_SYNCSTREAM _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
63#  define _LIBCPP_HAS_EXPERIMENTAL_HARDENING_OBSERVE_SEMANTIC _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
64
65// HARDENING {
66
67// TODO(LLVM 23): Remove this. We're making these an error to catch folks who might not have migrated.
68//       Since hardening went through several changes (many of which impacted user-facing macros),
69//       we're keeping these checks around for a bit longer than usual. Failure to properly configure
70//       hardening results in checks being dropped silently, which is a pretty big deal.
71#  if defined(_LIBCPP_ENABLE_ASSERTIONS)
72#    error "_LIBCPP_ENABLE_ASSERTIONS has been removed, please use _LIBCPP_HARDENING_MODE=<mode> instead (see docs)"
73#  endif
74#  if defined(_LIBCPP_ENABLE_HARDENED_MODE)
75#    error "_LIBCPP_ENABLE_HARDENED_MODE has been removed, please use _LIBCPP_HARDENING_MODE=<mode> instead (see docs)"
76#  endif
77#  if defined(_LIBCPP_ENABLE_SAFE_MODE)
78#    error "_LIBCPP_ENABLE_SAFE_MODE has been removed, please use _LIBCPP_HARDENING_MODE=<mode> instead (see docs)"
79#  endif
80#  if defined(_LIBCPP_ENABLE_DEBUG_MODE)
81#    error "_LIBCPP_ENABLE_DEBUG_MODE has been removed, please use _LIBCPP_HARDENING_MODE=<mode> instead (see docs)"
82#  endif
83
84// The library provides the macro `_LIBCPP_HARDENING_MODE` which can be set to one of the following values:
85//
86// - `_LIBCPP_HARDENING_MODE_NONE`;
87// - `_LIBCPP_HARDENING_MODE_FAST`;
88// - `_LIBCPP_HARDENING_MODE_EXTENSIVE`;
89// - `_LIBCPP_HARDENING_MODE_DEBUG`.
90//
91// These values have the following effects:
92//
93// - `_LIBCPP_HARDENING_MODE_NONE` -- sets the hardening mode to "none" which disables all runtime hardening checks;
94//
95// - `_LIBCPP_HARDENING_MODE_FAST` -- sets that hardening mode to "fast". The fast mode enables security-critical checks
96//   that can be done with relatively little runtime overhead in constant time;
97//
98// - `_LIBCPP_HARDENING_MODE_EXTENSIVE` -- sets the hardening mode to "extensive". The extensive mode is a superset of
99//   the fast mode that additionally enables checks that are relatively cheap and prevent common types of logic errors
100//   but are not necessarily security-critical;
101//
102// - `_LIBCPP_HARDENING_MODE_DEBUG` -- sets the hardening mode to "debug". The debug mode is a superset of the extensive
103//   mode and enables all checks available in the library, including internal assertions. Checks that are part of the
104//   debug mode can be very expensive and thus the debug mode is intended to be used for testing, not in production.
105
106// Inside the library, assertions are categorized so they can be cherry-picked based on the chosen hardening mode. These
107// macros are only for internal use -- users should only pick one of the high-level hardening modes described above.
108//
109// - `_LIBCPP_ASSERT_VALID_INPUT_RANGE` -- checks that ranges (whether expressed as an iterator pair, an iterator and
110//   a sentinel, an iterator and a count, or a `std::range`) given as input to library functions are valid:
111//   - the sentinel is reachable from the begin iterator;
112//   - TODO(hardening): both iterators refer to the same container.
113//
114// - `_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS` -- checks that any attempts to access a container element, whether through
115//   the container object or through an iterator, are valid and do not attempt to go out of bounds or otherwise access
116//   a non-existent element. For iterator checks to work, bounded iterators must be enabled in the ABI. Types like
117//   `optional` and `function` are considered one-element containers for the purposes of this check.
118//
119// - `_LIBCPP_ASSERT_NON_NULL` -- checks that the pointer being dereferenced is not null. On most modern platforms zero
120//   address does not refer to an actual location in memory, so a null pointer dereference would not compromize the
121//   memory security of a program (however, it is still undefined behavior that can result in strange errors due to
122//   compiler optimizations).
123//
124// - `_LIBCPP_ASSERT_NON_OVERLAPPING_RANGES` -- for functions that take several ranges as arguments, checks that the
125//   given ranges do not overlap.
126//
127// - `_LIBCPP_ASSERT_VALID_DEALLOCATION` -- checks that an attempt to deallocate memory is valid (e.g. the given object
128//   was allocated by the given allocator). Violating this category typically results in a memory leak.
129//
130// - `_LIBCPP_ASSERT_VALID_EXTERNAL_API_CALL` -- checks that a call to an external API doesn't fail in
131//   an unexpected manner. This includes triggering documented cases of undefined behavior in an external library (like
132//   attempting to unlock an unlocked mutex in pthreads). Any API external to the library falls under this category
133//   (from system calls to compiler intrinsics). We generally don't expect these failures to compromize memory safety or
134//   otherwise create an immediate security issue.
135//
136// - `_LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR` -- checks any operations that exchange nodes between containers to make sure
137//   the containers have compatible allocators.
138//
139// - `_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN` -- checks that the given argument is within the domain of valid arguments
140//   for the function. Violating this typically produces an incorrect result (e.g. the clamp algorithm returns the
141//   original value without clamping it due to incorrect functors) or puts an object into an invalid state (e.g.
142//   a string view where only a subset of elements is possible to access). This category is for assertions violating
143//   which doesn't cause any immediate issues in the library -- whatever the consequences are, they will happen in the
144//   user code.
145//
146// - `_LIBCPP_ASSERT_PEDANTIC` -- checks prerequisites which are imposed by the Standard, but violating which happens to
147//   be benign in our implementation.
148//
149// - `_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT` -- checks that the given argument satisfies the semantic requirements imposed
150//   by the Standard. Typically, there is no simple way to completely prove that a semantic requirement is satisfied;
151//   thus, this would often be a heuristic check and it might be quite expensive.
152//
153// - `_LIBCPP_ASSERT_INTERNAL` -- checks that internal invariants of the library hold. These assertions don't depend on
154//   user input.
155//
156// - `_LIBCPP_ASSERT_UNCATEGORIZED` -- for assertions that haven't been properly classified yet.
157
158// clang-format off
159#  define _LIBCPP_HARDENING_MODE_NONE      (1 << 1)
160#  define _LIBCPP_HARDENING_MODE_FAST      (1 << 2)
161#  define _LIBCPP_HARDENING_MODE_EXTENSIVE (1 << 4) // Deliberately not ordered.
162#  define _LIBCPP_HARDENING_MODE_DEBUG     (1 << 3)
163// clang-format on
164
165#  ifndef _LIBCPP_HARDENING_MODE
166
167#    ifndef _LIBCPP_HARDENING_MODE_DEFAULT
168#      error _LIBCPP_HARDENING_MODE_DEFAULT is not defined. This definition should be set at configuration time in the \
169`__config_site` header, please make sure your installation of libc++ is not broken.
170#    endif
171
172#    define _LIBCPP_HARDENING_MODE _LIBCPP_HARDENING_MODE_DEFAULT
173#  endif
174
175#  if _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_NONE &&                                                         \
176      _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_FAST &&                                                         \
177      _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_EXTENSIVE &&                                                    \
178      _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
179#    error _LIBCPP_HARDENING_MODE must be set to one of the following values: \
180_LIBCPP_HARDENING_MODE_NONE, \
181_LIBCPP_HARDENING_MODE_FAST, \
182_LIBCPP_HARDENING_MODE_EXTENSIVE, \
183_LIBCPP_HARDENING_MODE_DEBUG
184#  endif
185
186// Hardening assertion semantics generally mirror the evaluation semantics of C++26 Contracts:
187// - `ignore` evaluates the assertion but doesn't do anything if it fails (note that it differs from the Contracts
188//   `ignore` semantic which wouldn't evaluate the assertion at all);
189// - `observe` logs an error (indicating, if possible, that the error is fatal) and continues execution;
190// - `quick-enforce` terminates the program as fast as possible (via trapping);
191// - `enforce` logs an error and then terminates the program.
192//
193// Notes:
194// - Continuing execution after a hardening check fails results in undefined behavior; the `observe` semantic is meant
195//   to make adopting hardening easier but should not be used outside of this scenario;
196// - C++26 wording for Library Hardening precludes a conforming Hardened implementation from using the Contracts
197//   `ignore` semantic when evaluating hardened preconditions in the Library. Libc++ allows using this semantic for
198//   hardened preconditions, however, be aware that using `ignore` does not produce a conforming "Hardened"
199//   implementation, unlike the other semantics above.
200// clang-format off
201#  define _LIBCPP_ASSERTION_SEMANTIC_IGNORE        (1 << 1)
202#  define _LIBCPP_ASSERTION_SEMANTIC_OBSERVE       (1 << 2)
203#  define _LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE (1 << 3)
204#  define _LIBCPP_ASSERTION_SEMANTIC_ENFORCE       (1 << 4)
205// clang-format on
206
207// Allow users to define an arbitrary assertion semantic; otherwise, use the default mapping from modes to semantics.
208// The default is for production-capable modes to use `quick-enforce` (i.e., trap) and for the `debug` mode to use
209// `enforce` (i.e., log and abort).
210#  ifndef _LIBCPP_ASSERTION_SEMANTIC
211
212#    if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
213#      define _LIBCPP_ASSERTION_SEMANTIC _LIBCPP_ASSERTION_SEMANTIC_ENFORCE
214#    else
215#      define _LIBCPP_ASSERTION_SEMANTIC _LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE
216#    endif
217
218#  else
219#    if !_LIBCPP_HAS_EXPERIMENTAL_LIBRARY
220#      error "Assertion semantics are an experimental feature."
221#    endif
222#    if defined(_LIBCPP_CXX03_LANG)
223#      error "Assertion semantics are not available in the C++03 mode."
224#    endif
225
226#  endif // _LIBCPP_ASSERTION_SEMANTIC
227
228// } HARDENING
229
230#  define _LIBCPP_TOSTRING2(x) #x
231#  define _LIBCPP_TOSTRING(x) _LIBCPP_TOSTRING2(x)
232
233#  ifndef __has_constexpr_builtin
234#    define __has_constexpr_builtin(x) 0
235#  endif
236
237// This checks wheter a Clang module is built
238#  ifndef __building_module
239#    define __building_module(...) 0
240#  endif
241
242// '__is_identifier' returns '0' if '__x' is a reserved identifier provided by
243// the compiler and '1' otherwise.
244#  ifndef __is_identifier
245#    define __is_identifier(__x) 1
246#  endif
247
248#  ifndef __has_declspec_attribute
249#    define __has_declspec_attribute(__x) 0
250#  endif
251
252#  define __has_keyword(__x) !(__is_identifier(__x))
253
254#  ifndef __has_warning
255#    define __has_warning(...) 0
256#  endif
257
258#  if !defined(_LIBCPP_COMPILER_CLANG_BASED) && __cplusplus < 201103L
259#    error "libc++ only supports C++03 with Clang-based compilers. Please enable C++11"
260#  endif
261
262#  if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_NO_VCRUNTIME)
263#    define _LIBCPP_ABI_VCRUNTIME
264#  endif
265
266#  if defined(__MVS__)
267#    include <features.h> // for __NATIVE_ASCII_F
268#  endif
269
270#  if defined(_WIN32)
271#    define _LIBCPP_WIN32API
272#    define _LIBCPP_SHORT_WCHAR 1
273// Both MinGW and native MSVC provide a "MSVC"-like environment
274#    define _LIBCPP_MSVCRT_LIKE
275// If mingw not explicitly detected, assume using MS C runtime only if
276// a MS compatibility version is specified.
277#    if defined(_MSC_VER) && !defined(__MINGW32__)
278#      define _LIBCPP_MSVCRT // Using Microsoft's C Runtime library
279#    endif
280#    if (defined(_M_AMD64) || defined(__x86_64__)) || (defined(_M_ARM) || defined(__arm__))
281#      define _LIBCPP_HAS_BITSCAN64 1
282#    else
283#      define _LIBCPP_HAS_BITSCAN64 0
284#    endif
285#    define _LIBCPP_HAS_OPEN_WITH_WCHAR 1
286#  else
287#    define _LIBCPP_HAS_OPEN_WITH_WCHAR 0
288#    define _LIBCPP_HAS_BITSCAN64 0
289#  endif // defined(_WIN32)
290
291#  if defined(_AIX) && !defined(__64BIT__)
292// The size of wchar is 2 byte on 32-bit mode on AIX.
293#    define _LIBCPP_SHORT_WCHAR 1
294#  endif
295
296// Libc++ supports various implementations of std::random_device.
297//
298// _LIBCPP_USING_DEV_RANDOM
299//      Read entropy from the given file, by default `/dev/urandom`.
300//      If a token is provided, it is assumed to be the path to a file
301//      to read entropy from. This is the default behavior if nothing
302//      else is specified. This implementation requires storing state
303//      inside `std::random_device`.
304//
305// _LIBCPP_USING_ARC4_RANDOM
306//      Use arc4random(). This allows obtaining random data even when
307//      using sandboxing mechanisms. On some platforms like Apple, this
308//      is the recommended source of entropy for user-space programs.
309//      When this option is used, the token passed to `std::random_device`'s
310//      constructor *must* be "/dev/urandom" -- anything else is an error.
311//
312// _LIBCPP_USING_GETENTROPY
313//      Use getentropy().
314//      When this option is used, the token passed to `std::random_device`'s
315//      constructor *must* be "/dev/urandom" -- anything else is an error.
316//
317// _LIBCPP_USING_FUCHSIA_CPRNG
318//      Use Fuchsia's zx_cprng_draw() system call, which is specified to
319//      deliver high-quality entropy and cannot fail.
320//      When this option is used, the token passed to `std::random_device`'s
321//      constructor *must* be "/dev/urandom" -- anything else is an error.
322//
323// _LIBCPP_USING_NACL_RANDOM
324//      NaCl's sandbox (which PNaCl also runs in) doesn't allow filesystem access,
325//      including accesses to the special files under `/dev`. This implementation
326//      uses the NaCL syscall `nacl_secure_random_init()` to get entropy.
327//      When this option is used, the token passed to `std::random_device`'s
328//      constructor *must* be "/dev/urandom" -- anything else is an error.
329//
330// _LIBCPP_USING_WIN32_RANDOM
331//      Use rand_s(), for use on Windows.
332//      When this option is used, the token passed to `std::random_device`'s
333//      constructor *must* be "/dev/urandom" -- anything else is an error.
334#  if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) ||                     \
335      defined(__DragonFly__)
336#    define _LIBCPP_USING_ARC4_RANDOM
337#  elif defined(__wasi__) || defined(__EMSCRIPTEN__)
338#    define _LIBCPP_USING_GETENTROPY
339#  elif defined(__Fuchsia__)
340#    define _LIBCPP_USING_FUCHSIA_CPRNG
341#  elif defined(__native_client__)
342#    define _LIBCPP_USING_NACL_RANDOM
343#  elif defined(_LIBCPP_WIN32API)
344#    define _LIBCPP_USING_WIN32_RANDOM
345#  else
346#    define _LIBCPP_USING_DEV_RANDOM
347#  endif
348
349#  ifndef _LIBCPP_CXX03_LANG
350
351#    define _LIBCPP_ALIGNOF(_Tp) alignof(_Tp)
352#    define _ALIGNAS_TYPE(x) alignas(x)
353#    define _ALIGNAS(x) alignas(x)
354#    define _NOEXCEPT noexcept
355#    define _NOEXCEPT_(...) noexcept(__VA_ARGS__)
356#    define _LIBCPP_CONSTEXPR constexpr
357
358#  else
359
360#    define _LIBCPP_ALIGNOF(_Tp) _Alignof(_Tp)
361#    define _ALIGNAS_TYPE(x) __attribute__((__aligned__(_LIBCPP_ALIGNOF(x))))
362#    define _ALIGNAS(x) __attribute__((__aligned__(x)))
363#    define nullptr __nullptr
364#    define _NOEXCEPT throw()
365#    define _NOEXCEPT_(...)
366#    define static_assert(...) _Static_assert(__VA_ARGS__)
367#    define decltype(...) __decltype(__VA_ARGS__)
368#    define _LIBCPP_CONSTEXPR
369
370typedef __char16_t char16_t;
371typedef __char32_t char32_t;
372
373#  endif
374
375#  define _LIBCPP_PREFERRED_ALIGNOF(_Tp) __alignof(_Tp)
376
377#  if __has_extension(blocks) && defined(__APPLE__)
378#    define _LIBCPP_HAS_BLOCKS_RUNTIME 1
379#  else
380#    define _LIBCPP_HAS_BLOCKS_RUNTIME 0
381#  endif
382
383#  define _LIBCPP_ALWAYS_INLINE __attribute__((__always_inline__))
384
385#  if defined(_LIBCPP_OBJECT_FORMAT_COFF)
386
387#    ifdef _DLL
388#      define _LIBCPP_CRT_FUNC __declspec(dllimport)
389#    else
390#      define _LIBCPP_CRT_FUNC
391#    endif
392
393#    if defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS) || (defined(__MINGW32__) && !defined(_LIBCPP_BUILDING_LIBRARY))
394#      define _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS
395#      define _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS
396#      define _LIBCPP_OVERRIDABLE_FUNC_VIS
397#      define _LIBCPP_EXPORTED_FROM_ABI
398#    elif defined(_LIBCPP_BUILDING_LIBRARY)
399#      if defined(__MINGW32__)
400#        define _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __declspec(dllexport)
401#        define _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS
402#      else
403#        define _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS
404#        define _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __declspec(dllexport)
405#      endif
406#      define _LIBCPP_OVERRIDABLE_FUNC_VIS __declspec(dllexport)
407#      define _LIBCPP_EXPORTED_FROM_ABI __declspec(dllexport)
408#    else
409#      define _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __declspec(dllimport)
410#      define _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS
411#      define _LIBCPP_OVERRIDABLE_FUNC_VIS
412#      define _LIBCPP_EXPORTED_FROM_ABI __declspec(dllimport)
413#    endif
414
415#    define _LIBCPP_HIDDEN
416#    define _LIBCPP_TEMPLATE_DATA_VIS
417#    define _LIBCPP_NAMESPACE_VISIBILITY
418
419#  else
420
421#    if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
422#      define _LIBCPP_VISIBILITY(vis) __attribute__((__visibility__(vis)))
423#    else
424#      define _LIBCPP_VISIBILITY(vis)
425#    endif
426
427#    define _LIBCPP_HIDDEN _LIBCPP_VISIBILITY("hidden")
428#    define _LIBCPP_TEMPLATE_DATA_VIS _LIBCPP_VISIBILITY("default")
429#    define _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_VISIBILITY("default")
430#    define _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS _LIBCPP_VISIBILITY("default")
431#    define _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS
432
433// TODO: Make this a proper customization point or remove the option to override it.
434#    ifndef _LIBCPP_OVERRIDABLE_FUNC_VIS
435#      define _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_VISIBILITY("default")
436#    endif
437
438#    if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS) && __has_attribute(__type_visibility__)
439#      define _LIBCPP_NAMESPACE_VISIBILITY __attribute__((__type_visibility__("default")))
440#    elif !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
441#      define _LIBCPP_NAMESPACE_VISIBILITY __attribute__((__visibility__("default")))
442#    else
443#      define _LIBCPP_NAMESPACE_VISIBILITY
444#    endif
445
446#  endif // defined(_LIBCPP_OBJECT_FORMAT_COFF)
447
448#  if __has_attribute(exclude_from_explicit_instantiation)
449#    define _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION __attribute__((__exclude_from_explicit_instantiation__))
450#  else
451// Try to approximate the effect of exclude_from_explicit_instantiation
452// (which is that entities are not assumed to be provided by explicit
453// template instantiations in the dylib) by always inlining those entities.
454#    define _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION _LIBCPP_ALWAYS_INLINE
455#  endif
456
457#  ifdef _LIBCPP_COMPILER_CLANG_BASED
458#    define _LIBCPP_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
459#    define _LIBCPP_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
460#    define _LIBCPP_CLANG_DIAGNOSTIC_IGNORED(str) _Pragma(_LIBCPP_TOSTRING(clang diagnostic ignored str))
461#    define _LIBCPP_GCC_DIAGNOSTIC_IGNORED(str)
462#  elif defined(_LIBCPP_COMPILER_GCC)
463#    define _LIBCPP_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
464#    define _LIBCPP_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
465#    define _LIBCPP_CLANG_DIAGNOSTIC_IGNORED(str)
466#    define _LIBCPP_GCC_DIAGNOSTIC_IGNORED(str) _Pragma(_LIBCPP_TOSTRING(GCC diagnostic ignored str))
467#  else
468#    define _LIBCPP_DIAGNOSTIC_PUSH
469#    define _LIBCPP_DIAGNOSTIC_POP
470#    define _LIBCPP_CLANG_DIAGNOSTIC_IGNORED(str)
471#    define _LIBCPP_GCC_DIAGNOSTIC_IGNORED(str)
472#  endif
473
474#  if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_FAST
475#    define _LIBCPP_HARDENING_SIG f
476#  elif _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_EXTENSIVE
477#    define _LIBCPP_HARDENING_SIG s
478#  elif _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
479#    define _LIBCPP_HARDENING_SIG d
480#  else
481#    define _LIBCPP_HARDENING_SIG n // "none"
482#  endif
483
484#  if !_LIBCPP_HAS_EXCEPTIONS
485#    define _LIBCPP_EXCEPTIONS_SIG n
486#  else
487#    define _LIBCPP_EXCEPTIONS_SIG e
488#  endif
489
490#  define _LIBCPP_ODR_SIGNATURE                                                                                        \
491    _LIBCPP_CONCAT(_LIBCPP_CONCAT(_LIBCPP_HARDENING_SIG, _LIBCPP_EXCEPTIONS_SIG), _LIBCPP_VERSION)
492
493// This macro marks a symbol as being hidden from libc++'s ABI. This is achieved
494// on two levels:
495// 1. The symbol is given hidden visibility, which ensures that users won't start exporting
496//    symbols from their dynamic library by means of using the libc++ headers. This ensures
497//    that those symbols stay private to the dynamic library in which it is defined.
498//
499// 2. The symbol is given an ABI tag that encodes the ODR-relevant properties of the library.
500//    This ensures that no ODR violation can arise from mixing two TUs compiled with different
501//    versions or configurations of libc++ (such as exceptions vs no-exceptions). Indeed, if the
502//    program contains two definitions of a function, the ODR requires them to be token-by-token
503//    equivalent, and the linker is allowed to pick either definition and discard the other one.
504//
505//    For example, if a program contains a copy of `vector::at()` compiled with exceptions enabled
506//    *and* a copy of `vector::at()` compiled with exceptions disabled (by means of having two TUs
507//    compiled with different settings), the two definitions are both visible by the linker and they
508//    have the same name, but they have a meaningfully different implementation (one throws an exception
509//    and the other aborts the program). This violates the ODR and makes the program ill-formed, and in
510//    practice what will happen is that the linker will pick one of the definitions at random and will
511//    discard the other one. This can quite clearly lead to incorrect program behavior.
512//
513//    A similar reasoning holds for many other properties that are ODR-affecting. Essentially any
514//    property that causes the code of a function to differ from the code in another configuration
515//    can be considered ODR-affecting. In practice, we don't encode all such properties in the ABI
516//    tag, but we encode the ones that we think are most important: library version, exceptions, and
517//    hardening mode.
518//
519//    Note that historically, solving this problem has been achieved in various ways, including
520//    force-inlining all functions or giving internal linkage to all functions. Both these previous
521//    solutions suffer from drawbacks that lead notably to code bloat.
522//
523// Note that we use _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION to ensure that we don't depend
524// on _LIBCPP_HIDE_FROM_ABI methods of classes explicitly instantiated in the dynamic library.
525//
526// Also note that the _LIBCPP_HIDE_FROM_ABI_VIRTUAL macro should be used on virtual functions
527// instead of _LIBCPP_HIDE_FROM_ABI. That macro does not use an ABI tag. Indeed, the mangled
528// name of a virtual function is part of its ABI, since some architectures like arm64e can sign
529// the virtual function pointer in the vtable based on the mangled name of the function. Since
530// we use an ABI tag that changes with each released version, the mangled name of the virtual
531// function would change, which is incorrect. Note that it doesn't make much sense to change
532// the implementation of a virtual function in an ABI-incompatible way in the first place,
533// since that would be an ABI break anyway. Hence, the lack of ABI tag should not be noticeable.
534//
535// The macro can be applied to record and enum types. When the tagged type is nested in
536// a record this "parent" record needs to have the macro too. Another use case for applying
537// this macro to records and unions is to apply an ABI tag to inline constexpr variables.
538// This can be useful for inline variables that are implementation details which are expected
539// to change in the future.
540//
541// TODO: We provide a escape hatch with _LIBCPP_NO_ABI_TAG for folks who want to avoid increasing
542//       the length of symbols with an ABI tag. In practice, we should remove the escape hatch and
543//       use compression mangling instead, see https://github.com/itanium-cxx-abi/cxx-abi/issues/70.
544#  ifndef _LIBCPP_NO_ABI_TAG
545#    define _LIBCPP_HIDE_FROM_ABI                                                                                      \
546      _LIBCPP_HIDDEN _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION                                                       \
547      __attribute__((__abi_tag__(_LIBCPP_TOSTRING(_LIBCPP_ODR_SIGNATURE))))
548#  else
549#    define _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDDEN _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION
550#  endif
551#  define _LIBCPP_HIDE_FROM_ABI_VIRTUAL _LIBCPP_HIDDEN _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION
552
553#  ifdef _LIBCPP_BUILDING_LIBRARY
554#    if _LIBCPP_ABI_VERSION > 1
555#      define _LIBCPP_HIDE_FROM_ABI_AFTER_V1 _LIBCPP_HIDE_FROM_ABI
556#    else
557#      define _LIBCPP_HIDE_FROM_ABI_AFTER_V1
558#    endif
559#  else
560#    define _LIBCPP_HIDE_FROM_ABI_AFTER_V1 _LIBCPP_HIDE_FROM_ABI
561#  endif
562
563// Clang modules take a significant compile time hit when pushing and popping diagnostics.
564// Since all the headers are marked as system headers unless _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER is defined, we can
565// simply disable this pushing and popping when _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER isn't defined.
566#  ifdef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
567#    define _LIBCPP_PUSH_EXTENSION_DIAGNOSTICS                                                                         \
568      _LIBCPP_DIAGNOSTIC_PUSH                                                                                          \
569      _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wc++11-extensions")                                                           \
570      _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wc++14-extensions")                                                           \
571      _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wc++17-extensions")                                                           \
572      _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wc++20-extensions")                                                           \
573      _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wc++23-extensions")                                                           \
574      _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wc++14-extensions")                                                             \
575      _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wc++17-extensions")                                                             \
576      _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wc++20-extensions")                                                             \
577      _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wc++23-extensions")
578#    define _LIBCPP_POP_EXTENSION_DIAGNOSTICS _LIBCPP_DIAGNOSTIC_POP
579#  else
580#    define _LIBCPP_PUSH_EXTENSION_DIAGNOSTICS
581#    define _LIBCPP_POP_EXTENSION_DIAGNOSTICS
582#  endif
583
584// clang-format off
585
586// The unversioned namespace is used when we want to be ABI compatible with other standard libraries in some way. There
587// are two main categories where that's the case:
588// - Historically, we have made exception types ABI compatible with libstdc++ to allow throwing them between libstdc++
589//   and libc++. This is not used anymore for new exception types, since there is no use-case for it anymore.
590// - Types and functions which are used by the compiler are in the unversioned namespace, since the compiler has to know
591//   their mangling without the appropriate declaration in some cases.
592// If it's not clear whether using the unversioned namespace is the correct thing to do, it's not. The versioned
593// namespace (_LIBCPP_BEGIN_NAMESPACE_STD) should almost always be used.
594#  define _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD                                                                      \
595    _LIBCPP_PUSH_EXTENSION_DIAGNOSTICS namespace _LIBCPP_NAMESPACE_VISIBILITY std {
596
597#  define _LIBCPP_END_UNVERSIONED_NAMESPACE_STD } _LIBCPP_POP_EXTENSION_DIAGNOSTICS
598
599#  define _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD inline namespace _LIBCPP_ABI_NAMESPACE {
600#  define _LIBCPP_END_NAMESPACE_STD } _LIBCPP_END_UNVERSIONED_NAMESPACE_STD
601
602// TODO: This should really be in the versioned namespace
603#define _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD namespace experimental {
604#define _LIBCPP_END_NAMESPACE_EXPERIMENTAL } _LIBCPP_END_UNVERSIONED_NAMESPACE_STD
605
606#define _LIBCPP_BEGIN_NAMESPACE_LFTS _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL inline namespace fundamentals_v1 {
607#define _LIBCPP_END_NAMESPACE_LFTS } _LIBCPP_END_NAMESPACE_EXPERIMENTAL
608
609#define _LIBCPP_BEGIN_NAMESPACE_LFTS_V2 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL inline namespace fundamentals_v2 {
610#define _LIBCPP_END_NAMESPACE_LFTS_V2 } _LIBCPP_END_NAMESPACE_EXPERIMENTAL
611
612#ifdef _LIBCPP_ABI_NO_FILESYSTEM_INLINE_NAMESPACE
613#  define _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM _LIBCPP_BEGIN_NAMESPACE_STD namespace filesystem {
614#  define _LIBCPP_END_NAMESPACE_FILESYSTEM } _LIBCPP_END_NAMESPACE_STD
615#else
616#  define _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM _LIBCPP_BEGIN_NAMESPACE_STD                                               \
617                                             inline namespace __fs { namespace filesystem {
618
619#  define _LIBCPP_END_NAMESPACE_FILESYSTEM }} _LIBCPP_END_NAMESPACE_STD
620#endif
621
622// clang-format on
623
624#  if __has_attribute(__enable_if__)
625#    define _LIBCPP_PREFERRED_OVERLOAD __attribute__((__enable_if__(true, "")))
626#  endif
627
628#  if !defined(__SIZEOF_INT128__) || defined(_MSC_VER)
629#    define _LIBCPP_HAS_INT128 0
630#  else
631#    define _LIBCPP_HAS_INT128 1
632#  endif
633
634#  ifdef _LIBCPP_CXX03_LANG
635#    define _LIBCPP_DECLARE_STRONG_ENUM(x)                                                                             \
636      struct _LIBCPP_EXPORTED_FROM_ABI x {                                                                             \
637        enum __lx
638// clang-format off
639#    define _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(x)                                                                      \
640      __lx __v_;                                                                                                       \
641      _LIBCPP_HIDE_FROM_ABI x(__lx __v) : __v_(__v) {}                                                                 \
642      _LIBCPP_HIDE_FROM_ABI explicit x(int __v) : __v_(static_cast<__lx>(__v)) {}                                      \
643      _LIBCPP_HIDE_FROM_ABI operator int() const { return __v_; }                                                      \
644      };
645// clang-format on
646
647#  else // _LIBCPP_CXX03_LANG
648#    define _LIBCPP_DECLARE_STRONG_ENUM(x) enum class x
649#    define _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(x)
650#  endif // _LIBCPP_CXX03_LANG
651
652#  ifdef __FreeBSD__
653#    define _DECLARE_C99_LDBL_MATH 1
654#  endif
655
656// If we are getting operator new from the MSVC CRT, then allocation overloads
657// for align_val_t were added in 19.12, aka VS 2017 version 15.3.
658#  if defined(_LIBCPP_MSVCRT) && defined(_MSC_VER) && _MSC_VER < 1912
659#    define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 0
660#  elif defined(_LIBCPP_ABI_VCRUNTIME) && !defined(__cpp_aligned_new)
661// We're deferring to Microsoft's STL to provide aligned new et al. We don't
662// have it unless the language feature test macro is defined.
663#    define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 0
664#  elif defined(__MVS__)
665#    define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 0
666#  else
667#    define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 1
668#  endif
669
670#  if !_LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION || (!defined(__cpp_aligned_new) || __cpp_aligned_new < 201606)
671#    define _LIBCPP_HAS_ALIGNED_ALLOCATION 0
672#  else
673#    define _LIBCPP_HAS_ALIGNED_ALLOCATION 1
674#  endif
675
676// It is not yet possible to use aligned_alloc() on all Apple platforms since
677// 10.15 was the first version to ship an implementation of aligned_alloc().
678#  if defined(__APPLE__)
679#    if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) &&                                                     \
680         __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101500) ||                                                    \
681        (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) &&                                                    \
682         __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 130000) ||                                                   \
683        (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) &&                                                     \
684         __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 60000) ||                                                     \
685        (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 130000)
686#      define _LIBCPP_HAS_C11_ALIGNED_ALLOC 0
687#    else
688#      define _LIBCPP_HAS_C11_ALIGNED_ALLOC 1
689#    endif
690#  elif defined(__ANDROID__) && __ANDROID_API__ < 28
691// Android only provides aligned_alloc when targeting API 28 or higher.
692#    define _LIBCPP_HAS_C11_ALIGNED_ALLOC 0
693#  else
694#    define _LIBCPP_HAS_C11_ALIGNED_ALLOC 1
695#  endif
696
697#  if defined(__APPLE__) || defined(__FreeBSD__)
698#    define _LIBCPP_WCTYPE_IS_MASK
699#  endif
700
701#  if _LIBCPP_STD_VER <= 17 || !defined(__cpp_char8_t)
702#    define _LIBCPP_HAS_CHAR8_T 0
703#  else
704#    define _LIBCPP_HAS_CHAR8_T 1
705#  endif
706
707// Deprecation macros.
708//
709// Deprecations warnings are always enabled, except when users explicitly opt-out
710// by defining _LIBCPP_DISABLE_DEPRECATION_WARNINGS.
711#  if !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS)
712#    if __has_attribute(__deprecated__)
713#      define _LIBCPP_DEPRECATED __attribute__((__deprecated__))
714#      define _LIBCPP_DEPRECATED_(m) __attribute__((__deprecated__(m)))
715#    elif _LIBCPP_STD_VER >= 14
716#      define _LIBCPP_DEPRECATED [[deprecated]]
717#      define _LIBCPP_DEPRECATED_(m) [[deprecated(m)]]
718#    else
719#      define _LIBCPP_DEPRECATED
720#      define _LIBCPP_DEPRECATED_(m)
721#    endif
722#  else
723#    define _LIBCPP_DEPRECATED
724#    define _LIBCPP_DEPRECATED_(m)
725#  endif
726
727#  if !defined(_LIBCPP_CXX03_LANG)
728#    define _LIBCPP_DEPRECATED_IN_CXX11 _LIBCPP_DEPRECATED
729#  else
730#    define _LIBCPP_DEPRECATED_IN_CXX11
731#  endif
732
733#  if _LIBCPP_STD_VER >= 14
734#    define _LIBCPP_DEPRECATED_IN_CXX14 _LIBCPP_DEPRECATED
735#  else
736#    define _LIBCPP_DEPRECATED_IN_CXX14
737#  endif
738
739#  if _LIBCPP_STD_VER >= 17
740#    define _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_DEPRECATED
741#  else
742#    define _LIBCPP_DEPRECATED_IN_CXX17
743#  endif
744
745#  if _LIBCPP_STD_VER >= 20
746#    define _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_DEPRECATED
747#  else
748#    define _LIBCPP_DEPRECATED_IN_CXX20
749#  endif
750
751#  if _LIBCPP_STD_VER >= 23
752#    define _LIBCPP_DEPRECATED_IN_CXX23 _LIBCPP_DEPRECATED
753#  else
754#    define _LIBCPP_DEPRECATED_IN_CXX23
755#  endif
756
757#  if _LIBCPP_STD_VER >= 26
758#    define _LIBCPP_DEPRECATED_IN_CXX26 _LIBCPP_DEPRECATED
759#    define _LIBCPP_DEPRECATED_IN_CXX26_(m) _LIBCPP_DEPRECATED_(m)
760#  else
761#    define _LIBCPP_DEPRECATED_IN_CXX26
762#    define _LIBCPP_DEPRECATED_IN_CXX26_(m)
763#  endif
764
765#  if _LIBCPP_HAS_CHAR8_T
766#    define _LIBCPP_DEPRECATED_WITH_CHAR8_T _LIBCPP_DEPRECATED
767#  else
768#    define _LIBCPP_DEPRECATED_WITH_CHAR8_T
769#  endif
770
771// Macros to enter and leave a state where deprecation warnings are suppressed.
772#  if defined(_LIBCPP_COMPILER_CLANG_BASED) || defined(_LIBCPP_COMPILER_GCC)
773#    define _LIBCPP_SUPPRESS_DEPRECATED_PUSH                                                                           \
774      _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wdeprecated\"")                                \
775          _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
776#    define _LIBCPP_SUPPRESS_DEPRECATED_POP _Pragma("GCC diagnostic pop")
777#  else
778#    define _LIBCPP_SUPPRESS_DEPRECATED_PUSH
779#    define _LIBCPP_SUPPRESS_DEPRECATED_POP
780#  endif
781
782#  if _LIBCPP_STD_VER <= 11
783#    define _LIBCPP_EXPLICIT_SINCE_CXX14
784#  else
785#    define _LIBCPP_EXPLICIT_SINCE_CXX14 explicit
786#  endif
787
788#  if _LIBCPP_STD_VER >= 23
789#    define _LIBCPP_EXPLICIT_SINCE_CXX23 explicit
790#  else
791#    define _LIBCPP_EXPLICIT_SINCE_CXX23
792#  endif
793
794#  if _LIBCPP_STD_VER >= 14
795#    define _LIBCPP_CONSTEXPR_SINCE_CXX14 constexpr
796#  else
797#    define _LIBCPP_CONSTEXPR_SINCE_CXX14
798#  endif
799
800#  if _LIBCPP_STD_VER >= 17
801#    define _LIBCPP_CONSTEXPR_SINCE_CXX17 constexpr
802#  else
803#    define _LIBCPP_CONSTEXPR_SINCE_CXX17
804#  endif
805
806#  if _LIBCPP_STD_VER >= 20
807#    define _LIBCPP_CONSTEXPR_SINCE_CXX20 constexpr
808#  else
809#    define _LIBCPP_CONSTEXPR_SINCE_CXX20
810#  endif
811
812#  if _LIBCPP_STD_VER >= 23
813#    define _LIBCPP_CONSTEXPR_SINCE_CXX23 constexpr
814#  else
815#    define _LIBCPP_CONSTEXPR_SINCE_CXX23
816#  endif
817
818#  if _LIBCPP_STD_VER >= 26
819#    define _LIBCPP_CONSTEXPR_SINCE_CXX26 constexpr
820#  else
821#    define _LIBCPP_CONSTEXPR_SINCE_CXX26
822#  endif
823
824#  ifndef _LIBCPP_WEAK
825#    define _LIBCPP_WEAK __attribute__((__weak__))
826#  endif
827
828// Thread API
829// clang-format off
830#  if _LIBCPP_HAS_THREADS &&                                                                                           \
831      !_LIBCPP_HAS_THREAD_API_PTHREAD &&                                                                               \
832      !_LIBCPP_HAS_THREAD_API_WIN32 &&                                                                                 \
833      !_LIBCPP_HAS_THREAD_API_EXTERNAL
834
835#    if defined(__FreeBSD__) ||                                                                                        \
836        defined(__wasi__) ||                                                                                           \
837        defined(__NetBSD__) ||                                                                                         \
838        defined(__OpenBSD__) ||                                                                                        \
839        defined(__NuttX__) ||                                                                                          \
840        defined(__linux__) ||                                                                                          \
841        defined(__GNU__) ||                                                                                            \
842        defined(__APPLE__) ||                                                                                          \
843        defined(__MVS__) ||                                                                                            \
844        defined(_AIX) ||                                                                                               \
845        defined(__EMSCRIPTEN__)
846// clang-format on
847#      undef _LIBCPP_HAS_THREAD_API_PTHREAD
848#      define _LIBCPP_HAS_THREAD_API_PTHREAD 1
849#    elif defined(__Fuchsia__)
850// TODO(44575): Switch to C11 thread API when possible.
851#      undef _LIBCPP_HAS_THREAD_API_PTHREAD
852#      define _LIBCPP_HAS_THREAD_API_PTHREAD 1
853#    elif defined(_LIBCPP_WIN32API)
854#      undef _LIBCPP_HAS_THREAD_API_WIN32
855#      define _LIBCPP_HAS_THREAD_API_WIN32 1
856#    else
857#      error "No thread API"
858#    endif // _LIBCPP_HAS_THREAD_API
859#  endif   // _LIBCPP_HAS_THREADS
860
861#  if _LIBCPP_HAS_THREAD_API_PTHREAD
862#    if defined(__ANDROID__) && __ANDROID_API__ >= 30
863#      define _LIBCPP_HAS_COND_CLOCKWAIT 1
864#    elif defined(_LIBCPP_GLIBC_PREREQ)
865#      if _LIBCPP_GLIBC_PREREQ(2, 30)
866#        define _LIBCPP_HAS_COND_CLOCKWAIT 1
867#      else
868#        define _LIBCPP_HAS_COND_CLOCKWAIT 0
869#      endif
870#    else
871#      define _LIBCPP_HAS_COND_CLOCKWAIT 0
872#    endif
873#  else
874#    define _LIBCPP_HAS_COND_CLOCKWAIT 0
875#  endif
876
877#  if !_LIBCPP_HAS_THREADS && _LIBCPP_HAS_THREAD_API_PTHREAD
878#    error _LIBCPP_HAS_THREAD_API_PTHREAD may only be true when _LIBCPP_HAS_THREADS is true.
879#  endif
880
881#  if !_LIBCPP_HAS_THREADS && _LIBCPP_HAS_THREAD_API_EXTERNAL
882#    error _LIBCPP_HAS_THREAD_API_EXTERNAL may only be true when _LIBCPP_HAS_THREADS is true.
883#  endif
884
885#  if !_LIBCPP_HAS_MONOTONIC_CLOCK && _LIBCPP_HAS_THREADS
886#    error _LIBCPP_HAS_MONOTONIC_CLOCK may only be false when _LIBCPP_HAS_THREADS is false.
887#  endif
888
889#  if _LIBCPP_HAS_THREADS && !defined(__STDCPP_THREADS__)
890#    define __STDCPP_THREADS__ 1
891#  endif
892
893// The glibc and Bionic implementation of pthreads implements
894// pthread_mutex_destroy as nop for regular mutexes. Additionally, Win32
895// mutexes have no destroy mechanism.
896//
897// This optimization can't be performed on Apple platforms, where
898// pthread_mutex_destroy can allow the kernel to release resources.
899// See https://llvm.org/D64298 for details.
900//
901// TODO(EricWF): Enable this optimization on Bionic after speaking to their
902//               respective stakeholders.
903// clang-format off
904#  if (_LIBCPP_HAS_THREAD_API_PTHREAD && defined(__GLIBC__)) ||                                                        \
905      (_LIBCPP_HAS_THREAD_API_C11 && defined(__Fuchsia__)) ||                                                          \
906       _LIBCPP_HAS_THREAD_API_WIN32
907// clang-format on
908#    define _LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION 1
909#  else
910#    define _LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION 0
911#  endif
912
913// Destroying a condvar is a nop on Windows.
914//
915// This optimization can't be performed on Apple platforms, where
916// pthread_cond_destroy can allow the kernel to release resources.
917// See https://llvm.org/D64298 for details.
918//
919// TODO(EricWF): This is potentially true for some pthread implementations
920// as well.
921#  if (_LIBCPP_HAS_THREAD_API_C11 && defined(__Fuchsia__)) || _LIBCPP_HAS_THREAD_API_WIN32
922#    define _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION 1
923#  else
924#    define _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION 0
925#  endif
926
927#  if defined(__BIONIC__) || defined(__NuttX__) || defined(__Fuchsia__) || defined(__wasi__) ||                        \
928      _LIBCPP_HAS_MUSL_LIBC || defined(__OpenBSD__) || defined(__LLVM_LIBC__)
929#    define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE
930#  endif
931
932#  if __has_feature(cxx_atomic) || __has_extension(c_atomic) || __has_keyword(_Atomic)
933#    define _LIBCPP_HAS_C_ATOMIC_IMP 1
934#    define _LIBCPP_HAS_GCC_ATOMIC_IMP 0
935#    define _LIBCPP_HAS_EXTERNAL_ATOMIC_IMP 0
936#  elif defined(_LIBCPP_COMPILER_GCC)
937#    define _LIBCPP_HAS_C_ATOMIC_IMP 0
938#    define _LIBCPP_HAS_GCC_ATOMIC_IMP 1
939#    define _LIBCPP_HAS_EXTERNAL_ATOMIC_IMP 0
940#  endif
941
942#  if !_LIBCPP_HAS_C_ATOMIC_IMP && !_LIBCPP_HAS_GCC_ATOMIC_IMP && !_LIBCPP_HAS_EXTERNAL_ATOMIC_IMP
943#    define _LIBCPP_HAS_ATOMIC_HEADER 0
944#  else
945#    define _LIBCPP_HAS_ATOMIC_HEADER 1
946#    ifndef _LIBCPP_ATOMIC_FLAG_TYPE
947#      define _LIBCPP_ATOMIC_FLAG_TYPE bool
948#    endif
949#  endif
950
951#  if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(__no_thread_safety_analysis__)
952#    define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((__no_thread_safety_analysis__))
953#  else
954#    define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
955#  endif
956
957#  if _LIBCPP_STD_VER >= 20
958#    define _LIBCPP_CONSTINIT constinit
959#  elif __has_attribute(__require_constant_initialization__)
960#    define _LIBCPP_CONSTINIT __attribute__((__require_constant_initialization__))
961#  else
962#    define _LIBCPP_CONSTINIT
963#  endif
964
965#  if defined(__CUDACC__) || defined(__CUDA_ARCH__) || defined(__CUDA_LIBDEVICE__)
966// The CUDA SDK contains an unfortunate definition for the __noinline__ macro,
967// which breaks the regular __attribute__((__noinline__)) syntax. Therefore,
968// when compiling for CUDA we use the non-underscored version of the noinline
969// attribute.
970//
971// This is a temporary workaround and we still expect the CUDA SDK team to solve
972// this issue properly in the SDK headers.
973//
974// See https://github.com/llvm/llvm-project/pull/73838 for more details.
975#    define _LIBCPP_NOINLINE __attribute__((noinline))
976#  elif __has_attribute(__noinline__)
977#    define _LIBCPP_NOINLINE __attribute__((__noinline__))
978#  else
979#    define _LIBCPP_NOINLINE
980#  endif
981
982// We often repeat things just for handling wide characters in the library.
983// When wide characters are disabled, it can be useful to have a quick way of
984// disabling it without having to resort to #if-#endif, which has a larger
985// impact on readability.
986#  if !_LIBCPP_HAS_WIDE_CHARACTERS
987#    define _LIBCPP_IF_WIDE_CHARACTERS(...)
988#  else
989#    define _LIBCPP_IF_WIDE_CHARACTERS(...) __VA_ARGS__
990#  endif
991
992// clang-format off
993#  define _LIBCPP_PUSH_MACROS _Pragma("push_macro(\"min\")") _Pragma("push_macro(\"max\")") _Pragma("push_macro(\"refresh\")") _Pragma("push_macro(\"move\")") _Pragma("push_macro(\"erase\")")
994#  define _LIBCPP_POP_MACROS _Pragma("pop_macro(\"min\")") _Pragma("pop_macro(\"max\")") _Pragma("pop_macro(\"refresh\")") _Pragma("pop_macro(\"move\")") _Pragma("pop_macro(\"erase\")")
995// clang-format on
996
997#  ifndef _LIBCPP_NO_AUTO_LINK
998#    if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
999#      if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
1000#        pragma comment(lib, "c++.lib")
1001#      else
1002#        pragma comment(lib, "libc++.lib")
1003#      endif
1004#    endif // defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
1005#  endif   // _LIBCPP_NO_AUTO_LINK
1006
1007// Configures the fopen close-on-exec mode character, if any. This string will
1008// be appended to any mode string used by fstream for fopen/fdopen.
1009//
1010// Not all platforms support this, but it helps avoid fd-leaks on platforms that
1011// do.
1012#  if defined(__BIONIC__)
1013#    define _LIBCPP_FOPEN_CLOEXEC_MODE "e"
1014#  else
1015#    define _LIBCPP_FOPEN_CLOEXEC_MODE
1016#  endif
1017
1018#  if __has_cpp_attribute(msvc::no_unique_address)
1019// MSVC implements [[no_unique_address]] as a silent no-op currently.
1020// (If/when MSVC breaks its C++ ABI, it will be changed to work as intended.)
1021// However, MSVC implements [[msvc::no_unique_address]] which does what
1022// [[no_unique_address]] is supposed to do, in general.
1023#    define _LIBCPP_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
1024#  else
1025#    define _LIBCPP_NO_UNIQUE_ADDRESS [[__no_unique_address__]]
1026#  endif
1027
1028// c8rtomb() and mbrtoc8() were added in C++20 and C23. Support for these
1029// functions is gradually being added to existing C libraries. The conditions
1030// below check for known C library versions and conditions under which these
1031// functions are declared by the C library.
1032//
1033// GNU libc 2.36 and newer declare c8rtomb() and mbrtoc8() in C++ modes if
1034// __cpp_char8_t is defined or if C2X extensions are enabled. Determining
1035// the latter depends on internal GNU libc details that are not appropriate
1036// to depend on here, so any declarations present when __cpp_char8_t is not
1037// defined are ignored.
1038#  if defined(_LIBCPP_GLIBC_PREREQ)
1039#    if _LIBCPP_GLIBC_PREREQ(2, 36) && defined(__cpp_char8_t)
1040#      define _LIBCPP_HAS_C8RTOMB_MBRTOC8 1
1041#    else
1042#      define _LIBCPP_HAS_C8RTOMB_MBRTOC8 0
1043#    endif
1044#  else
1045#    define _LIBCPP_HAS_C8RTOMB_MBRTOC8 0
1046#  endif
1047
1048// There are a handful of public standard library types that are intended to
1049// support CTAD but don't need any explicit deduction guides to do so. This
1050// macro is used to mark them as such, which suppresses the
1051// '-Wctad-maybe-unsupported' compiler warning when CTAD is used in user code
1052// with these classes.
1053#  if _LIBCPP_STD_VER >= 17
1054#    ifdef _LIBCPP_COMPILER_CLANG_BASED
1055#      define _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(_ClassName)                                                              \
1056        template <class... _Tag>                                                                                       \
1057        [[maybe_unused]] _ClassName(typename _Tag::__allow_ctad...)->_ClassName<_Tag...>
1058#    else
1059#      define _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(ClassName)                                                               \
1060        template <class... _Tag>                                                                                       \
1061        ClassName(typename _Tag::__allow_ctad...)->ClassName<_Tag...>
1062#    endif
1063#  else
1064#    define _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(_ClassName) static_assert(true, "")
1065#  endif
1066
1067// TODO(LLVM 22): Remove the workaround
1068#  if defined(__OBJC__) && (!defined(_LIBCPP_CLANG_VER) || _LIBCPP_CLANG_VER < 2001)
1069#    define _LIBCPP_WORKAROUND_OBJCXX_COMPILER_INTRINSICS
1070#  endif
1071
1072#  define _PSTL_PRAGMA(x) _Pragma(#x)
1073
1074// Enable SIMD for compilers that support OpenMP 4.0
1075#  if (defined(_OPENMP) && _OPENMP >= 201307)
1076
1077#    define _PSTL_UDR_PRESENT
1078#    define _PSTL_PRAGMA_SIMD _PSTL_PRAGMA(omp simd)
1079#    define _PSTL_PRAGMA_DECLARE_SIMD _PSTL_PRAGMA(omp declare simd)
1080#    define _PSTL_PRAGMA_SIMD_REDUCTION(PRM) _PSTL_PRAGMA(omp simd reduction(PRM))
1081#    define _PSTL_PRAGMA_SIMD_SCAN(PRM) _PSTL_PRAGMA(omp simd reduction(inscan, PRM))
1082#    define _PSTL_PRAGMA_SIMD_INCLUSIVE_SCAN(PRM) _PSTL_PRAGMA(omp scan inclusive(PRM))
1083#    define _PSTL_PRAGMA_SIMD_EXCLUSIVE_SCAN(PRM) _PSTL_PRAGMA(omp scan exclusive(PRM))
1084
1085// Declaration of reduction functor, where
1086// NAME - the name of the functor
1087// OP - type of the callable object with the reduction operation
1088// omp_in - refers to the local partial result
1089// omp_out - refers to the final value of the combiner operator
1090// omp_priv - refers to the private copy of the initial value
1091// omp_orig - refers to the original variable to be reduced
1092#    define _PSTL_PRAGMA_DECLARE_REDUCTION(NAME, OP)                                                                   \
1093      _PSTL_PRAGMA(omp declare reduction(NAME:OP : omp_out(omp_in)) initializer(omp_priv = omp_orig))
1094
1095#  elif defined(_LIBCPP_COMPILER_CLANG_BASED)
1096
1097#    define _PSTL_PRAGMA_SIMD _Pragma("clang loop vectorize(enable) interleave(enable)")
1098#    define _PSTL_PRAGMA_DECLARE_SIMD
1099#    define _PSTL_PRAGMA_SIMD_REDUCTION(PRM) _Pragma("clang loop vectorize(enable) interleave(enable)")
1100#    define _PSTL_PRAGMA_SIMD_SCAN(PRM) _Pragma("clang loop vectorize(enable) interleave(enable)")
1101#    define _PSTL_PRAGMA_SIMD_INCLUSIVE_SCAN(PRM)
1102#    define _PSTL_PRAGMA_SIMD_EXCLUSIVE_SCAN(PRM)
1103#    define _PSTL_PRAGMA_DECLARE_REDUCTION(NAME, OP)
1104
1105#  else // (defined(_OPENMP) && _OPENMP >= 201307)
1106
1107#    define _PSTL_PRAGMA_SIMD
1108#    define _PSTL_PRAGMA_DECLARE_SIMD
1109#    define _PSTL_PRAGMA_SIMD_REDUCTION(PRM)
1110#    define _PSTL_PRAGMA_SIMD_SCAN(PRM)
1111#    define _PSTL_PRAGMA_SIMD_INCLUSIVE_SCAN(PRM)
1112#    define _PSTL_PRAGMA_SIMD_EXCLUSIVE_SCAN(PRM)
1113#    define _PSTL_PRAGMA_DECLARE_REDUCTION(NAME, OP)
1114
1115#  endif // (defined(_OPENMP) && _OPENMP >= 201307)
1116
1117#  define _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED
1118
1119// Optional attributes - these are useful for a better QoI, but not required to be available
1120
1121#  define _LIBCPP_NOALIAS __attribute__((__malloc__))
1122#  define _LIBCPP_NODEBUG [[__gnu__::__nodebug__]]
1123#  define _LIBCPP_NO_SANITIZE(...) __attribute__((__no_sanitize__(__VA_ARGS__)))
1124#  define _LIBCPP_INIT_PRIORITY_MAX __attribute__((__init_priority__(100)))
1125#  define _LIBCPP_ATTRIBUTE_FORMAT(archetype, format_string_index, first_format_arg_index)                             \
1126    __attribute__((__format__(archetype, format_string_index, first_format_arg_index)))
1127#  define _LIBCPP_PACKED __attribute__((__packed__))
1128
1129#  if __has_attribute(__no_sanitize__) && !defined(_LIBCPP_COMPILER_GCC)
1130#    define _LIBCPP_NO_CFI __attribute__((__no_sanitize__("cfi")))
1131#  else
1132#    define _LIBCPP_NO_CFI
1133#  endif
1134
1135#  if __has_attribute(__using_if_exists__)
1136#    define _LIBCPP_USING_IF_EXISTS __attribute__((__using_if_exists__))
1137#  else
1138#    define _LIBCPP_USING_IF_EXISTS
1139#  endif
1140
1141#  if __has_cpp_attribute(_Clang::__no_destroy__)
1142#    define _LIBCPP_NO_DESTROY [[_Clang::__no_destroy__]]
1143#  else
1144#    define _LIBCPP_NO_DESTROY
1145#  endif
1146
1147#  if __has_attribute(__diagnose_if__)
1148#    define _LIBCPP_DIAGNOSE_WARNING(...) __attribute__((__diagnose_if__(__VA_ARGS__, "warning")))
1149#  else
1150#    define _LIBCPP_DIAGNOSE_WARNING(...)
1151#  endif
1152
1153#  if __has_cpp_attribute(_Clang::__lifetimebound__)
1154#    define _LIBCPP_LIFETIMEBOUND [[_Clang::__lifetimebound__]]
1155#  else
1156#    define _LIBCPP_LIFETIMEBOUND
1157#  endif
1158
1159#  if __has_cpp_attribute(_Clang::__noescape__)
1160#    define _LIBCPP_NOESCAPE [[_Clang::__noescape__]]
1161#  else
1162#    define _LIBCPP_NOESCAPE
1163#  endif
1164
1165#  if __has_cpp_attribute(_Clang::__no_specializations__)
1166#    define _LIBCPP_NO_SPECIALIZATIONS                                                                                 \
1167      [[_Clang::__no_specializations__("Users are not allowed to specialize this standard library entity")]]
1168#  else
1169#    define _LIBCPP_NO_SPECIALIZATIONS
1170#  endif
1171
1172#  if __has_cpp_attribute(_Clang::__standalone_debug__)
1173#    define _LIBCPP_STANDALONE_DEBUG [[_Clang::__standalone_debug__]]
1174#  else
1175#    define _LIBCPP_STANDALONE_DEBUG
1176#  endif
1177
1178#  if __has_cpp_attribute(_Clang::__preferred_name__)
1179#    define _LIBCPP_PREFERRED_NAME(x) [[_Clang::__preferred_name__(x)]]
1180#  else
1181#    define _LIBCPP_PREFERRED_NAME(x)
1182#  endif
1183
1184#  if __has_cpp_attribute(_Clang::__scoped_lockable__)
1185#    define _LIBCPP_SCOPED_LOCKABLE [[_Clang::__scoped_lockable__]]
1186#  else
1187#    define _LIBCPP_SCOPED_LOCKABLE
1188#  endif
1189
1190#  if __has_cpp_attribute(_Clang::__capability__)
1191#    define _LIBCPP_CAPABILITY(...) [[_Clang::__capability__(__VA_ARGS__)]]
1192#  else
1193#    define _LIBCPP_CAPABILITY(...)
1194#  endif
1195
1196#  if __has_attribute(__acquire_capability__)
1197#    define _LIBCPP_ACQUIRE_CAPABILITY(...) __attribute__((__acquire_capability__(__VA_ARGS__)))
1198#  else
1199#    define _LIBCPP_ACQUIRE_CAPABILITY(...)
1200#  endif
1201
1202#  if __has_cpp_attribute(_Clang::__try_acquire_capability__)
1203#    define _LIBCPP_TRY_ACQUIRE_CAPABILITY(...) [[_Clang::__try_acquire_capability__(__VA_ARGS__)]]
1204#  else
1205#    define _LIBCPP_TRY_ACQUIRE_CAPABILITY(...)
1206#  endif
1207
1208#  if __has_cpp_attribute(_Clang::__acquire_shared_capability__)
1209#    define _LIBCPP_ACQUIRE_SHARED_CAPABILITY [[_Clang::__acquire_shared_capability__]]
1210#  else
1211#    define _LIBCPP_ACQUIRE_SHARED_CAPABILITY
1212#  endif
1213
1214#  if __has_cpp_attribute(_Clang::__try_acquire_shared_capability__)
1215#    define _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(...) [[_Clang::__try_acquire_shared_capability__(__VA_ARGS__)]]
1216#  else
1217#    define _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(...)
1218#  endif
1219
1220#  if __has_cpp_attribute(_Clang::__release_capability__)
1221#    define _LIBCPP_RELEASE_CAPABILITY [[_Clang::__release_capability__]]
1222#  else
1223#    define _LIBCPP_RELEASE_CAPABILITY
1224#  endif
1225
1226#  if __has_cpp_attribute(_Clang::__release_shared_capability__)
1227#    define _LIBCPP_RELEASE_SHARED_CAPABILITY [[_Clang::__release_shared_capability__]]
1228#  else
1229#    define _LIBCPP_RELEASE_SHARED_CAPABILITY
1230#  endif
1231
1232#  if __has_attribute(__requires_capability__)
1233#    define _LIBCPP_REQUIRES_CAPABILITY(...) __attribute__((__requires_capability__(__VA_ARGS__)))
1234#  else
1235#    define _LIBCPP_REQUIRES_CAPABILITY(...)
1236#  endif
1237
1238#  if defined(_LIBCPP_ABI_MICROSOFT) && __has_declspec_attribute(empty_bases)
1239#    define _LIBCPP_DECLSPEC_EMPTY_BASES __declspec(empty_bases)
1240#  else
1241#    define _LIBCPP_DECLSPEC_EMPTY_BASES
1242#  endif
1243
1244// Allow for build-time disabling of unsigned integer sanitization
1245#  if __has_attribute(no_sanitize) && !defined(_LIBCPP_COMPILER_GCC)
1246#    define _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK __attribute__((__no_sanitize__("unsigned-integer-overflow")))
1247#  else
1248#    define _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1249#  endif
1250
1251#  if __has_feature(nullability)
1252#    define _LIBCPP_DIAGNOSE_NULLPTR _Nonnull
1253#  else
1254#    define _LIBCPP_DIAGNOSE_NULLPTR
1255#  endif
1256
1257// TODO(LLVM 22): Remove this macro once LLVM19 support ends. __cpp_explicit_this_parameter has been set in LLVM20.
1258// Clang-18 has support for deducing this, but it does not set the FTM.
1259#  if defined(__cpp_explicit_this_parameter) || (defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 1800)
1260#    define _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER 1
1261#  else
1262#    define _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER 0
1263#  endif
1264
1265#endif // __cplusplus
1266
1267#endif // _LIBCPP___CONFIG
1268