__availability 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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, two macros are added below to allow marking the feature
  39. // as unavailable:
  40. // 1. A macro named `_LIBCPP_AVAILABILITY_HAS_NO_<feature>` which must be defined
  41. // exactly when compiling for a target that doesn't support the feature.
  42. // 2. A macro named `_LIBCPP_AVAILABILITY_<feature>`, which must always be defined
  43. // and must expand to the proper availability attribute for the platform.
  44. //
  45. // When vendors decide to ship the feature as part of their shared library, they
  46. // can update these macros appropriately for their platform, and the library will
  47. // use those to provide an optimal user experience.
  48. //
  49. // Furthermore, many features in the standard library have corresponding
  50. // feature-test macros. The `_LIBCPP_AVAILABILITY_HAS_NO_<feature>` macros
  51. // are checked by the corresponding feature-test macros generated by
  52. // generate_feature_test_macro_components.py to ensure that the library
  53. // doesn't announce a feature as being implemented if it is unavailable on
  54. // the deployment target.
  55. //
  56. // Note that this mechanism is disabled by default in the "upstream" libc++.
  57. // Availability annotations are only meaningful when shipping libc++ inside
  58. // a platform (i.e. as a system library), and so vendors that want them should
  59. // turn those annotations on at CMake configuration time.
  60. //
  61. // [1]: https://clang.llvm.org/docs/AttributeReference.html#availability
  62. // For backwards compatibility, allow users to define _LIBCPP_DISABLE_AVAILABILITY
  63. // for a while.
  64. #if defined(_LIBCPP_DISABLE_AVAILABILITY)
  65. # if !defined(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
  66. # define _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS
  67. # endif
  68. #endif
  69. // Availability markup is disabled when building the library, or when a non-Clang
  70. // compiler is used because only Clang supports the necessary attributes.
  71. // doesn't support the proper attributes.
  72. #if defined(_LIBCPP_BUILDING_LIBRARY) || defined(_LIBCXXABI_BUILDING_LIBRARY) || !defined(_LIBCPP_COMPILER_CLANG_BASED)
  73. # if !defined(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
  74. # define _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS
  75. # endif
  76. #endif
  77. #if defined(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
  78. // These macros control the availability of std::bad_optional_access and
  79. // other exception types. These were put in the shared library to prevent
  80. // code bloat from every user program defining the vtable for these exception
  81. // types.
  82. //
  83. // Note that when exceptions are disabled, the methods that normally throw
  84. // these exceptions can be used even on older deployment targets, but those
  85. // methods will abort instead of throwing.
  86. # define _LIBCPP_AVAILABILITY_HAS_BAD_OPTIONAL_ACCESS 1
  87. # define _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  88. # define _LIBCPP_AVAILABILITY_HAS_BAD_VARIANT_ACCESS 1
  89. # define _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS
  90. # define _LIBCPP_AVAILABILITY_HAS_BAD_ANY_CAST 1
  91. # define _LIBCPP_AVAILABILITY_BAD_ANY_CAST
  92. // These macros control the availability of all parts of <filesystem> that
  93. // depend on something in the dylib.
  94. # define _LIBCPP_AVAILABILITY_HAS_FILESYSTEM_LIBRARY 1
  95. # define _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
  96. # define _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
  97. # define _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
  98. // This controls the availability of the C++20 synchronization library,
  99. // which requires shared library support for various operations
  100. // (see libcxx/src/atomic.cpp). This includes <barier>, <latch>,
  101. // <semaphore>, and notification functions on std::atomic.
  102. # define _LIBCPP_AVAILABILITY_HAS_SYNC 1
  103. # define _LIBCPP_AVAILABILITY_SYNC
  104. // Enable additional explicit instantiations of iostreams components. This
  105. // reduces the number of weak definitions generated in programs that use
  106. // iostreams by providing a single strong definition in the shared library.
  107. //
  108. // TODO: Enable additional explicit instantiations on GCC once it supports exclude_from_explicit_instantiation,
  109. // or once libc++ doesn't use the attribute anymore.
  110. // TODO: Enable them on Windows once https://llvm.org/PR41018 has been fixed.
  111. # if !defined(_LIBCPP_COMPILER_GCC) && !defined(_WIN32)
  112. # define _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1 1
  113. # else
  114. # define _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1 0
  115. # endif
  116. // This controls the availability of floating-point std::to_chars functions.
  117. // These overloads were added later than the integer overloads.
  118. # define _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT 1
  119. # define _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT
  120. // This controls whether the library claims to provide a default verbose
  121. // termination function, and consequently whether the headers will try
  122. // to use it when the mechanism isn't overriden at compile-time.
  123. # define _LIBCPP_AVAILABILITY_HAS_VERBOSE_ABORT 1
  124. # define _LIBCPP_AVAILABILITY_VERBOSE_ABORT
  125. // This controls the availability of the C++17 std::pmr library,
  126. // which is implemented in large part in the built library.
  127. # define _LIBCPP_AVAILABILITY_HAS_PMR 1
  128. # define _LIBCPP_AVAILABILITY_PMR
  129. // These macros controls the availability of __cxa_init_primary_exception
  130. // in the built library, which std::make_exception_ptr might use
  131. // (see libcxx/include/__exception/exception_ptr.h).
  132. # define _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION 1
  133. # define _LIBCPP_AVAILABILITY_INIT_PRIMARY_EXCEPTION
  134. // This controls the availability of C++23 <print>, which
  135. // has a dependency on the built library (it needs access to
  136. // the underlying buffer types of std::cout, std::cerr, and std::clog.
  137. # define _LIBCPP_AVAILABILITY_HAS_PRINT 1
  138. # define _LIBCPP_AVAILABILITY_PRINT
  139. // This controls the availability of the C++20 time zone database.
  140. // The parser code is built in the library.
  141. # define _LIBCPP_AVAILABILITY_HAS_TZDB 1
  142. # define _LIBCPP_AVAILABILITY_TZDB
  143. #elif defined(__APPLE__)
  144. # define _LIBCPP_AVAILABILITY_HAS_BAD_OPTIONAL_ACCESS \
  145. (!defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) || __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 50000)
  146. # define _LIBCPP_AVAILABILITY_HAS_BAD_VARIANT_ACCESS _LIBCPP_AVAILABILITY_HAS_BAD_OPTIONAL_ACCESS
  147. # define _LIBCPP_AVAILABILITY_HAS_BAD_ANY_CAST _LIBCPP_AVAILABILITY_HAS_BAD_OPTIONAL_ACCESS
  148. # define _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS __attribute__((availability(watchos, strict, introduced = 5.0)))
  149. # define _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  150. # define _LIBCPP_AVAILABILITY_BAD_ANY_CAST _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  151. // TODO: Update once this is released
  152. # define _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION 0
  153. # define _LIBCPP_AVAILABILITY_INIT_PRIMARY_EXCEPTION __attribute__((unavailable))
  154. // <filesystem>
  155. // clang-format off
  156. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101500) || \
  157. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 130000) || \
  158. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 130000) || \
  159. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 60000)
  160. // clang-format on
  161. # define _LIBCPP_AVAILABILITY_HAS_FILESYSTEM_LIBRARY 0
  162. # else
  163. # define _LIBCPP_AVAILABILITY_HAS_FILESYSTEM_LIBRARY 1
  164. # endif
  165. # define _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY \
  166. __attribute__((availability(macos, strict, introduced = 10.15))) \
  167. __attribute__((availability(ios, strict, introduced = 13.0))) \
  168. __attribute__((availability(tvos, strict, introduced = 13.0))) \
  169. __attribute__((availability(watchos, strict, introduced = 6.0)))
  170. // clang-format off
  171. # define _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH \
  172. _Pragma("clang attribute push(__attribute__((availability(macos,strict,introduced=10.15))), apply_to=any(function,record))") \
  173. _Pragma("clang attribute push(__attribute__((availability(ios,strict,introduced=13.0))), apply_to=any(function,record))") \
  174. _Pragma("clang attribute push(__attribute__((availability(tvos,strict,introduced=13.0))), apply_to=any(function,record))") \
  175. _Pragma("clang attribute push(__attribute__((availability(watchos,strict,introduced=6.0))), apply_to=any(function,record))")
  176. # define _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP \
  177. _Pragma("clang attribute pop") \
  178. _Pragma("clang attribute pop") \
  179. _Pragma("clang attribute pop") \
  180. _Pragma("clang attribute pop")
  181. // clang-format on
  182. // std::to_chars(floating-point)
  183. // clang-format off
  184. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 130300) || \
  185. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 160300) || \
  186. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 160300) || \
  187. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 90300)
  188. // clang-format on
  189. # define _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT 0
  190. # else
  191. # define _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT 1
  192. # endif
  193. # define _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT \
  194. __attribute__((availability(macos, strict, introduced = 13.3))) \
  195. __attribute__((availability(ios, strict, introduced = 16.3))) \
  196. __attribute__((availability(tvos, strict, introduced = 16.3))) \
  197. __attribute__((availability(watchos, strict, introduced = 9.3)))
  198. // c++20 synchronization library
  199. // clang-format off
  200. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 110000) || \
  201. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 140000) || \
  202. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 140000) || \
  203. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 70000)
  204. // clang-format on
  205. # define _LIBCPP_AVAILABILITY_HAS_SYNC 0
  206. # else
  207. # define _LIBCPP_AVAILABILITY_HAS_SYNC 1
  208. # endif
  209. # define _LIBCPP_AVAILABILITY_SYNC \
  210. __attribute__((availability(macos, strict, introduced = 11.0))) \
  211. __attribute__((availability(ios, strict, introduced = 14.0))) \
  212. __attribute__((availability(tvos, strict, introduced = 14.0))) \
  213. __attribute__((availability(watchos, strict, introduced = 7.0)))
  214. // __libcpp_verbose_abort
  215. // TODO: Update once this is released
  216. # define _LIBCPP_AVAILABILITY_HAS_VERBOSE_ABORT 0
  217. # define _LIBCPP_AVAILABILITY_VERBOSE_ABORT __attribute__((unavailable))
  218. // std::pmr
  219. // clang-format off
  220. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 140000) || \
  221. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 170000) || \
  222. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 170000) || \
  223. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 100000)
  224. // clang-format on
  225. # define _LIBCPP_AVAILABILITY_HAS_PMR 0
  226. # else
  227. # define _LIBCPP_AVAILABILITY_HAS_PMR 1
  228. # endif
  229. // TODO: Enable std::pmr markup once https://github.com/llvm/llvm-project/issues/40340 has been fixed
  230. // Until then, it is possible for folks to try to use `std::pmr` when back-deploying to targets that don't support
  231. // it and it'll be a load-time error, but we don't have a good alternative because the library won't compile if we
  232. // use availability annotations until that bug has been fixed.
  233. # if 0
  234. # define _LIBCPP_AVAILABILITY_PMR \
  235. __attribute__((availability(macos, strict, introduced = 14.0))) \
  236. __attribute__((availability(ios, strict, introduced = 17.0))) \
  237. __attribute__((availability(tvos, strict, introduced = 17.0))) \
  238. __attribute__((availability(watchos, strict, introduced = 10.0)))
  239. # else
  240. # define _LIBCPP_AVAILABILITY_PMR
  241. # endif
  242. # define _LIBCPP_AVAILABILITY_HAS_TZDB 0
  243. # define _LIBCPP_AVAILABILITY_TZDB __attribute__((unavailable))
  244. // Warning: This availability macro works differently than the other macros.
  245. // The dylib part of print is not needed on Apple platforms. Therefore when
  246. // the macro is not available the code calling the dylib is commented out.
  247. // The macro _LIBCPP_AVAILABILITY_PRINT is not used.
  248. # define _LIBCPP_AVAILABILITY_HAS_PRINT 0
  249. # define _LIBCPP_AVAILABILITY_PRINT __attribute__((unavailable))
  250. // clang-format off
  251. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 120000) || \
  252. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 150000) || \
  253. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 150000) || \
  254. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 80000)
  255. // clang-format on
  256. # define _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1 0
  257. # else
  258. # define _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1 1
  259. # endif
  260. #else
  261. // ...New vendors can add availability markup here...
  262. # error \
  263. "It looks like you're trying to enable vendor availability markup, but you haven't defined the corresponding macros yet!"
  264. #endif
  265. // Define availability attributes that depend on _LIBCPP_HAS_NO_EXCEPTIONS.
  266. // Those are defined in terms of the availability attributes above, and
  267. // should not be vendor-specific.
  268. #if defined(_LIBCPP_HAS_NO_EXCEPTIONS)
  269. # define _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
  270. # define _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
  271. # define _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
  272. #else
  273. # define _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST _LIBCPP_AVAILABILITY_BAD_ANY_CAST
  274. # define _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  275. # define _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS
  276. #endif
  277. // Define availability attributes that depend on both
  278. // _LIBCPP_HAS_NO_EXCEPTIONS and _LIBCPP_HAS_NO_RTTI.
  279. #if defined(_LIBCPP_HAS_NO_EXCEPTIONS) || defined(_LIBCPP_HAS_NO_RTTI)
  280. # undef _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION
  281. # undef _LIBCPP_AVAILABILITY_INIT_PRIMARY_EXCEPTION
  282. # define _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION 0
  283. # define _LIBCPP_AVAILABILITY_INIT_PRIMARY_EXCEPTION
  284. #endif
  285. #endif // _LIBCPP___AVAILABILITY