Skip to content

Commit

Permalink
[libc++] Use the using_if_exists attribute when provided
Browse files Browse the repository at this point in the history
As discussed on cfe-dev [1], use the using_if_exists Clang attribute when
the compiler supports it. This makes it easier to port libc++ on top of
new platforms that don't fully support the C Standard library.

Previously, libc++ would fail to build when trying to import a missing
declaration in a <cXXXX> header. With the attribute, the declaration will
simply not be imported into namespace std, and hence it won't be available
for libc++ to use. In many cases, the declarations were *not* actually
required for libc++ to work (they were only surfaced for users to use
them as std::XXXX), so not importing them into namespace std is acceptable.

The same thing could be achieved by conscious usage of `#ifdef` along
with platform detection, however that quickly creates a maintenance
problem as libc++ is ported to new platforms. Furthermore, this problem
is exacerbated when mixed with vendor internal-only platforms, which can
lead to difficulties maintaining a downstream fork of the library.

For the time being, we only use the using_if_exists attribute when it
is supported. At some point in the future, we will start removing #ifdef
paths that are unnecessary when the attribute is supported, and folks
who need those #ifdef paths will be required to use a compiler that
supports the attribute.

[1]: http://lists.llvm.org/pipermail/cfe-dev/2020-June/066038.html

Differential Revision: https://reviews.llvm.org/D90257
  • Loading branch information
ldionne committed Jun 4, 2021
1 parent 86c2449 commit a9c9183
Show file tree
Hide file tree
Showing 17 changed files with 513 additions and 507 deletions.
6 changes: 6 additions & 0 deletions libcxx/include/__config
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,12 @@ typedef unsigned int char32_t;
# define _LIBCPP_EXPLICIT
#endif

#if __has_attribute(using_if_exists)
# define _LIBCPP_USING_IF_EXISTS __attribute__((using_if_exists))
#else
# define _LIBCPP_USING_IF_EXISTS
#endif

#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
# define _LIBCPP_DECLARE_STRONG_ENUM(x) struct _LIBCPP_TYPE_VIS x { enum __lx
# define _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(x) \
Expand Down
28 changes: 14 additions & 14 deletions libcxx/include/cctype
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#endif


using ::isalnum;
using ::isalpha;
using ::isblank;
using ::iscntrl;
using ::isdigit;
using ::isgraph;
using ::islower;
using ::isprint;
using ::ispunct;
using ::isspace;
using ::isupper;
using ::isxdigit;
using ::tolower;
using ::toupper;
using ::isalnum _LIBCPP_USING_IF_EXISTS;
using ::isalpha _LIBCPP_USING_IF_EXISTS;
using ::isblank _LIBCPP_USING_IF_EXISTS;
using ::iscntrl _LIBCPP_USING_IF_EXISTS;
using ::isdigit _LIBCPP_USING_IF_EXISTS;
using ::isgraph _LIBCPP_USING_IF_EXISTS;
using ::islower _LIBCPP_USING_IF_EXISTS;
using ::isprint _LIBCPP_USING_IF_EXISTS;
using ::ispunct _LIBCPP_USING_IF_EXISTS;
using ::isspace _LIBCPP_USING_IF_EXISTS;
using ::isupper _LIBCPP_USING_IF_EXISTS;
using ::isxdigit _LIBCPP_USING_IF_EXISTS;
using ::tolower _LIBCPP_USING_IF_EXISTS;
using ::toupper _LIBCPP_USING_IF_EXISTS;

_LIBCPP_END_NAMESPACE_STD

Expand Down
28 changes: 14 additions & 14 deletions libcxx/include/cfenv
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ int feupdateenv(const fenv_t* envp);

_LIBCPP_BEGIN_NAMESPACE_STD

using ::fenv_t;
using ::fexcept_t;

using ::feclearexcept;
using ::fegetexceptflag;
using ::feraiseexcept;
using ::fesetexceptflag;
using ::fetestexcept;
using ::fegetround;
using ::fesetround;
using ::fegetenv;
using ::feholdexcept;
using ::fesetenv;
using ::feupdateenv;
using ::fenv_t _LIBCPP_USING_IF_EXISTS;
using ::fexcept_t _LIBCPP_USING_IF_EXISTS;

using ::feclearexcept _LIBCPP_USING_IF_EXISTS;
using ::fegetexceptflag _LIBCPP_USING_IF_EXISTS;
using ::feraiseexcept _LIBCPP_USING_IF_EXISTS;
using ::fesetexceptflag _LIBCPP_USING_IF_EXISTS;
using ::fetestexcept _LIBCPP_USING_IF_EXISTS;
using ::fegetround _LIBCPP_USING_IF_EXISTS;
using ::fesetround _LIBCPP_USING_IF_EXISTS;
using ::fegetenv _LIBCPP_USING_IF_EXISTS;
using ::feholdexcept _LIBCPP_USING_IF_EXISTS;
using ::fesetenv _LIBCPP_USING_IF_EXISTS;
using ::feupdateenv _LIBCPP_USING_IF_EXISTS;

_LIBCPP_END_NAMESPACE_STD

Expand Down
14 changes: 7 additions & 7 deletions libcxx/include/cinttypes
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ uintmax_t wcstoumax(const wchar_t* restrict nptr, wchar_t** restrict endptr, int

_LIBCPP_BEGIN_NAMESPACE_STD

using::imaxdiv_t;
using::imaxabs;
using::imaxdiv;
using::strtoimax;
using::strtoumax;
using::wcstoimax;
using::wcstoumax;
using ::imaxdiv_t _LIBCPP_USING_IF_EXISTS;
using ::imaxabs _LIBCPP_USING_IF_EXISTS;
using ::imaxdiv _LIBCPP_USING_IF_EXISTS;
using ::strtoimax _LIBCPP_USING_IF_EXISTS;
using ::strtoumax _LIBCPP_USING_IF_EXISTS;
using ::wcstoimax _LIBCPP_USING_IF_EXISTS;
using ::wcstoumax _LIBCPP_USING_IF_EXISTS;

_LIBCPP_END_NAMESPACE_STD

Expand Down
6 changes: 3 additions & 3 deletions libcxx/include/clocale
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ lconv* localeconv();

_LIBCPP_BEGIN_NAMESPACE_STD

using ::lconv;
using ::lconv _LIBCPP_USING_IF_EXISTS;
#ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
using ::setlocale;
using ::setlocale _LIBCPP_USING_IF_EXISTS;
#endif
using ::localeconv;
using ::localeconv _LIBCPP_USING_IF_EXISTS;

_LIBCPP_END_NAMESPACE_STD

Expand Down
Loading

0 comments on commit a9c9183

Please sign in to comment.