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__) || defined(__BIONIC__)
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 defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606
161# define _LIBCPP_HAS_ALIGNED_ALLOCATION 1
162# else
163# define _LIBCPP_HAS_ALIGNED_ALLOCATION 0
164# endif
165
166# if defined(__APPLE__) || defined(__FreeBSD__)
167# define _LIBCPP_WCTYPE_IS_MASK
168# endif
169
170// FIXME: using `#warning` causes diagnostics from system headers which include deprecated headers. This can only be
171// enabled again once https://github.com/llvm/llvm-project/pull/168041 (or a similar feature) has landed, since that
172// allows suppression in system headers.
173# if defined(__DEPRECATED) && __DEPRECATED && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && 0
174# define _LIBCPP_DIAGNOSE_DEPRECATED_HEADERS 1
175# else
176# define _LIBCPP_DIAGNOSE_DEPRECATED_HEADERS 0
177# endif
178
179// Thread API
180// clang-format off
181# if _LIBCPP_HAS_THREADS && \
182 !_LIBCPP_HAS_THREAD_API_PTHREAD && \
183 !_LIBCPP_HAS_THREAD_API_WIN32 && \
184 !_LIBCPP_HAS_THREAD_API_EXTERNAL && \
185 !_LIBCPP_HAS_THREAD_API_C11
186
187# if defined(__FreeBSD__) || \
188 defined(__wasi__) || \
189 defined(__NetBSD__) || \
190 defined(__OpenBSD__) || \
191 defined(__NuttX__) || \
192 defined(__linux__) || \
193 defined(__GNU__) || \
194 defined(__APPLE__) || \
195 defined(__MVS__) || \
196 defined(_AIX) || \
197 defined(__EMSCRIPTEN__)
198// clang-format on
199# undef _LIBCPP_HAS_THREAD_API_PTHREAD
200# define _LIBCPP_HAS_THREAD_API_PTHREAD 1
201# elif defined(__Fuchsia__)
202// TODO(44575): Switch to C11 thread API when possible.
203# undef _LIBCPP_HAS_THREAD_API_PTHREAD
204# define _LIBCPP_HAS_THREAD_API_PTHREAD 1
205# elif defined(_WIN32)
206# undef _LIBCPP_HAS_THREAD_API_WIN32
207# define _LIBCPP_HAS_THREAD_API_WIN32 1
208# else
209# error "No thread API"
210# endif // _LIBCPP_HAS_THREAD_API
211# endif // _LIBCPP_HAS_THREADS
212
213# if !_LIBCPP_HAS_THREADS && _LIBCPP_HAS_THREAD_API_PTHREAD
214# error _LIBCPP_HAS_THREAD_API_PTHREAD may only be true when _LIBCPP_HAS_THREADS is true.
215# endif
216
217# if !_LIBCPP_HAS_THREADS && _LIBCPP_HAS_THREAD_API_EXTERNAL
218# error _LIBCPP_HAS_THREAD_API_EXTERNAL may only be true when _LIBCPP_HAS_THREADS is true.
219# endif
220
221# if !_LIBCPP_HAS_THREADS && _LIBCPP_HAS_THREAD_API_C11
222# error _LIBCPP_HAS_THREAD_API_C11 may only be true when _LIBCPP_HAS_THREADS is true.
223# endif
224
225# if !_LIBCPP_HAS_MONOTONIC_CLOCK && _LIBCPP_HAS_THREADS
226# error _LIBCPP_HAS_MONOTONIC_CLOCK may only be false when _LIBCPP_HAS_THREADS is false.
227# endif
228
229# if _LIBCPP_HAS_THREADS && !defined(__STDCPP_THREADS__)
230# define __STDCPP_THREADS__ 1
231# endif
232
233// The glibc and Bionic implementation of pthreads implements
234// pthread_mutex_destroy as nop for regular mutexes. Additionally, Win32
235// mutexes have no destroy mechanism.
236//
237// This optimization can't be performed on Apple platforms, where
238// pthread_mutex_destroy can allow the kernel to release resources.
239// See https://llvm.org/D64298 for details.
240//
241// clang-format off
242# if (_LIBCPP_HAS_THREAD_API_PTHREAD && (defined(__GLIBC__) || defined(__BIONIC__))) || \
243 (_LIBCPP_HAS_THREAD_API_C11 && defined(__Fuchsia__)) || \
244 _LIBCPP_HAS_THREAD_API_WIN32
245// clang-format on
246# define _LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION 1
247# else
248# define _LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION 0
249# endif
250
251// Destroying a condvar is a nop on Windows.
252//
253// This optimization can't be performed on Apple platforms, where
254// pthread_cond_destroy can allow the kernel to release resources.
255// See https://llvm.org/D64298 for details.
256//
257// TODO(EricWF): This is potentially true for some pthread implementations
258// as well.
259# if (_LIBCPP_HAS_THREAD_API_C11 && defined(__Fuchsia__)) || _LIBCPP_HAS_THREAD_API_WIN32
260# define _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION 1
261# else
262# define _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION 0
263# endif
264
265// We often repeat things just for handling wide characters in the library.
266// When wide characters are disabled, it can be useful to have a quick way of
267// disabling it without having to resort to #if-#endif, which has a larger
268// impact on readability.
269# if !_LIBCPP_HAS_WIDE_CHARACTERS
270# define _LIBCPP_IF_WIDE_CHARACTERS(...)
271# else
272# define _LIBCPP_IF_WIDE_CHARACTERS(...) __VA_ARGS__
273# endif
274
275// clang-format off
276# define _LIBCPP_PUSH_MACROS _Pragma("push_macro(\"min\")") _Pragma("push_macro(\"max\")") _Pragma("push_macro(\"refresh\")") _Pragma("push_macro(\"move\")") _Pragma("push_macro(\"erase\")")
277# define _LIBCPP_POP_MACROS _Pragma("pop_macro(\"min\")") _Pragma("pop_macro(\"max\")") _Pragma("pop_macro(\"refresh\")") _Pragma("pop_macro(\"move\")") _Pragma("pop_macro(\"erase\")")
278// clang-format on
279
280# ifndef _LIBCPP_NO_AUTO_LINK
281# if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
282# if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
283# pragma comment(lib, "c++.lib")
284# else
285# pragma comment(lib, "libc++.lib")
286# endif
287# endif // defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
288# endif // _LIBCPP_NO_AUTO_LINK
289
290// c8rtomb() and mbrtoc8() were added in C++20 and C23. Support for these
291// functions is gradually being added to existing C libraries. The conditions
292// below check for known C library versions and conditions under which these
293// functions are declared by the C library.
294//
295// GNU libc 2.36 and newer declare c8rtomb() and mbrtoc8() in C++ modes if
296// __cpp_char8_t is defined or if C2X extensions are enabled. Determining
297// the latter depends on internal GNU libc details that are not appropriate
298// to depend on here, so any declarations present when __cpp_char8_t is not
299// defined are ignored.
300# if _LIBCPP_GLIBC_PREREQ(2, 36) && defined(__cpp_char8_t)
301# define _LIBCPP_HAS_C8RTOMB_MBRTOC8 1
302# else
303# define _LIBCPP_HAS_C8RTOMB_MBRTOC8 0
304# endif
305
306// There are a handful of public standard library types that are intended to
307// support CTAD but don't need any explicit deduction guides to do so. This
308// macro is used to mark them as such, which suppresses the
309// '-Wctad-maybe-unsupported' compiler warning when CTAD is used in user code
310// with these classes.
311# if _LIBCPP_STD_VER >= 17
312# ifdef _LIBCPP_COMPILER_CLANG_BASED
313# define _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(_ClassName) \
314 template <class... _Tag> \
315 [[maybe_unused]] _ClassName(typename _Tag::__allow_ctad...)->_ClassName<_Tag...>
316# else
317# define _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(ClassName) \
318 template <class... _Tag> \
319 ClassName(typename _Tag::__allow_ctad...)->ClassName<_Tag...>
320# endif
321# else
322# define _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(_ClassName) static_assert(true, "")
323# endif
324
325#endif // __cplusplus
326
327#endif // _LIBCPP___CONFIG
328