__availability 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 std::__libcpp_assertion_handler default
  139. // assertion handler is provided by the library.
  140. //
  141. // Note that when users provide their own custom assertion handler,
  142. // it doesn't matter whether the dylib provides a default handler,
  143. // and the availability markup can actually give a false positive
  144. // diagnostic (it will think that no handler is provided, when in
  145. // reality the user has provided their own).
  146. //
  147. // Users can pass -D_LIBCPP_AVAILABILITY_CUSTOM_ASSERTION_HANDLER_PROVIDED
  148. // to the compiler to tell the library to ignore the fact that the
  149. // default handler isn't available on their deployment target. Note that
  150. // defining this macro but failing to define a custom assertion handler
  151. // will lead to a load-time error on back-deployment targets, so it
  152. // should be avoided.
  153. # define _LIBCPP_AVAILABILITY_DEFAULT_ASSERTION_HANDLER
  154. #elif defined(__APPLE__)
  155. # define _LIBCPP_AVAILABILITY_SHARED_MUTEX \
  156. __attribute__((availability(macos,strict,introduced=10.12))) \
  157. __attribute__((availability(ios,strict,introduced=10.0))) \
  158. __attribute__((availability(tvos,strict,introduced=10.0))) \
  159. __attribute__((availability(watchos,strict,introduced=3.0)))
  160. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) || \
  161. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \
  162. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) || \
  163. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000)
  164. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_mutex
  165. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_timed_mutex
  166. # endif
  167. // Note: bad_optional_access & friends were not introduced in the matching
  168. // macOS and iOS versions, so the version mismatch between macOS and others
  169. // is intended.
  170. # define _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS \
  171. __attribute__((availability(macos,strict,introduced=10.13))) \
  172. __attribute__((availability(ios,strict,introduced=12.0))) \
  173. __attribute__((availability(tvos,strict,introduced=12.0))) \
  174. __attribute__((availability(watchos,strict,introduced=5.0)))
  175. # define _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS \
  176. _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  177. # define _LIBCPP_AVAILABILITY_BAD_ANY_CAST \
  178. _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  179. # define _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS \
  180. __attribute__((availability(macos,strict,introduced=10.12))) \
  181. __attribute__((availability(ios,strict,introduced=10.0))) \
  182. __attribute__((availability(tvos,strict,introduced=10.0))) \
  183. __attribute__((availability(watchos,strict,introduced=3.0)))
  184. # define _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE \
  185. __attribute__((availability(macos,strict,introduced=10.12))) \
  186. __attribute__((availability(ios,strict,introduced=10.0))) \
  187. __attribute__((availability(tvos,strict,introduced=10.0))) \
  188. __attribute__((availability(watchos,strict,introduced=3.0)))
  189. # define _LIBCPP_AVAILABILITY_FUTURE_ERROR \
  190. __attribute__((availability(ios,strict,introduced=6.0)))
  191. # define _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE \
  192. __attribute__((availability(macos,strict,introduced=10.9))) \
  193. __attribute__((availability(ios,strict,introduced=7.0)))
  194. # define _LIBCPP_AVAILABILITY_LOCALE_CATEGORY \
  195. __attribute__((availability(macos,strict,introduced=10.9))) \
  196. __attribute__((availability(ios,strict,introduced=7.0)))
  197. # define _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR \
  198. __attribute__((availability(macos,strict,introduced=10.9))) \
  199. __attribute__((availability(ios,strict,introduced=7.0)))
  200. # define _LIBCPP_AVAILABILITY_FILESYSTEM \
  201. __attribute__((availability(macos,strict,introduced=10.15))) \
  202. __attribute__((availability(ios,strict,introduced=13.0))) \
  203. __attribute__((availability(tvos,strict,introduced=13.0))) \
  204. __attribute__((availability(watchos,strict,introduced=6.0)))
  205. # define _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH \
  206. _Pragma("clang attribute push(__attribute__((availability(macos,strict,introduced=10.15))), apply_to=any(function,record))") \
  207. _Pragma("clang attribute push(__attribute__((availability(ios,strict,introduced=13.0))), apply_to=any(function,record))") \
  208. _Pragma("clang attribute push(__attribute__((availability(tvos,strict,introduced=13.0))), apply_to=any(function,record))") \
  209. _Pragma("clang attribute push(__attribute__((availability(watchos,strict,introduced=6.0))), apply_to=any(function,record))")
  210. # define _LIBCPP_AVAILABILITY_FILESYSTEM_POP \
  211. _Pragma("clang attribute pop") \
  212. _Pragma("clang attribute pop") \
  213. _Pragma("clang attribute pop") \
  214. _Pragma("clang attribute pop")
  215. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101500) || \
  216. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 130000) || \
  217. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 130000) || \
  218. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 60000)
  219. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_filesystem
  220. # endif
  221. # define _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT \
  222. __attribute__((unavailable))
  223. # define _LIBCPP_AVAILABILITY_SYNC \
  224. __attribute__((availability(macos,strict,introduced=11.0))) \
  225. __attribute__((availability(ios,strict,introduced=14.0))) \
  226. __attribute__((availability(tvos,strict,introduced=14.0))) \
  227. __attribute__((availability(watchos,strict,introduced=7.0)))
  228. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 110000) || \
  229. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 140000) || \
  230. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 140000) || \
  231. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 70000)
  232. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_atomic_wait
  233. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_barrier
  234. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_latch
  235. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_semaphore
  236. # endif
  237. # define _LIBCPP_AVAILABILITY_FORMAT \
  238. __attribute__((unavailable))
  239. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_format
  240. # define _LIBCPP_AVAILABILITY_DEFAULT_ASSERTION_HANDLER \
  241. __attribute__((unavailable))
  242. #else
  243. // ...New vendors can add availability markup here...
  244. # error "It looks like you're trying to enable vendor availability markup, but you haven't defined the corresponding macros yet!"
  245. #endif
  246. // Define availability attributes that depend on _LIBCPP_NO_EXCEPTIONS.
  247. // Those are defined in terms of the availability attributes above, and
  248. // should not be vendor-specific.
  249. #if defined(_LIBCPP_NO_EXCEPTIONS)
  250. # define _LIBCPP_AVAILABILITY_FUTURE
  251. # define _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
  252. # define _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
  253. # define _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
  254. #else
  255. # define _LIBCPP_AVAILABILITY_FUTURE _LIBCPP_AVAILABILITY_FUTURE_ERROR
  256. # define _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST _LIBCPP_AVAILABILITY_BAD_ANY_CAST
  257. # define _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  258. # define _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS
  259. #endif
  260. // Define the special assertion handler availability attribute, which can be silenced by
  261. // users if they provide their own custom assertion handler. The rest of the code should
  262. // not use the *_DEFAULT_* macro directly, since that would make it ignore the fact that
  263. // the user provided a custom handler.
  264. #if defined(_LIBCPP_AVAILABILITY_CUSTOM_ASSERTION_HANDLER_PROVIDED)
  265. # define _LIBCPP_AVAILABILITY_ASSERTION_HANDLER /* nothing */
  266. #else
  267. # define _LIBCPP_AVAILABILITY_ASSERTION_HANDLER _LIBCPP_AVAILABILITY_DEFAULT_ASSERTION_HANDLER
  268. #endif
  269. #endif // _LIBCPP___AVAILABILITY