__availability 18 KB

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