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