Skip to content

Commit

Permalink
[libc++] Remove workaround for C11 features on compilers that don't s…
Browse files Browse the repository at this point in the history
…upport using_if_exists

Instead of carrying around #ifdefs to determine whether those functions
are available on the platform, unconditionally use the using_if_exists
attribute to import it into namespace std only when available. That was
the purpose of this attribute from the start.

This change means that trying to use libc++ with an old SDK (or on an
old platform for platforms that ship system headers in /usr/include)
will require a recent Clang that supports the using_if_exists attribute.
When using an older Clang or GCC, the underlying platform has to support
a C11 standard library.

Differential Revision: https://reviews.llvm.org/D108203
  • Loading branch information
ldionne committed Mar 11, 2022
1 parent 611469c commit 21f73d5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 87 deletions.
69 changes: 0 additions & 69 deletions libcxx/include/__config
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,6 @@
# define _LIBCPP_HAS_BITSCAN64
# endif
# define _LIBCPP_HAS_OPEN_WITH_WCHAR
# if defined(_LIBCPP_MSVCRT)
# define _LIBCPP_HAS_QUICK_EXIT
# endif

// Some CRT APIs are unavailable to store apps
# if defined(WINAPI_FAMILY)
Expand Down Expand Up @@ -392,72 +389,6 @@
# define _LIBCPP_NO_CFI
#endif

// If the compiler supports using_if_exists, pretend we have those functions and they'll
// be picked up if the C library provides them.
//
// TODO: Once we drop support for Clang 12, we can assume the compiler supports using_if_exists
// for platforms that don't have a conforming C11 library, so we can drop this whole thing.
#if __has_attribute(using_if_exists)
# define _LIBCPP_HAS_TIMESPEC_GET
# define _LIBCPP_HAS_QUICK_EXIT
# define _LIBCPP_HAS_ALIGNED_ALLOC
#else
#if (defined(__ISO_C_VISIBLE) && (__ISO_C_VISIBLE >= 2011)) || __cplusplus >= 201103L
# if defined(__FreeBSD__)
# define _LIBCPP_HAS_ALIGNED_ALLOC
# define _LIBCPP_HAS_QUICK_EXIT
# if __FreeBSD_version >= 1300064 || \
(__FreeBSD_version >= 1201504 && __FreeBSD_version < 1300000)
# define _LIBCPP_HAS_TIMESPEC_GET
# endif
# elif defined(__BIONIC__)
# if __ANDROID_API__ >= 21
# define _LIBCPP_HAS_QUICK_EXIT
# endif
# if __ANDROID_API__ >= 28
# define _LIBCPP_HAS_ALIGNED_ALLOC
# endif
# if __ANDROID_API__ >= 29
# define _LIBCPP_HAS_TIMESPEC_GET
# endif
# elif defined(__Fuchsia__) || defined(__wasi__) || defined(__NetBSD__)
# define _LIBCPP_HAS_ALIGNED_ALLOC
# define _LIBCPP_HAS_QUICK_EXIT
# define _LIBCPP_HAS_TIMESPEC_GET
# elif defined(__OpenBSD__)
# define _LIBCPP_HAS_ALIGNED_ALLOC
# define _LIBCPP_HAS_TIMESPEC_GET
# elif defined(__linux__)
# if !defined(_LIBCPP_HAS_MUSL_LIBC)
# if _LIBCPP_GLIBC_PREREQ(2, 15) || defined(__BIONIC__)
# define _LIBCPP_HAS_QUICK_EXIT
# endif
# if _LIBCPP_GLIBC_PREREQ(2, 17)
# define _LIBCPP_HAS_ALIGNED_ALLOC
# define _LIBCPP_HAS_TIMESPEC_GET
# endif
# else // defined(_LIBCPP_HAS_MUSL_LIBC)
# define _LIBCPP_HAS_ALIGNED_ALLOC
# define _LIBCPP_HAS_QUICK_EXIT
# define _LIBCPP_HAS_TIMESPEC_GET
# endif
# elif defined(_LIBCPP_MSVCRT)
// Using Microsoft's C Runtime library, not MinGW
# define _LIBCPP_HAS_TIMESPEC_GET
# elif defined(__APPLE__)
// timespec_get and aligned_alloc were introduced in macOS 10.15 and
// aligned releases
# 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_HAS_ALIGNED_ALLOC
# define _LIBCPP_HAS_TIMESPEC_GET
# endif
# endif // __APPLE__
#endif
#endif // __has_attribute(using_if_exists)

