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
15#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
16# pragma GCC system_header
17#endif
18
19#ifdef __cplusplus
20
21# include <__configuration/abi.h>
22# include <__configuration/attributes.h>
23# include <__configuration/availability.h>
24# include <__configuration/compiler.h>
25# include <__configuration/diagnostic_suppression.h>
26# include <__configuration/experimental.h>
27# include <__configuration/hardening.h>
28# include <__configuration/language.h>
29# include <__configuration/namespace.h>
30# include <__configuration/platform.h>
31# include <__configuration/pstl.h>
32# include <__configuration/utility.h>
33
34// The attributes supported by clang are documented at https://clang.llvm.org/docs/AttributeReference.html
35
36// _LIBCPP_VERSION represents the version of libc++, which matches the version of LLVM.
37// Given a LLVM release LLVM XX.YY.ZZ (e.g. LLVM 17.0.1 == 17.00.01), _LIBCPP_VERSION is
38// defined to XXYYZZ.
39# define _LIBCPP_VERSION 230000
40
41# if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_NO_VCRUNTIME)
42# define _LIBCPP_ABI_VCRUNTIME
43# endif
44
45# if defined(_WIN32)
46# define _LIBCPP_WIN32API
47# define _LIBCPP_SHORT_WCHAR 1
48// Both MinGW and native MSVC provide a "MSVC"-like environment
49# define _LIBCPP_MSVCRT_LIKE
50// If mingw not explicitly detected, assume using MS C runtime only if
51// a MS compatibility version is specified.
52# if defined(_MSC_VER) && !defined(__MINGW32__)
53# define _LIBCPP_MSVCRT // Using Microsoft's C Runtime library
54# endif
55# define _LIBCPP_HAS_OPEN_WITH_WCHAR 1
56# else
57# define _LIBCPP_HAS_OPEN_WITH_WCHAR 0
58# endif // defined(_WIN32)
59
60# if defined(_AIX) && !defined(__64BIT__)
61// The size of wchar is 2 byte on 32-bit mode on AIX.
62# define _LIBCPP_SHORT_WCHAR 1
63# endif
64
65// Libc++ supports various implementations of std::random_device.
66//
67// _LIBCPP_USING_DEV_RANDOM
68// Read entropy from the given file, by default `/dev/urandom`.
69// If a token is provided, it is assumed to be the path to a file
70// to read entropy from. This is the default behavior if nothing
71// else is specified. This implementation requires storing state
72// inside `std::random_device`.
73//
74// _LIBCPP_USING_ARC4_RANDOM
75// Use arc4random(). This allows obtaining random data even when
76// using sandboxing mechanisms. On some platforms like Apple, this
77// is the recommended source of entropy for user-space programs.
78// When this option is used, the token passed to `std::random_device`'s
79// constructor *must* be "/dev/urandom" -- anything else is an error.
80//
81// _LIBCPP_USING_GETENTROPY
82// Use getentropy().
83// When this option is used, the token passed to `std::random_device`'s
84// constructor *must* be "/dev/urandom" -- anything else is an error.
85//
86// _LIBCPP_USING_FUCHSIA_CPRNG
87// Use Fuchsia's zx_cprng_draw() system call, which is specified to
88// deliver high-quality entropy and cannot fail.
89// When this option is used, the token passed to `std::random_device`'s
90// constructor *must* be "/dev/urandom" -- anything else is an error.
91//
92// _LIBCPP_USING_WIN32_RANDOM
93// Use rand_s(), for use on Windows.
94// When this option is used, the token passed to `std::random_device`'s
95// constructor *must* be "/dev/urandom" -- anything else is an error.
96# if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
97 defined(__DragonFly__)
98# define _LIBCPP_USING_ARC4_RANDOM
99# elif defined(__wasi__) || defined(__EMSCRIPTEN__)
100# define _LIBCPP_USING_GETENTROPY
101# elif defined(__Fuchsia__)
102# define _LIBCPP_USING_FUCHSIA_CPRNG
103# elif defined(_LIBCPP_WIN32API)
104# define _LIBCPP_USING_WIN32_RANDOM
105# else
106# define _LIBCPP_USING_DEV_RANDOM
107# endif
108
109# ifndef _LIBCPP_CXX03_LANG
110
111# define _LIBCPP_ALIGNOF(...) alignof(__VA_ARGS__)
112# define _ALIGNAS_TYPE(x) alignas(x)
113# define _ALIGNAS(x) alignas(x)
114# define _NOEXCEPT noexcept
115# define _NOEXCEPT_(...) noexcept(__VA_ARGS__)
116# define _LIBCPP_CONSTEXPR constexpr
117
118# else
119
120# define _LIBCPP_ALIGNOF(...) _Alignof(__VA_ARGS__)
121# define _ALIGNAS_TYPE(x) __attribute__((__aligned__(_LIBCPP_ALIGNOF(x))))
122# define _ALIGNAS(x) __attribute__((__aligned__(x)))
123# define nullptr __nullptr
124# define _NOEXCEPT throw()
125# define _NOEXCEPT_(...)
126# define static_assert(...) _Static_assert(__VA_ARGS__)
127# define decltype(...) __decltype(__VA_ARGS__)
128# define _LIBCPP_CONSTEXPR
129
130typedef __char16_t char16_t;
131typedef __char32_t char32_t;
132
133# endif
134
135# define _LIBCPP_PREFERRED_ALIGNOF(_Tp) __alignof(_Tp)
136
137# if __has_extension(blocks) && defined(__APPLE__)
138# define _LIBCPP_HAS_BLOCKS_RUNTIME 1
139# else
140# define _LIBCPP_HAS_BLOCKS_RUNTIME 0
141# endif
142
143# if !defined(__SIZEOF_INT128__) || defined(_MSC_VER)
144# define _LIBCPP_HAS_INT128 0
145# else
146# define _LIBCPP_HAS_INT128 1
147# endif
148
149# ifdef _LIBCPP_CXX03_LANG
150# define _LIBCPP_DECLARE_STRONG_ENUM(x) \
151 struct _LIBCPP_EXPORTED_FROM_ABI x { \
152 enum __lx
153// clang-format off
154# define _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(x) \
155 __lx __v_; \
156 _LIBCPP_HIDE_FROM_ABI x(__lx __v) : __v_(__v) {} \
157 _LIBCPP_HIDE_FROM_ABI explicit x(int __v) : __v_(static_cast<__lx>(__v)) {} \
158 _LIBCPP_HIDE_FROM_ABI operator int() const { return __v_; } \
159 };
160// clang-format on
161
162# else // _LIBCPP_CXX03_LANG
163# define _LIBCPP_DECLARE_STRONG_ENUM(x) enum class x
164# define _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(x)
165# endif // _LIBCPP_CXX03_LANG
166
167# ifdef __FreeBSD__
168# define _DECLARE_C99_LDBL_MATH 1
169# endif
170
171// If we are getting operator new from the MSVC CRT, then allocation overloads
172// for align_val_t were added in 19.12, aka VS 2017 version 15.3.
173# if defined(_LIBCPP_MSVCRT) && defined(_MSC_VER) && _MSC_VER < 1912
174# define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 0
175# elif defined(_LIBCPP_ABI_VCRUNTIME) && !defined(__cpp_aligned_new)
176// We're deferring to Microsoft's STL to provide aligned new et al. We don't
177// have it unless the language feature test macro is defined.
178# define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 0
179# elif defined(__MVS__)
180# define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 0
181# else
182# define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 1
183# endif
184
185# if !_LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION || (!defined(__cpp_aligned_new) || __cpp_aligned_new < 201606)
186# define _LIBCPP_HAS_ALIGNED_ALLOCATION 0
187# else
188# define _LIBCPP_HAS_ALIGNED_ALLOCATION 1
189# endif
190
191# if defined(__APPLE__) || defined(__FreeBSD__)
192# define _LIBCPP_WCTYPE_IS_MASK
193# endif
194
195// FIXME: using `#warning` causes diagnostics from system headers which include deprecated headers. This can only be
196// enabled again once https://github.com/llvm/llvm-project/pull/168041 (or a similar feature) has landed, since that
197// allows suppression in system headers.
198# if defined(__DEPRECATED) && __DEPRECATED && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && 0
199# define _LIBCPP_DIAGNOSE_DEPRECATED_HEADERS 1
200# else
201# define _LIBCPP_DIAGNOSE_DEPRECATED_HEADERS 0
202# endif
203
204// Thread API
205// clang-format off
206# if _LIBCPP_HAS_THREADS && \
207 !_LIBCPP_HAS_THREAD_API_PTHREAD && \
208 !_LIBCPP_HAS_THREAD_API_WIN32 && \
209 !_LIBCPP_HAS_THREAD_API_EXTERNAL && \
210 !_LIBCPP_HAS_THREAD_API_C11
211
212# if defined(__FreeBSD__) || \
213 defined(__wasi__) || \
214 defined(__NetBSD__) || \
215 defined(__OpenBSD__) || \
216 defined(__NuttX__) || \
217 defined(__linux__) || \
218 defined(__GNU__) || \
219 defined(__APPLE__) || \
220 defined(__MVS__) || \
221 defined(_AIX) || \
222 defined(__EMSCRIPTEN__)
223// clang-format on
224# undef _LIBCPP_HAS_THREAD_API_PTHREAD
225# define _LIBCPP_HAS_THREAD_API_PTHREAD 1
226# elif defined(__Fuchsia__)
227// TODO(44575): Switch to C11 thread API when possible.
228# undef _LIBCPP_HAS_THREAD_API_PTHREAD
229# define _LIBCPP_HAS_THREAD_API_PTHREAD 1
230# elif defined(_LIBCPP_WIN32API)
231# undef _LIBCPP_HAS_THREAD_API_WIN32
232# define _LIBCPP_HAS_THREAD_API_WIN32 1
233# else
234# error "No thread API"
235# endif // _LIBCPP_HAS_THREAD_API
236# endif // _LIBCPP_HAS_THREADS
237
238# if !_LIBCPP_HAS_THREAD_API_PTHREAD
239# define _LIBCPP_HAS_COND_CLOCKWAIT 0
240# elif (defined(__ANDROID__) && __ANDROID_API__ >= 30) || _LIBCPP_GLIBC_PREREQ(2, 30)
241# define _LIBCPP_HAS_COND_CLOCKWAIT 1
242# else
243# define _LIBCPP_HAS_COND_CLOCKWAIT 0
244# endif
245
246# if !_LIBCPP_HAS_THREADS && _LIBCPP_HAS_THREAD_API_PTHREAD
247# error _LIBCPP_HAS_THREAD_API_PTHREAD may only be true when _LIBCPP_HAS_THREADS is true.
248# endif
249
250# if !_LIBCPP_HAS_THREADS && _LIBCPP_HAS_THREAD_API_EXTERNAL
251# error _LIBCPP_HAS_THREAD_API_EXTERNAL may only be true when _LIBCPP_HAS_THREADS is true.
252# endif
253
254# if !_LIBCPP_HAS_THREADS && _LIBCPP_HAS_THREAD_API_C11
255# error _LIBCPP_HAS_THREAD_API_C11 may only be true when _LIBCPP_HAS_THREADS is true.
256# endif
257
258# if !_LIBCPP_HAS_MONOTONIC_CLOCK && _LIBCPP_HAS_THREADS
259# error _LIBCPP_HAS_MONOTONIC_CLOCK may only be false when _LIBCPP_HAS_THREADS is false.
260# endif
261
262# if _LIBCPP_HAS_THREADS && !defined(__STDCPP_THREADS__)
263# define __STDCPP_THREADS__ 1
264# endif
265
266// The glibc and Bionic implementation of pthreads implements
267// pthread_mutex_destroy as nop for regular mutexes. Additionally, Win32
268// mutexes have no destroy mechanism.
269//
270// This optimization can't be performed on Apple platforms, where
271// pthread_mutex_destroy can allow the kernel to release resources.
272// See https://llvm.org/D64298 for details.
273//
274// TODO(EricWF): Enable this optimization on Bionic after speaking to their
275// respective stakeholders.
276// clang-format off
277# if (_LIBCPP_HAS_THREAD_API_PTHREAD && defined(__GLIBC__)) || \
278 (_LIBCPP_HAS_THREAD_API_C11 && defined(__Fuchsia__)) || \
279 _LIBCPP_HAS_THREAD_API_WIN32
280// clang-format on
281# define _LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION 1
282# else
283# define _LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION 0
284# endif
285
286// Destroying a condvar is a nop on Windows.
287//
288// This optimization can't be performed on Apple platforms, where
289// pthread_cond_destroy can allow the kernel to release resources.
290// See https://llvm.org/D64298 for details.
291//
292// TODO(EricWF): This is potentially true for some pthread implementations
293// as well.
294# if (_LIBCPP_HAS_THREAD_API_C11 && defined(__Fuchsia__)) || _LIBCPP_HAS_THREAD_API_WIN32
295# define _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION 1
296# else
297# define _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION 0
298# endif
299
300# if defined(__BIONIC__) || defined(__NuttX__) || defined(__Fuchsia__) || defined(__wasi__) || \
301 _LIBCPP_HAS_MUSL_LIBC || defined(__OpenBSD__) || _LIBCPP_LIBC_LLVM_LIBC
302# define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE
303# endif
304
305// We often repeat things just for handling wide characters in the library.
306// When wide characters are disabled, it can be useful to have a quick way of
307// disabling it without having to resort to #if-#endif, which has a larger
308// impact on readability.
309# if !_LIBCPP_HAS_WIDE_CHARACTERS
310# define _LIBCPP_IF_WIDE_CHARACTERS(...)
311# else
312# define _LIBCPP_IF_WIDE_CHARACTERS(...) __VA_ARGS__
313# endif
314
315// clang-format off
316# define _LIBCPP_PUSH_MACROS _Pragma("push_macro(\"min\")") _Pragma("push_macro(\"max\")") _Pragma("push_macro(\"refresh\")") _Pragma("push_macro(\"move\")") _Pragma("push_macro(\"erase\")")
317# define _LIBCPP_POP_MACROS _Pragma("pop_macro(\"min\")") _Pragma("pop_macro(\"max\")") _Pragma("pop_macro(\"refresh\")") _Pragma("pop_macro(\"move\")") _Pragma("pop_macro(\"erase\")")
318// clang-format on
319
320# ifndef _LIBCPP_NO_AUTO_LINK
321# if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
322# if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
323# pragma comment(lib, "c++.lib")
324# else
325# pragma comment(lib, "libc++.lib")
326# endif
327# endif // defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
328# endif // _LIBCPP_NO_AUTO_LINK
329
330// c8rtomb() and mbrtoc8() were added in C++20 and C23. Support for these
331// functions is gradually being added to existing C libraries. The conditions
332// below check for known C library versions and conditions under which these
333// functions are declared by the C library.
334//
335// GNU libc 2.36 and newer declare c8rtomb() and mbrtoc8() in C++ modes if
336// __cpp_char8_t is defined or if C2X extensions are enabled. Determining
337// the latter depends on internal GNU libc details that are not appropriate
338// to depend on here, so any declarations present when __cpp_char8_t is not
339// defined are ignored.
340# if _LIBCPP_GLIBC_PREREQ(2, 36) && defined(__cpp_char8_t)
341# define _LIBCPP_HAS_C8RTOMB_MBRTOC8 1
342# else
343# define _LIBCPP_HAS_C8RTOMB_MBRTOC8 0
344# endif
345
346// There are a handful of public standard library types that are intended to
347// support CTAD but don't need any explicit deduction guides to do so. This
348// macro is used to mark them as such, which suppresses the
349// '-Wctad-maybe-unsupported' compiler warning when CTAD is used in user code
350// with these classes.
351# if _LIBCPP_STD_VER >= 17
352# ifdef _LIBCPP_COMPILER_CLANG_BASED
353# define _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(_ClassName) \
354 template <class... _Tag> \
355 [[maybe_unused]] _ClassName(typename _Tag::__allow_ctad...)->_ClassName<_Tag...>
356# else
357# define _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(ClassName) \
358 template <class... _Tag> \
359 ClassName(typename _Tag::__allow_ctad...)->ClassName<_Tag...>
360# endif
361# else
362# define _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(_ClassName) static_assert(true, "")
363# endif
364
365#endif // __cplusplus
366
367#endif // _LIBCPP___CONFIG
368