__availability 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #ifndef _LIBCPP___AVAILABILITY
  10. #define _LIBCPP___AVAILABILITY
  11. #include <__config>
  12. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  13. # pragma GCC system_header
  14. #endif
  15. // Libc++ is shipped by various vendors. In particular, it is used as a system
  16. // library on macOS, iOS and other Apple platforms. In order for users to be
  17. // able to compile a binary that is intended to be deployed to an older version
  18. // of a platform, Clang provides availability attributes [1]. These attributes
  19. // can be placed on declarations and are used to describe the life cycle of a
  20. // symbol in the library.
  21. //
  22. // The main goal is to ensure a compile-time error if a symbol that hasn't been
  23. // introduced in a previously released library is used in a program that targets
  24. // that previously released library. Normally, this would be a load-time error
  25. // when one tries to launch the program against the older library.
  26. //
  27. // For example, the filesystem library was introduced in the dylib in macOS 10.15.
  28. // If a user compiles on a macOS 10.15 host but targets macOS 10.13 with their
  29. // program, the compiler would normally not complain (because the required
  30. // declarations are in the headers), but the dynamic loader would fail to find
  31. // the symbols when actually trying to launch the program on macOS 10.13. To
  32. // turn this into a compile-time issue instead, declarations are annotated with
  33. // when they were introduced, and the compiler can produce a diagnostic if the
  34. // program references something that isn't available on the deployment target.
  35. //
  36. // This mechanism is general in nature, and any vendor can add their markup to
  37. // the library (see below). Whenever a new feature is added that requires support
  38. // in the shared library, a macro should be added below to mark this feature
  39. // as unavailable. When vendors decide to ship the feature as part of their
  40. // shared library, they can update the markup appropriately.
  41. //
  42. // Furthermore, many features in the standard library have corresponding
  43. // feature-test macros. When a feature is made unavailable on some deployment
  44. // target, a macro should be defined to signal that it is unavailable. That
  45. // macro can then be picked up when feature-test macros are generated (see
  46. // generate_feature_test_macro_components.py) to make sure that feature-test
  47. // macros don't announce a feature as being implemented if it has been marked
  48. // as unavailable.
  49. //
  50. // Note that this mechanism is disabled by default in the "upstream" libc++.
  51. // Availability annotations are only meaningful when shipping libc++ inside
  52. // a platform (i.e. as a system library), and so vendors that want them should
  53. // turn those annotations on at CMake configuration time.
  54. //
  55. // [1]: https://clang.llvm.org/docs/AttributeReference.html#availability
  56. // For backwards compatibility, allow users to define _LIBCPP_DISABLE_AVAILABILITY
  57. // for a while.
  58. #if defined(_LIBCPP_DISABLE_AVAILABILITY)
  59. # if !defined(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
  60. # define _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS
  61. # endif
  62. #endif
  63. // Availability markup is disabled when building the library, or when the compiler
  64. // doesn't support the proper attributes.
  65. #if defined(_LIBCPP_BUILDING_LIBRARY) || \
  66. defined(_LIBCXXABI_BUILDING_LIBRARY) || \
  67. !__has_feature(attribute_availability_with_strict) || \
  68. !__has_feature(attribute_availability_in_templates) || \
  69. !__has_extension(pragma_clang_attribute_external_declaration)
  70. # if !defined(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
  71. # define _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS
  72. # endif
  73. #endif
  74. #if defined(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
  75. // This controls the availability of std::shared_mutex and std::shared_timed_mutex,
  76. // which were added to the dylib later.
  77. # define _LIBCPP_AVAILABILITY_SHARED_MUTEX
  78. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_mutex
  79. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_timed_mutex
  80. // These macros control the availability of std::bad_optional_access and
  81. // other exception types. These were put in the shared library to prevent
  82. // code bloat from every user program defining the vtable for these exception
  83. // types.
  84. //
  85. // Note that when exceptions are disabled, the methods that normally throw
  86. // these exceptions can be used even on older deployment targets, but those
  87. // methods will abort instead of throwing.
  88. # define _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  89. # define _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS
  90. # define _LIBCPP_AVAILABILITY_BAD_ANY_CAST
  91. // This controls the availability of std::uncaught_exceptions().
  92. # define _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS
  93. // This controls the availability of the sized version of ::operator delete,
  94. // ::operator delete[], and their align_val_t variants, which were all added
  95. // in C++17, and hence not present in early dylibs.
  96. # define _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE
  97. // This controls the availability of the std::future_error exception.
  98. //
  99. // Note that when exceptions are disabled, the methods that normally throw
  100. // std::future_error can be used even on older deployment targets, but those
  101. // methods will abort instead of throwing.
  102. # define _LIBCPP_AVAILABILITY_FUTURE_ERROR
  103. // This controls the availability of std::type_info's vtable.
  104. // I can't imagine how using std::type_info can work at all if
  105. // this isn't supported.
  106. # define _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
  107. // This controls the availability of std::locale::category members
  108. // (e.g. std::locale::collate), which are defined in the dylib.
  109. # define _LIBCPP_AVAILABILITY_LOCALE_CATEGORY
  110. // This controls the availability of atomic operations on std::shared_ptr
  111. // (e.g. `std::atomic_store(std::shared_ptr)`), which require a shared
  112. // lock table located in the dylib.
  113. # define _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  114. // These macros control the availability of all parts of <filesystem> that
  115. // depend on something in the dylib.
  116. # define _LIBCPP_AVAILABILITY_FILESYSTEM
  117. # define _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
  118. # define _LIBCPP_AVAILABILITY_FILESYSTEM_POP
  119. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_filesystem
  120. // This controls the availability of floating-point std::to_chars functions.
  121. // These overloads were added later than the integer overloads.
  122. # define _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT
  123. // This controls the availability of the C++20 synchronization library,
  124. // which requires shared library support for various operations
  125. // (see libcxx/src/atomic.cpp). This includes <barier>, <latch>,
  126. // <semaphore>, and notification functions on std::atomic.
  127. # define _LIBCPP_AVAILABILITY_SYNC
  128. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_atomic_wait
  129. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_barrier
  130. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_latch
  131. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_semaphore
  132. // This controls the availability of the C++20 format library.
  133. // The library is in development and not ABI stable yet. P2216 is
  134. // retroactively accepted in C++20. This paper contains ABI breaking
  135. // changes.
  136. # define _LIBCPP_AVAILABILITY_FORMAT
  137. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_format
  138. // This controls whether the library claims to provide a default verbose
  139. // termination function, and consequently whether the headers will try
  140. // to use it when the mechanism isn't overriden at compile-time.
  141. // # define _LIBCPP_HAS_NO_VERBOSE_ABORT_IN_LIBRARY
  142. #elif defined(__APPLE__)
  143. # define _LIBCPP_AVAILABILITY_SHARED_MUTEX \
  144. __attribute__((availability(macos,strict,introduced=10.12))) \
  145. __attribute__((availability(ios,strict,introduced=10.0))) \
  146. __attribute__((availability(tvos,strict,introduced=10.0))) \
  147. __attribute__((availability(watchos,strict,introduced=3.0)))
  148. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) || \
  149. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \
  150. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) || \
  151. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000)
  152. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_mutex
  153. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_timed_mutex
  154. # endif
  155. // Note: bad_optional_access & friends were not introduced in the matching
  156. // macOS and iOS versions, so the version mismatch between macOS and others
  157. // is intended.
  158. # define _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS \
  159. __attribute__((availability(macos,strict,introduced=10.13))) \
  160. __attribute__((availability(ios,strict,introduced=12.0))) \
  161. __attribute__((availability(tvos,strict,introduced=12.0))) \
  162. __attribute__((availability(watchos,strict,introduced=5.0)))
  163. # define _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS \
  164. _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  165. # define _LIBCPP_AVAILABILITY_BAD_ANY_CAST \
  166. _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  167. # define _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS \
  168. __attribute__((availability(macos,strict,introduced=10.12))) \
  169. __attribute__((availability(ios,strict,introduced=10.0))) \
  170. __attribute__((availability(tvos,strict,introduced=10.0))) \
  171. __attribute__((availability(watchos,strict,introduced=3.0)))
  172. # define _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE \
  173. __attribute__((availability(macos,strict,introduced=10.12))) \
  174. __attribute__((availability(ios,strict,introduced=10.0))) \
  175. __attribute__((availability(tvos,strict,introduced=10.0))) \
  176. __attribute__((availability(watchos,strict,introduced=3.0)))
  177. # define _LIBCPP_AVAILABILITY_FUTURE_ERROR \
  178. __attribute__((availability(ios,strict,introduced=6.0)))
  179. # define _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE \
  180. __attribute__((availability(macos,strict,introduced=10.9))) \
  181. __attribute__((availability(ios,strict,introduced=7.0)))
  182. # define _LIBCPP_AVAILABILITY_LOCALE_CATEGORY \
  183. __attribute__((availability(macos,strict,introduced=10.9))) \
  184. __attribute__((availability(ios,strict,introduced=7.0)))
  185. # define _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR \
  186. __attribute__((availability(macos,strict,introduced=10.9))) \
  187. __attribute__((availability(ios,strict,introduced=7.0)))
  188. # define _LIBCPP_AVAILABILITY_FILESYSTEM \
  189. __attribute__((availability(macos,strict,introduced=10.15))) \
  190. __attribute__((availability(ios,strict,introduced=13.0))) \
  191. __attribute__((availability(tvos,strict,introduced=13.0))) \
  192. __attribute__((availability(watchos,strict,introduced=6.0)))
  193. # define _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH \
  194. _Pragma("clang attribute push(__attribute__((availability(macos,strict,introduced=10.15))), apply_to=any(function,record))") \
  195. _Pragma("clang attribute push(__attribute__((availability(ios,strict,introduced=13.0))), apply_to=any(function,record))") \
  196. _Pragma("clang attribute push(__attribute__((availability(tvos,strict,introduced=13.0))), apply_to=any(function,record))") \
  197. _Pragma("clang attribute push(__attribute__((availability(watchos,strict,introduced=6.0))), apply_to=any(function,record))")
  198. # define _LIBCPP_AVAILABILITY_FILESYSTEM_POP \
  199. _Pragma("clang attribute pop") \
  200. _Pragma("clang attribute pop") \
  201. _Pragma("clang attribute pop") \
  202. _Pragma("clang attribute pop")
  203. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101500) || \
  204. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 130000) || \
  205. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 130000) || \
  206. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 60000)
  207. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_filesystem
  208. # endif
  209. # define _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT \
  210. __attribute__((unavailable))
  211. # define _LIBCPP_AVAILABILITY_SYNC \
  212. __attribute__((availability(macos,strict,introduced=11.0))) \
  213. __attribute__((availability(ios,strict,introduced=14.0))) \
  214. __attribute__((availability(tvos,strict,introduced=14.0))) \
  215. __attribute__((availability(watchos,strict,introduced=7.0)))
  216. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 110000) || \
  217. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 140000) || \
  218. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 140000) || \
  219. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 70000)
  220. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_atomic_wait
  221. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_barrier
  222. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_latch
  223. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_semaphore
  224. # endif
  225. # define _LIBCPP_AVAILABILITY_FORMAT \
  226. __attribute__((unavailable))
  227. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_format
  228. # define _LIBCPP_HAS_NO_VERBOSE_ABORT_IN_LIBRARY
  229. #else
  230. // ...New vendors can add availability markup here...
  231. # error "It looks like you're trying to enable vendor availability markup, but you haven't defined the corresponding macros yet!"
  232. #endif
  233. // Define availability attributes that depend on _LIBCPP_NO_EXCEPTIONS.
  234. // Those are defined in terms of the availability attributes above, and
  235. // should not be vendor-specific.
  236. #if defined(_LIBCPP_NO_EXCEPTIONS)
  237. # define _LIBCPP_AVAILABILITY_FUTURE
  238. # define _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
  239. # define _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
  240. # define _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
  241. #else
  242. # define _LIBCPP_AVAILABILITY_FUTURE _LIBCPP_AVAILABILITY_FUTURE_ERROR
  243. # define _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST _LIBCPP_AVAILABILITY_BAD_ANY_CAST
  244. # define _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  245. # define _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS
  246. #endif
  247. #endif // _LIBCPP___AVAILABILITY