#ifndef _LIBCPP_CXX03_LANG
# define _LIBCPP_ALIGNOF(_Tp) alignof(_Tp)
#elif defined(_LIBCPP_COMPILER_CLANG_BASED)
Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/cstdlib
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ using ::mbtowc _LIBCPP_USING_IF_EXISTS;
using ::wctomb _LIBCPP_USING_IF_EXISTS;
using ::mbstowcs _LIBCPP_USING_IF_EXISTS;
using ::wcstombs _LIBCPP_USING_IF_EXISTS;
#if !defined(_LIBCPP_CXX03_LANG) && defined(_LIBCPP_HAS_QUICK_EXIT)
#if !defined(_LIBCPP_CXX03_LANG)
using ::at_quick_exit _LIBCPP_USING_IF_EXISTS;
using ::quick_exit _LIBCPP_USING_IF_EXISTS;
#endif
#if _LIBCPP_STD_VER > 14 && defined(_LIBCPP_HAS_ALIGNED_ALLOC)
#if _LIBCPP_STD_VER > 14
using ::aligned_alloc _LIBCPP_USING_IF_EXISTS;
#endif

Expand Down
18 changes: 2 additions & 16 deletions libcxx/include/ctime
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,13 @@ int timespec_get( struct timespec *ts, int base); // C++17
# pragma GCC system_header
#endif

// FIXME:
// Apple SDKs don't define ::timespec_get unconditionally in C++ mode. This
// should be fixed in future SDKs, but for the time being we need to avoid
// trying to use that declaration when the SDK doesn't provide it. Note that
// we're detecting this here instead of in <__config> because we can't include
// system headers from <__config>, since it leads to circular module dependencies.
// This is also meant to be a very temporary workaround until the SDKs are fixed.
#if defined(__APPLE__) && !__has_attribute(using_if_exists)
# include <sys/cdefs.h>
# if defined(_LIBCPP_HAS_TIMESPEC_GET) && (__DARWIN_C_LEVEL < __DARWIN_C_FULL)
# define _LIBCPP_HAS_TIMESPEC_GET_NOT_ACTUALLY_PROVIDED
# endif
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

using ::clock_t _LIBCPP_USING_IF_EXISTS;
using ::size_t _LIBCPP_USING_IF_EXISTS;
using ::time_t _LIBCPP_USING_IF_EXISTS;
using ::tm _LIBCPP_USING_IF_EXISTS;
#if _LIBCPP_STD_VER > 14 && defined(_LIBCPP_HAS_TIMESPEC_GET)
#if _LIBCPP_STD_VER > 14
using ::timespec _LIBCPP_USING_IF_EXISTS;
#endif
using ::clock _LIBCPP_USING_IF_EXISTS;
Expand All @@ -84,7 +70,7 @@ using ::ctime _LIBCPP_USING_IF_EXISTS;
using ::gmtime _LIBCPP_USING_IF_EXISTS;
using ::localtime _LIBCPP_USING_IF_EXISTS;
using ::strftime _LIBCPP_USING_IF_EXISTS;
#if _LIBCPP_STD_VER > 14 && defined(_LIBCPP_HAS_TIMESPEC_GET) && !defined(_LIBCPP_HAS_TIMESPEC_GET_NOT_ACTUALLY_PROVIDED)
#if _LIBCPP_STD_VER > 14
using ::timespec_get _LIBCPP_USING_IF_EXISTS;
#endif

Expand Down

0 comments on commit 21f73d5

Please sign in to comment.