[libc++] Make feature-test macros consistent with availability macros

Before this patch, feature-test macros didn't take special availability
markup into account, which means that feature-test macros can sometimes
appear to "lie". For example, if you compile in C++20 mode and target
macOS 10.13, the __cpp_lib_filesystem feature-test macro will be provided
even though the <filesystem> declarations are marked as unavailable.
This patch fixes that.

rdar://68142369

Differential Revision: https://reviews.llvm.org/D94983

GitOrigin-RevId: 76fc35752d19ac605c1c1fd757af9c7c3bb4a906
diff --git a/include/__availability b/include/__availability
index db2267c..cc3b6fa 100644
--- a/include/__availability
+++ b/include/__availability
@@ -43,6 +43,14 @@
 // as unavailable. When vendors decide to ship the feature as part of their
 // shared library, they can update the markup appropriately.
 //
+// Furthermore, many features in the standard library have corresponding
+// feature-test macros. When a feature is made unavailable on some deployment
+// target, a macro should be defined to signal that it is unavailable. That
+// macro can then be picked up when feature-test macros are generated (see
+// generate_feature_test_macro_components.py) to make sure that feature-test
+// macros don't announce a feature as being implemented if it has been marked
+// as unavailable.
+//
 // Note that this mechanism is disabled by default in the "upstream" libc++.
 // Availability annotations are only meaningful when shipping libc++ inside
 // a platform (i.e. as a system library), and so vendors that want them should
@@ -76,6 +84,8 @@
     // This controls the availability of std::shared_mutex and std::shared_timed_mutex,
     // which were added to the dylib later.
 #   define _LIBCPP_AVAILABILITY_SHARED_MUTEX
+// #   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_mutex
+// #   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_timed_mutex
 
     // These macros control the availability of std::bad_optional_access and
     // other exception types. These were put in the shared library to prevent
@@ -114,6 +124,7 @@
 #   define _LIBCPP_AVAILABILITY_FILESYSTEM
 #   define _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
 #   define _LIBCPP_AVAILABILITY_FILESYSTEM_POP
+// #   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_filesystem
 
     // This controls the availability of std::to_chars.
 #   define _LIBCPP_AVAILABILITY_TO_CHARS
@@ -122,6 +133,10 @@
     // which requires shared library support for various operations
     // (see libcxx/src/atomic.cpp).
 #   define _LIBCPP_AVAILABILITY_SYNC
+// #   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_atomic_wait
+// #   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_barrier
+// #   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_latch
+// #   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_semaphore
 
 #elif defined(__APPLE__)
 
@@ -130,6 +145,14 @@
         __attribute__((availability(ios,strict,introduced=10.0)))               \
         __attribute__((availability(tvos,strict,introduced=10.0)))              \
         __attribute__((availability(watchos,strict,introduced=3.0)))
+#   if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) ||    \
+        (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \
+        (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) ||         \
+        (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000)
+#       define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_mutex
+#       define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_timed_mutex
+#   endif
+
 #   define _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS                             \
         __attribute__((availability(macosx,strict,introduced=10.13)))           \
         __attribute__((availability(ios,strict,introduced=11.0)))               \
@@ -139,27 +162,34 @@
         _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
 #   define _LIBCPP_AVAILABILITY_BAD_ANY_CAST                                    \
         _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
+
 #   define _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS                             \
         __attribute__((availability(macosx,strict,introduced=10.12)))           \
         __attribute__((availability(ios,strict,introduced=10.0)))               \
         __attribute__((availability(tvos,strict,introduced=10.0)))              \
         __attribute__((availability(watchos,strict,introduced=3.0)))
+
 #   define _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE                                \
         __attribute__((availability(macosx,strict,introduced=10.12)))           \
         __attribute__((availability(ios,strict,introduced=10.0)))               \
         __attribute__((availability(tvos,strict,introduced=10.0)))              \
         __attribute__((availability(watchos,strict,introduced=3.0)))
+
 #   define _LIBCPP_AVAILABILITY_FUTURE_ERROR                                    \
         __attribute__((availability(ios,strict,introduced=6.0)))
+
 #   define _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE                                 \
         __attribute__((availability(macosx,strict,introduced=10.9)))            \
         __attribute__((availability(ios,strict,introduced=7.0)))
+
 #   define _LIBCPP_AVAILABILITY_LOCALE_CATEGORY                                 \
         __attribute__((availability(macosx,strict,introduced=10.9)))            \
         __attribute__((availability(ios,strict,introduced=7.0)))
+
 #   define _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR                               \
         __attribute__((availability(macosx,strict,introduced=10.9)))            \
         __attribute__((availability(ios,strict,introduced=7.0)))
+
 #   define _LIBCPP_AVAILABILITY_FILESYSTEM                                      \
         __attribute__((availability(macosx,strict,introduced=10.15)))           \
         __attribute__((availability(ios,strict,introduced=13.0)))               \
@@ -175,10 +205,23 @@
         _Pragma("clang attribute pop")                                          \
         _Pragma("clang attribute pop")                                          \
         _Pragma("clang attribute pop")
+#   if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101500) ||    \
+        (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 130000) || \
+        (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 130000) ||         \
+        (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 60000)
+#       define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_filesystem
+#   endif
+
 #   define _LIBCPP_AVAILABILITY_TO_CHARS                                        \
         _LIBCPP_AVAILABILITY_FILESYSTEM
+
+    // Note: Those are not ABI-stable yet, so we can't ship them.
 #   define _LIBCPP_AVAILABILITY_SYNC                                            \
         __attribute__((unavailable))
+#   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_atomic_wait
+#   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_barrier
+#   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_latch
+#   define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_semaphore
 
 #else