1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___CONFIGURATION_ATTRIBUTES_H
10#define _LIBCPP___CONFIGURATION_ATTRIBUTES_H
11
12#include <__config_site>
13#include <__configuration/hardening.h>
14#include <__configuration/language.h>
15
16#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
17# pragma GCC system_header
18#endif
19
20#ifndef __has_declspec_attribute
21# define __has_declspec_attribute(__x) 0
22#endif
23
24// Attributes relevant for layout ABI
25// ----------------------------------
26
27#if __has_cpp_attribute(msvc::no_unique_address)
28// MSVC implements [[no_unique_address]] as a silent no-op currently.
29// (If/when MSVC breaks its C++ ABI, it will be changed to work as intended.)
30// However, MSVC implements [[msvc::no_unique_address]] which does what
31// [[no_unique_address]] is supposed to do, in general.
32# define _LIBCPP_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
33#else
34# define _LIBCPP_NO_UNIQUE_ADDRESS [[__no_unique_address__]]
35#endif
36
37#define _LIBCPP_PACKED __attribute__((__packed__))
38
39// Attributes affecting overload resolution
40// ----------------------------------------
41
42#if __has_attribute(__enable_if__)
43# define _LIBCPP_PREFERRED_OVERLOAD __attribute__((__enable_if__(true, "")))
44#endif
45
46// Visibility attributes
47// ---------------------
48
49#if defined(_LIBCPP_OBJECT_FORMAT_COFF)
50
51# ifdef _DLL
52# define _LIBCPP_CRT_FUNC __declspec(dllimport)
53# else
54# define _LIBCPP_CRT_FUNC
55# endif
56
57# if defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS) || (defined(__MINGW32__) && !defined(_LIBCPP_BUILDING_LIBRARY))
58# define _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS
59# define _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS
60# define _LIBCPP_OVERRIDABLE_FUNC_VIS
61# define _LIBCPP_EXPORTED_FROM_ABI
62# elif defined(_LIBCPP_BUILDING_LIBRARY)
63# if defined(__MINGW32__)
64# define _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __declspec(dllexport)
65# define _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS
66# else
67# define _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS
68# define _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __declspec(dllexport)
69# endif
70# define _LIBCPP_OVERRIDABLE_FUNC_VIS __declspec(dllexport)
71# define _LIBCPP_EXPORTED_FROM_ABI __declspec(dllexport)
72# else
73# define _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __declspec(dllimport)
74# define _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS
75# define _LIBCPP_OVERRIDABLE_FUNC_VIS
76# define _LIBCPP_EXPORTED_FROM_ABI __declspec(dllimport)
77# endif
78
79# define _LIBCPP_HIDDEN
80# define _LIBCPP_TEMPLATE_DATA_VIS
81# define _LIBCPP_NAMESPACE_VISIBILITY
82
83#else
84
85# if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
86# define _LIBCPP_VISIBILITY(vis) __attribute__((__visibility__(vis)))
87# else
88# define _LIBCPP_VISIBILITY(vis)
89# endif
90
91# define _LIBCPP_HIDDEN _LIBCPP_VISIBILITY("hidden")
92# define _LIBCPP_TEMPLATE_DATA_VIS _LIBCPP_VISIBILITY("default")
93# define _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_VISIBILITY("default")
94# define _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS _LIBCPP_VISIBILITY("default")
95# define _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS
96
97// TODO: Make this a proper customization point or remove the option to override it.
98# ifndef _LIBCPP_OVERRIDABLE_FUNC_VIS
99# define _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_VISIBILITY("default")
100# endif
101
102# if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS) && __has_attribute(__type_visibility__)
103# define _LIBCPP_NAMESPACE_VISIBILITY __attribute__((__type_visibility__("default")))
104# elif !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
105# define _LIBCPP_NAMESPACE_VISIBILITY __attribute__((__visibility__("default")))
106# else
107# define _LIBCPP_NAMESPACE_VISIBILITY
108# endif
109
110#endif // defined(_LIBCPP_OBJECT_FORMAT_COFF)
111
112// hide_from_abi
113// -------------
114
115#define _LIBCPP_ALWAYS_INLINE __attribute__((__always_inline__))
116
117#if __has_attribute(exclude_from_explicit_instantiation)
118# define _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION __attribute__((__exclude_from_explicit_instantiation__))
119#else
120// Try to approximate the effect of exclude_from_explicit_instantiation
121// (which is that entities are not assumed to be provided by explicit
122// template instantiations in the dylib) by always inlining those entities.
123# define _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION _LIBCPP_ALWAYS_INLINE
124#endif
125
126#if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_FAST
127# define _LIBCPP_HARDENING_SIG f
128#elif _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_EXTENSIVE
129# define _LIBCPP_HARDENING_SIG s
130#elif _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
131# define _LIBCPP_HARDENING_SIG d
132#else
133# define _LIBCPP_HARDENING_SIG n // "none"
134#endif
135
136#if _LIBCPP_ASSERTION_SEMANTIC == _LIBCPP_ASSERTION_SEMANTIC_OBSERVE
137# define _LIBCPP_ASSERTION_SEMANTIC_SIG o
138#elif _LIBCPP_ASSERTION_SEMANTIC == _LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE
139# define _LIBCPP_ASSERTION_SEMANTIC_SIG q
140#elif _LIBCPP_ASSERTION_SEMANTIC == _LIBCPP_ASSERTION_SEMANTIC_ENFORCE
141# define _LIBCPP_ASSERTION_SEMANTIC_SIG e
142#else
143# define _LIBCPP_ASSERTION_SEMANTIC_SIG i // `ignore`
144#endif
145
146#if !_LIBCPP_HAS_EXCEPTIONS
147# define _LIBCPP_EXCEPTIONS_SIG n
148#else
149# define _LIBCPP_EXCEPTIONS_SIG e
150#endif
151
152#define _LIBCPP_ODR_SIGNATURE \
153 _LIBCPP_CONCAT( \
154 _LIBCPP_CONCAT(_LIBCPP_CONCAT(_LIBCPP_HARDENING_SIG, _LIBCPP_ASSERTION_SEMANTIC_SIG), _LIBCPP_EXCEPTIONS_SIG), \
155 _LIBCPP_VERSION)
156
157// This macro marks a symbol as being hidden from libc++'s ABI. This is achieved
158// on two levels:
159// 1. The symbol is given hidden visibility, which ensures that users won't start exporting
160// symbols from their dynamic library by means of using the libc++ headers. This ensures
161// that those symbols stay private to the dynamic library in which it is defined.
162//
163// 2. The symbol is given an ABI tag that encodes the ODR-relevant properties of the library.
164// This ensures that no ODR violation can arise from mixing two TUs compiled with different
165// versions or configurations of libc++ (such as exceptions vs no-exceptions). Indeed, if the
166// program contains two definitions of a function, the ODR requires them to be token-by-token
167// equivalent, and the linker is allowed to pick either definition and discard the other one.
168//
169// For example, if a program contains a copy of `vector::at()` compiled with exceptions enabled
170// *and* a copy of `vector::at()` compiled with exceptions disabled (by means of having two TUs
171// compiled with different settings), the two definitions are both visible by the linker and they
172// have the same name, but they have a meaningfully different implementation (one throws an exception
173// and the other aborts the program). This violates the ODR and makes the program ill-formed, and in
174// practice what will happen is that the linker will pick one of the definitions at random and will
175// discard the other one. This can quite clearly lead to incorrect program behavior.
176//
177// A similar reasoning holds for many other properties that are ODR-affecting. Essentially any
178// property that causes the code of a function to differ from the code in another configuration
179// can be considered ODR-affecting. In practice, we don't encode all such properties in the ABI
180// tag, but we encode the ones that we think are most important: library version, exceptions, and
181// hardening mode.
182//
183// Note that historically, solving this problem has been achieved in various ways, including
184// force-inlining all functions or giving internal linkage to all functions. Both these previous
185// solutions suffer from drawbacks that lead notably to code bloat.
186//
187// Note that we use _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION to ensure that we don't depend
188// on _LIBCPP_HIDE_FROM_ABI methods of classes explicitly instantiated in the dynamic library.
189//
190// Also note that the _LIBCPP_HIDE_FROM_ABI_VIRTUAL macro should be used on virtual functions
191// instead of _LIBCPP_HIDE_FROM_ABI. That macro does not use an ABI tag. Indeed, the mangled
192// name of a virtual function is part of its ABI, since some architectures like arm64e can sign
193// the virtual function pointer in the vtable based on the mangled name of the function. Since
194// we use an ABI tag that changes with each released version, the mangled name of the virtual
195// function would change, which is incorrect. Note that it doesn't make much sense to change
196// the implementation of a virtual function in an ABI-incompatible way in the first place,
197// since that would be an ABI break anyway. Hence, the lack of ABI tag should not be noticeable.
198//
199// The macro can be applied to record and enum types. When the tagged type is nested in
200// a record this "parent" record needs to have the macro too. Another use case for applying
201// this macro to records and unions is to apply an ABI tag to inline constexpr variables.
202// This can be useful for inline variables that are implementation details which are expected
203// to change in the future.
204//
205// TODO: We provide a escape hatch with _LIBCPP_NO_ABI_TAG for folks who want to avoid increasing
206// the length of symbols with an ABI tag. In practice, we should remove the escape hatch and
207// use compression mangling instead, see https://github.com/itanium-cxx-abi/cxx-abi/issues/70.
208#ifndef _LIBCPP_NO_ABI_TAG
209# define _LIBCPP_HIDE_FROM_ABI \
210 _LIBCPP_HIDDEN _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION \
211 __attribute__((__abi_tag__(_LIBCPP_TOSTRING(_LIBCPP_ODR_SIGNATURE))))
212#else
213# define _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDDEN _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION
214#endif
215#define _LIBCPP_HIDE_FROM_ABI_VIRTUAL _LIBCPP_HIDDEN _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION
216
217// Optional attributes
218// -------------------
219
220// these are useful for a better QoI, but not required to be available
221
222#define _LIBCPP_NOALIAS __attribute__((__malloc__))
223#define _LIBCPP_NODEBUG [[__gnu__::__nodebug__]]
224#define _LIBCPP_NO_SANITIZE(...) __attribute__((__no_sanitize__(__VA_ARGS__)))
225#define _LIBCPP_INIT_PRIORITY_MAX __attribute__((__init_priority__(100)))
226#define _LIBCPP_ATTRIBUTE_FORMAT(archetype, format_string_index, first_format_arg_index) \
227 __attribute__((__format__(archetype, format_string_index, first_format_arg_index)))
228
229#if __has_attribute(__no_sanitize__) && !defined(_LIBCPP_COMPILER_GCC)
230# define _LIBCPP_NO_CFI __attribute__((__no_sanitize__("cfi")))
231#else
232# define _LIBCPP_NO_CFI
233#endif
234
235#if __has_attribute(__using_if_exists__)
236# define _LIBCPP_USING_IF_EXISTS __attribute__((__using_if_exists__))
237#else
238# define _LIBCPP_USING_IF_EXISTS
239#endif
240
241#if __has_cpp_attribute(_Clang::__no_destroy__)
242# define _LIBCPP_NO_DESTROY [[_Clang::__no_destroy__]]
243#else
244# define _LIBCPP_NO_DESTROY
245#endif
246
247#if __has_attribute(__diagnose_if__)
248# define _LIBCPP_DIAGNOSE_WARNING(...) __attribute__((__diagnose_if__(__VA_ARGS__, "warning")))
249#else
250# define _LIBCPP_DIAGNOSE_WARNING(...)
251#endif
252
253#if __has_attribute(__diagnose_if__) && !defined(_LIBCPP_APPLE_CLANG_VER) && \
254 (!defined(_LIBCPP_CLANG_VER) || _LIBCPP_CLANG_VER >= 2001)
255# define _LIBCPP_DIAGNOSE_IF(...) __attribute__((__diagnose_if__(__VA_ARGS__)))
256#else
257# define _LIBCPP_DIAGNOSE_IF(...)
258#endif
259
260#define _LIBCPP_DIAGNOSE_NULLPTR_IF(condition, condition_description) \
261 _LIBCPP_DIAGNOSE_IF( \
262 condition, \
263 "null passed to callee that requires a non-null argument" condition_description, \
264 "warning", \
265 "nonnull")
266
267#if __has_cpp_attribute(_Clang::__lifetimebound__)
268# define _LIBCPP_LIFETIMEBOUND [[_Clang::__lifetimebound__]]
269#else
270# define _LIBCPP_LIFETIMEBOUND
271#endif
272
273// This is to work around https://llvm.org/PR156809
274#ifndef _LIBCPP_CXX03_LANG
275# define _LIBCPP_CTOR_LIFETIMEBOUND _LIBCPP_LIFETIMEBOUND
276#else
277# define _LIBCPP_CTOR_LIFETIMEBOUND
278#endif
279
280#if __has_cpp_attribute(_Clang::__noescape__)
281# define _LIBCPP_NOESCAPE [[_Clang::__noescape__]]
282#else
283# define _LIBCPP_NOESCAPE
284#endif
285
286#if __has_cpp_attribute(_Clang::__no_specializations__)
287# define _LIBCPP_NO_SPECIALIZATIONS \
288 [[_Clang::__no_specializations__("Users are not allowed to specialize this standard library entity")]]
289#else
290# define _LIBCPP_NO_SPECIALIZATIONS
291#endif
292
293#if __has_cpp_attribute(_Clang::__preferred_name__)
294# define _LIBCPP_PREFERRED_NAME(x) [[_Clang::__preferred_name__(x)]]
295#else
296# define _LIBCPP_PREFERRED_NAME(x)
297#endif
298
299#if __has_cpp_attribute(_Clang::__scoped_lockable__)
300# define _LIBCPP_SCOPED_LOCKABLE [[_Clang::__scoped_lockable__]]
301#else
302# define _LIBCPP_SCOPED_LOCKABLE
303#endif
304
305#if __has_cpp_attribute(_Clang::__capability__)
306# define _LIBCPP_CAPABILITY(...) [[_Clang::__capability__(__VA_ARGS__)]]
307#else
308# define _LIBCPP_CAPABILITY(...)
309#endif
310
311#if __has_attribute(__acquire_capability__)
312# define _LIBCPP_ACQUIRE_CAPABILITY(...) __attribute__((__acquire_capability__(__VA_ARGS__)))
313#else
314# define _LIBCPP_ACQUIRE_CAPABILITY(...)
315#endif
316
317#if __has_cpp_attribute(_Clang::__try_acquire_capability__)
318# define _LIBCPP_TRY_ACQUIRE_CAPABILITY(...) [[_Clang::__try_acquire_capability__(__VA_ARGS__)]]
319#else
320# define _LIBCPP_TRY_ACQUIRE_CAPABILITY(...)
321#endif
322
323#if __has_cpp_attribute(_Clang::__acquire_shared_capability__)
324# define _LIBCPP_ACQUIRE_SHARED_CAPABILITY [[_Clang::__acquire_shared_capability__]]
325#else
326# define _LIBCPP_ACQUIRE_SHARED_CAPABILITY
327#endif
328
329#if __has_cpp_attribute(_Clang::__try_acquire_shared_capability__)
330# define _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(...) [[_Clang::__try_acquire_shared_capability__(__VA_ARGS__)]]
331#else
332# define _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(...)
333#endif
334
335#if __has_cpp_attribute(_Clang::__release_capability__)
336# define _LIBCPP_RELEASE_CAPABILITY [[_Clang::__release_capability__]]
337#else
338# define _LIBCPP_RELEASE_CAPABILITY
339#endif
340
341#if __has_cpp_attribute(_Clang::__release_shared_capability__)
342# define _LIBCPP_RELEASE_SHARED_CAPABILITY [[_Clang::__release_shared_capability__]]
343#else
344# define _LIBCPP_RELEASE_SHARED_CAPABILITY
345#endif
346
347#if __has_attribute(__requires_capability__)
348# define _LIBCPP_REQUIRES_CAPABILITY(...) __attribute__((__requires_capability__(__VA_ARGS__)))
349#else
350# define _LIBCPP_REQUIRES_CAPABILITY(...)
351#endif
352
353#if __has_cpp_attribute(_Clang::__no_thread_safety_analysis__)
354# define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS [[_Clang::__no_thread_safety_analysis__]]
355#else
356# define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
357#endif
358
359#if defined(_LIBCPP_ABI_MICROSOFT) && __has_declspec_attribute(empty_bases)
360# define _LIBCPP_DECLSPEC_EMPTY_BASES __declspec(empty_bases)
361#else
362# define _LIBCPP_DECLSPEC_EMPTY_BASES
363#endif
364
365// Allow for build-time disabling of unsigned integer sanitization
366#if __has_attribute(no_sanitize) && !defined(_LIBCPP_COMPILER_GCC)
367# define _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK __attribute__((__no_sanitize__("unsigned-integer-overflow")))
368#else
369# define _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
370#endif
371
372#if __has_feature(nullability)
373# define _LIBCPP_DIAGNOSE_NULLPTR _Nonnull
374#else
375# define _LIBCPP_DIAGNOSE_NULLPTR
376#endif
377
378#if defined(__CUDACC__) || defined(__CUDA_ARCH__) || defined(__CUDA_LIBDEVICE__)
379// The CUDA SDK contains an unfortunate definition for the __noinline__ macro,
380// which breaks the regular __attribute__((__noinline__)) syntax. Therefore,
381// when compiling for CUDA we use the non-underscored version of the noinline
382// attribute.
383//
384// This is a temporary workaround and we still expect the CUDA SDK team to solve
385// this issue properly in the SDK headers.
386//
387// See https://github.com/llvm/llvm-project/pull/73838 for more details.
388# define _LIBCPP_NOINLINE __attribute__((noinline))
389#elif __has_attribute(__noinline__)
390# define _LIBCPP_NOINLINE __attribute__((__noinline__))
391#else
392# define _LIBCPP_NOINLINE
393#endif
394
395// Deprecation macros
396// ------------------
397
398// Deprecations warnings are always enabled, except when users explicitly opt-out
399// by defining _LIBCPP_DISABLE_DEPRECATION_WARNINGS.
400#if !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS)
401# if __has_attribute(__deprecated__)
402# define _LIBCPP_DEPRECATED __attribute__((__deprecated__))
403# define _LIBCPP_DEPRECATED_(m) __attribute__((__deprecated__(m)))
404# elif _LIBCPP_STD_VER >= 14
405# define _LIBCPP_DEPRECATED [[deprecated]]
406# define _LIBCPP_DEPRECATED_(m) [[deprecated(m)]]
407# else
408# define _LIBCPP_DEPRECATED
409# define _LIBCPP_DEPRECATED_(m)
410# endif
411#else
412# define _LIBCPP_DEPRECATED
413# define _LIBCPP_DEPRECATED_(m)
414#endif
415
416#if !defined(_LIBCPP_CXX03_LANG)
417# define _LIBCPP_DEPRECATED_IN_CXX11 _LIBCPP_DEPRECATED
418#else
419# define _LIBCPP_DEPRECATED_IN_CXX11
420#endif
421
422#if _LIBCPP_STD_VER >= 14
423# define _LIBCPP_DEPRECATED_IN_CXX14 _LIBCPP_DEPRECATED
424#else
425# define _LIBCPP_DEPRECATED_IN_CXX14
426#endif
427
428#if _LIBCPP_STD_VER >= 17
429# define _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_DEPRECATED
430#else
431# define _LIBCPP_DEPRECATED_IN_CXX17
432#endif
433
434#if _LIBCPP_STD_VER >= 20
435# define _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_DEPRECATED
436#else
437# define _LIBCPP_DEPRECATED_IN_CXX20
438#endif
439
440#if _LIBCPP_STD_VER >= 23
441# define _LIBCPP_DEPRECATED_IN_CXX23 _LIBCPP_DEPRECATED
442#else
443# define _LIBCPP_DEPRECATED_IN_CXX23
444#endif
445
446#if _LIBCPP_STD_VER >= 26
447# define _LIBCPP_DEPRECATED_IN_CXX26 _LIBCPP_DEPRECATED
448# define _LIBCPP_DEPRECATED_IN_CXX26_(m) _LIBCPP_DEPRECATED_(m)
449#else
450# define _LIBCPP_DEPRECATED_IN_CXX26
451# define _LIBCPP_DEPRECATED_IN_CXX26_(m)
452#endif
453
454#if _LIBCPP_HAS_CHAR8_T
455# define _LIBCPP_DEPRECATED_WITH_CHAR8_T _LIBCPP_DEPRECATED
456#else
457# define _LIBCPP_DEPRECATED_WITH_CHAR8_T
458#endif
459
460#endif // _LIBCPP___CONFIGURATION_ATTRIBUTES_H
461