Fix overload sets of strchr, strpbrk, strrchr, memchr and strstr from

<string.h> and wcschr, wcspbrk, wcsrchr, wmemchr, and wcsstr from <wchar.h> to
provide a const-correct overload set even when the underlying C library does
not.

This change adds a new macro, _LIBCPP_PREFERRED_OVERLOAD, which (if defined)
specifies that a given overload is a better match than an otherwise equally
good function declaration without the overload. This is implemented in modern
versions of Clang via __attribute__((enable_if)), and not elsewhere.

We use this new macro to define overloads in the global namespace for these
functions that displace the overloads provided by the C library, unless we
believe the C library is already providing the correct signatures.


git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@260337 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Richard Smith
2016-02-10 00:59:02 +00:00
parent 1654db495f
commit b4aa97130b
9 changed files with 193 additions and 57 deletions

View File

@@ -47,4 +47,15 @@ int main()
static_assert((std::is_same<decltype(memset(vp, 0, s)), void*>::value), "");
static_assert((std::is_same<decltype(strerror(0)), char*>::value), "");
static_assert((std::is_same<decltype(strlen(cpc)), size_t>::value), "");
// These tests fail on systems whose C library doesn't provide a correct overload
// set for strchr, strpbrk, strrchr, strstr, and memchr, unless the compiler is
// a suitably recent version of Clang.
#if !defined(__APPLE__) || defined(_LIBCPP_PREFERRED_OVERLOAD)
static_assert((std::is_same<decltype(strchr(cpc, 0)), const char*>::value), "");
static_assert((std::is_same<decltype(strpbrk(cpc, cpc)), const char*>::value), "");
static_assert((std::is_same<decltype(strrchr(cpc, 0)), const char*>::value), "");
static_assert((std::is_same<decltype(strstr(cpc, cpc)), const char*>::value), "");
static_assert((std::is_same<decltype(memchr(vpc, 0, s)), const void*>::value), "");
#endif
}