From patchwork Sat Jan 8 00:42:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Honermann X-Patchwork-Id: 49745 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A9D203857C59 for ; Sat, 8 Jan 2022 00:43:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A9D203857C59 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641602620; bh=OQeim3mxNxye7jC+9+Ip0QI1skNw7CdNse4RfFhWjd0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=SXDXVgtJ0k7y7kALeVbJ2IJjY+/w0F7H10vxAvF4QqAh4/Ki8EJCJ4Oys8yESOwYV sJg1t3Ecl4Ox5ahveBfufOLeIW1P2hkhA0Dj9aT+LgoSYK5oJETI+V28LxEGi1vWcR naJrZSfaZSTwCf4pe6jgCyutDBXhKiuEOhljK5qQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp92.ord1c.emailsrvr.com (smtp92.ord1c.emailsrvr.com [108.166.43.92]) by sourceware.org (Postfix) with ESMTPS id 068323857815 for ; Sat, 8 Jan 2022 00:42:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 068323857815 X-Auth-ID: tom@honermann.net Received: by smtp4.relay.ord1c.emailsrvr.com (Authenticated sender: tom-AT-honermann.net) with ESMTPSA id 34BD6A00E1; Fri, 7 Jan 2022 19:42:23 -0500 (EST) To: "libstdc++@gcc.gnu.org" , gcc-patches Subject: [PATCH] C++ P0482R6 char8_t: declare std::c8rtomb and std::mbrtoc8 if provided by the C library Message-ID: <79037d6b-3c48-eb7b-030a-f388fb988187@honermann.net> Date: Fri, 7 Jan 2022 19:42:22 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 Content-Language: en-US X-Classification-ID: b084d52c-97c4-4a93-89dc-8c63ed70bcd4-1-1 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Honermann via Gcc-patches From: Tom Honermann Reply-To: Tom Honermann Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This patch completes implementation of the C++20 proposal P0482R6 [1] by adding declarations of std::c8rtomb() and std::mbrtoc8() in if provided by the C library in . This patch addresses feedback provided in response to a previous patch submission [2]. Autoconf changes determine if the C library declares c8rtomb and mbrtoc8 at global scope when uchar.h is included and compiled with either -fchar8_t or -std=c++20. New _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T and _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20 configuration macros reflect the probe results. The header declares these functions in the std namespace only if available and the _GLIBCXX_USE_CHAR8_T configuration macro is defined (by default it is defined if the C++20 __cpp_char8_t feature test macro is defined) Patches to glibc to implement c8rtomb and mbrtoc8 have been submitted [3]. New tests validate the presence of these declarations. The tests pass trivially if the C library does not provide these functions. Otherwise they ensure that the functions are declared when is included and either -fchar8_t or -std=c++20 is enabled. Tested on Linux x86_64. libstdc++-v3/ChangeLog: 2022-01-07 Tom Honermann * acinclude.m4 Define config macros if uchar.h provides c8rtomb() and mbrtoc8(). * config.h.in: Re-generate. * configure: Re-generate. * include/c_compatibility/uchar.h: Declare ::c8rtomb and ::mbrtoc8. * include/c_global/cuchar: Declare std::c8rtomb and std::mbrtoc8. * include/c_std/cuchar: Declare std::c8rtomb and std::mbrtoc8. * testsuite/21_strings/headers/cuchar/functions_std_cxx20.cc: New test. * testsuite/21_strings/headers/cuchar/functions_std_fchar8_t.cc: New test. Tom. [1]: WG21 P0482R6 "char8_t: A type for UTF-8 characters and strings (Revision 6)" https://wg21.link/p0482r6 [2]: [PATCH] C++ P0482R6 char8_t: declare std::c8rtomb and std::mbrtoc8 if provided by the C library https://gcc.gnu.org/pipermail/libstdc++/2021-June/052685.html [3]: "C++20 P0482R6 and C2X N2653" [Patch 0/3]: https://sourceware.org/pipermail/libc-alpha/2022-January/135061.html [Patch 1/3]: https://sourceware.org/pipermail/libc-alpha/2022-January/135062.html [Patch 2/3]: https://sourceware.org/pipermail/libc-alpha/2022-January/135063.html [Patch 3/3]: https://sourceware.org/pipermail/libc-alpha/2022-January/135064.html Tom. commit 3d40bc9bf5c79343ea5a6cc355539542f4b56c9b Author: Tom Honermann Date: Sat Jan 1 17:26:31 2022 -0500 P0482R6 char8_t: declare std::c8rtomb and std::mbrtoc8 if provided by the C library. This change completes implementation of the C++20 proposal P0482R6 by adding declarations of std::c8rtomb() and std::mbrtoc8() if provided by the C library. Autoconf changes determine if the C library declares c8rtomb and mbrtoc8 at global scope when uchar.h is included and compiled with either -fchar8_t or -std=c++20 enabled; new _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T and _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20 configuration macros are defined accordingly. The header declares these functions in the std namespace only if available and the _GLIBCXX_USE_CHAR8_T configuration macro is defined (by default it is defined if the C++20 __cpp_char8_t feature test macro is defined). New tests validate the presence of these declarations. The tests pass trivially if the C library does not provide these functions. Otherwise they ensure that the functions are declared when is included and either -fchar8_t or -std=c++20 is enabled. diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4 index 635168d7e25..85235005c7e 100644 --- a/libstdc++-v3/acinclude.m4 +++ b/libstdc++-v3/acinclude.m4 @@ -2039,6 +2039,50 @@ AC_DEFUN([GLIBCXX_CHECK_UCHAR_H], [ namespace std in .]) fi + CXXFLAGS="$CXXFLAGS -fchar8_t" + if test x"$ac_has_uchar_h" = x"yes"; then + AC_MSG_CHECKING([for c8rtomb and mbrtoc8 in with -fchar8_t]) + AC_TRY_COMPILE([#include + namespace test + { + using ::c8rtomb; + using ::mbrtoc8; + } + ], + [], [ac_uchar_c8rtomb_mbrtoc8_fchar8_t=yes], + [ac_uchar_c8rtomb_mbrtoc8_fchar8_t=no]) + else + ac_uchar_c8rtomb_mbrtoc8_fchar8_t=no + fi + AC_MSG_RESULT($ac_uchar_c8rtomb_mbrtoc8_fchar8_t) + if test x"$ac_uchar_c8rtomb_mbrtoc8_fchar8_t" = x"yes"; then + AC_DEFINE(_GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T, 1, + [Define if c8rtomb and mbrtoc8 functions in should be + imported into namespace std in for -fchar8_t.]) + fi + + CXXFLAGS="$CXXFLAGS -std=c++20" + if test x"$ac_has_uchar_h" = x"yes"; then + AC_MSG_CHECKING([for c8rtomb and mbrtoc8 in with -std=c++20]) + AC_TRY_COMPILE([#include + namespace test + { + using ::c8rtomb; + using ::mbrtoc8; + } + ], + [], [ac_uchar_c8rtomb_mbrtoc8_cxx20=yes], + [ac_uchar_c8rtomb_mbrtoc8_cxx20=no]) + else + ac_uchar_c8rtomb_mbrtoc8_cxx20=no + fi + AC_MSG_RESULT($ac_uchar_c8rtomb_mbrtoc8_cxx20) + if test x"$ac_uchar_c8rtomb_mbrtoc8_cxx20" = x"yes"; then + AC_DEFINE(_GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20, 1, + [Define if c8rtomb and mbrtoc8 functions in should be + imported into namespace std in for C++20.]) + fi + CXXFLAGS="$ac_save_CXXFLAGS" AC_LANG_RESTORE ]) diff --git a/libstdc++-v3/config.h.in b/libstdc++-v3/config.h.in index 10675fe2530..e09744961d1 100644 --- a/libstdc++-v3/config.h.in +++ b/libstdc++-v3/config.h.in @@ -1000,6 +1000,14 @@ /* Define if obsolescent tmpnam is available in . */ #undef _GLIBCXX_USE_TMPNAM +/* Define if c8rtomb and std functions in should be + imported into namespace std in for -fchar8_t. */ +#undef _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T + +/* Define if c8rtomb and std functions in should be + imported into namespace std in for C++20. */ +#undef _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20 + /* Define if utime is available in . */ #undef _GLIBCXX_USE_UTIME diff --git a/libstdc++-v3/configure b/libstdc++-v3/configure index b1a0157d0b9..fae78ec1cc6 100755 --- a/libstdc++-v3/configure +++ b/libstdc++-v3/configure @@ -19130,6 +19130,82 @@ $as_echo "#define _GLIBCXX_USE_C11_UCHAR_CXX11 1" >>confdefs.h fi + CXXFLAGS="$CXXFLAGS -fchar8_t" + if test x"$ac_has_uchar_h" = x"yes"; then + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for c8rtomb and mbrtoc8 in with -fchar8_t" >&5 +$as_echo_n "checking for c8rtomb and mbrtoc8 in with -fchar8_t... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include + namespace test + { + using ::c8rtomb; + using ::mbrtoc8; + } + +int +main () +{ + + ; + return 0; +} +_ACEOF +if ac_fn_cxx_try_compile "$LINENO"; then : + ac_uchar_c8rtomb_mbrtoc8_fchar8_t=yes +else + ac_uchar_c8rtomb_mbrtoc8_fchar8_t=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + else + ac_uchar_c8rtomb_mbrtoc8_fchar8_t=no + fi + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_uchar_c8rtomb_mbrtoc8_fchar8_t" >&5 +$as_echo "$ac_uchar_c8rtomb_mbrtoc8_fchar8_t" >&6; } + if test x"$ac_uchar_c8rtomb_mbrtoc8_fchar8_t" = x"yes"; then + +$as_echo "#define _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T 1" >>confdefs.h + + fi + + CXXFLAGS="$CXXFLAGS -std=c++20" + if test x"$ac_has_uchar_h" = x"yes"; then + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for c8rtomb and mbrtoc8 in with -std=c++20" >&5 +$as_echo_n "checking for c8rtomb and mbrtoc8 in with -std=c++20... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include + namespace test + { + using ::c8rtomb; + using ::mbrtoc8; + } + +int +main () +{ + + ; + return 0; +} +_ACEOF +if ac_fn_cxx_try_compile "$LINENO"; then : + ac_uchar_c8rtomb_mbrtoc8_cxx20=yes +else + ac_uchar_c8rtomb_mbrtoc8_cxx20=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + else + ac_uchar_c8rtomb_mbrtoc8_cxx20=no + fi + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_uchar_c8rtomb_mbrtoc8_cxx20" >&5 +$as_echo "$ac_uchar_c8rtomb_mbrtoc8_cxx20" >&6; } + if test x"$ac_uchar_c8rtomb_mbrtoc8_cxx20" = x"yes"; then + +$as_echo "#define _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20 1" >>confdefs.h + + fi + CXXFLAGS="$ac_save_CXXFLAGS" ac_ext=c ac_cpp='$CPP $CPPFLAGS' diff --git a/libstdc++-v3/include/c_compatibility/uchar.h b/libstdc++-v3/include/c_compatibility/uchar.h index 1fe8a22f78a..b2346e70d2b 100644 --- a/libstdc++-v3/include/c_compatibility/uchar.h +++ b/libstdc++-v3/include/c_compatibility/uchar.h @@ -33,6 +33,14 @@ #ifdef _GLIBCXX_NAMESPACE_C +#if (_GLIBCXX_USE_CHAR8_T \ + && (_GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T \ + || (__cplusplus >= 202002 \ + && _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20))) +using std::mbrtoc8; +using std::c8rtomb; +#endif // _GLIBCXX_USE_CHAR8_T + #if _GLIBCXX_USE_C11_UCHAR_CXX11 using std::mbrtoc16; using std::c16rtomb; diff --git a/libstdc++-v3/include/c_global/cuchar b/libstdc++-v3/include/c_global/cuchar index 57047d74218..943d2727878 100644 --- a/libstdc++-v3/include/c_global/cuchar +++ b/libstdc++-v3/include/c_global/cuchar @@ -48,10 +48,41 @@ #include #include -#if _GLIBCXX_USE_C11_UCHAR_CXX11 +#if (_GLIBCXX_USE_C11_UCHAR_CXX11 \ + || (_GLIBCXX_USE_CHAR8_T \ + && (_GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T \ + || (__cplusplus >= 202002 \ + && _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20)))) #include +#endif + + +// Support for mbrtoc8 and c8rtomb is conditioned on support by the C library. +#if (_GLIBCXX_USE_CHAR8_T \ + && (_GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T \ + || (__cplusplus >= 202002 \ + && _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20))) + +#undef mbrtoc8 +#undef c8rtomb + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + using ::mbrtoc8; + using ::c8rtomb; + +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace std + +#endif // _GLIBCXX_USE_CHAR8_T + + +#if _GLIBCXX_USE_C11_UCHAR_CXX11 + // Get rid of those macros defined in in lieu of real functions. #undef mbrtoc16 #undef c16rtomb diff --git a/libstdc++-v3/include/c_std/cuchar b/libstdc++-v3/include/c_std/cuchar index 7ce413e1bb0..56c11f7d24f 100644 --- a/libstdc++-v3/include/c_std/cuchar +++ b/libstdc++-v3/include/c_std/cuchar @@ -48,10 +48,42 @@ #include #include -#if _GLIBCXX_USE_C11_UCHAR_CXX11 +#if (_GLIBCXX_USE_C11_UCHAR_CXX11 \ + || (_GLIBCXX_USE_CHAR8_T \ + && (_GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T \ + || (__cplusplus >= 202002 \ + && _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20)))) #include +#endif + + +// Support for mbrtoc8 and c8rtomb is conditioned on support by the C library. +#if (_GLIBCXX_USE_CHAR8_T \ + && (_GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T \ + || (__cplusplus >= 202002 \ + && _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20))) + +// Get rid of those macros defined in in lieu of real functions. +#undef mbrtoc8 +#undef c8rtomb + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + using ::mbrtoc8; + using ::c8rtomb; + +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace std + +#endif // _GLIBCXX_USE_CHAR8_T + + +#if _GLIBCXX_USE_C11_UCHAR_CXX11 + // Get rid of those macros defined in in lieu of real functions. #undef mbrtoc16 #undef c16rtomb diff --git a/libstdc++-v3/testsuite/21_strings/headers/cuchar/functions_std_cxx20.cc b/libstdc++-v3/testsuite/21_strings/headers/cuchar/functions_std_cxx20.cc new file mode 100644 index 00000000000..7c152ed42b5 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/headers/cuchar/functions_std_cxx20.cc @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-options "-std=c++20" } + +#include + +namespace gnu +{ +#if _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20 + using std::mbrtoc8; + using std::c8rtomb; +#endif // _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_CXX20 +} diff --git a/libstdc++-v3/testsuite/21_strings/headers/cuchar/functions_std_fchar8_t.cc b/libstdc++-v3/testsuite/21_strings/headers/cuchar/functions_std_fchar8_t.cc new file mode 100644 index 00000000000..1cfaf7427e5 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/headers/cuchar/functions_std_fchar8_t.cc @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-options "-fchar8_t" } + +#include + +namespace gnu +{ +#if _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T + using std::mbrtoc8; + using std::c8rtomb; +#endif // _GLIBCXX_USE_UCHAR_C8RTOMB_MBRTOC8_FCHAR8_T +}