public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tom Honermann <tom@honermann.net>
To: "libstdc++@gcc.gnu.org" <libstdc++@gcc.gnu.org>
Subject: [PATCH] C++ P0482R6 char8_t: declare std::c8rtomb and std::mbrtoc8 if provided by the C library
Date: Sun, 6 Jun 2021 22:16:01 -0400	[thread overview]
Message-ID: <c6edf8b0-c2e6-1a18-8d04-e67ca6fc69cc@honermann.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 1932 bytes --]

This patch completes implementation of the C++20 proposal P0482R6 [1] 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 -fchar8_t; 
_GLIBCXX_USE_UCHAR_CHAR8_T is defined if so.  The <cuchar> header 
re-declares these functions in the std namespace only if available and 
the C++20 __cpp_char8_t feature test macro is defined.

Patches to glibc to implement c8rtomb and mbrtoc8 have been submitted [2].

A new test is provided.  The test passes trivially if the C library does 
not provide these functions.  Otherwise it ensures that the functions 
are declared when <cuchar> is included and -fchar8_t support is enabled.

Tested on Linux x86_64.

libstdc++-v3/ChangeLog:

2021-05-31  Tom Honermann  <tom@honermann.net>

         * acinclude.m4 (_GLIBCXX_USE_UCHAR_CHAR8_T) Define 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.cc: New test.

Tom.

[1]: WG21 P0482R6
      "char8_t: A type for UTF-8 characters and strings (Revision 6)"
      https://wg21.link/p0482r6

[2]: C++20 P0482R6 and C2X N2653: support for char8_t, mbrtoc8(), and 
c8rtomb().
      [Patch 0]: 
https://sourceware.org/pipermail/libc-alpha/2021-June/127230.html
      [Patch 1]: 
https://sourceware.org/pipermail/libc-alpha/2021-June/127231.html
      [Patch 2]: 
https://sourceware.org/pipermail/libc-alpha/2021-June/127232.html
      [Patch 3]: 
https://sourceware.org/pipermail/libc-alpha/2021-June/127233.html




[-- Attachment #2: p0482r6-n2653.patch --]
[-- Type: text/x-patch, Size: 6687 bytes --]

commit bffd3c8d94b7978d296df4b0e2fa77cc5887efe6
Author: Tom Honermann <tom@honermann.net>
Date:   Sat Feb 13 07:53:34 2021 -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 -fchar8_t;
    _GLIBCXX_USE_UCHAR_CHAR8_T is defined if so.  The <cuchar> header
    re-declares these functions in the std namespace only if available and
    the C++20 __cpp_char8_t feature test macro is defined.
    
    A new test is provided.  The test passes trivially if the C library does
    not provide these functions.  Otherwise it ensures that the functions
    are declared when <cuchar> is included and -fchar8_t support is enabled.

diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4
index 90ecc4a87a2..355c21a03b5 100644
--- a/libstdc++-v3/acinclude.m4
+++ b/libstdc++-v3/acinclude.m4
@@ -2063,6 +2063,28 @@ AC_DEFUN([GLIBCXX_CHECK_UCHAR_H], [
 	      namespace std in <cuchar>.])
   fi
 
+  CXXFLAGS="$CXXFLAGS -fchar8_t"
+  if test x"$ac_has_uchar_h" = x"yes"; then
+    AC_MSG_CHECKING([for char8_t support in <uchar.h>])
+    AC_TRY_COMPILE([#include <uchar.h>
+		    char8_t c8;
+		    namespace test
+		    {
+		      using ::c8rtomb;
+		      using ::mbrtoc8;
+		    }
+		   ],
+		   [], [ac_uchar_char8_t=yes], [ac_uchar_char8_t=no])
+  else
+    ac_uchar_char8_t=no
+  fi
+  AC_MSG_RESULT($ac_uchar_char8_t)
+  if test x"$ac_uchar_char8_t" = x"yes"; then
+    AC_DEFINE(_GLIBCXX_USE_UCHAR_CHAR8_T, 1,
+	      [Define if char8_t functions in <uchar.h> should be imported into
+	      namespace std in <cuchar>.])
+  fi
+
   CXXFLAGS="$ac_save_CXXFLAGS"
   AC_LANG_RESTORE
 ])
diff --git a/libstdc++-v3/config.h.in b/libstdc++-v3/config.h.in
index e545488386a..745de9687a4 100644
--- a/libstdc++-v3/config.h.in
+++ b/libstdc++-v3/config.h.in
@@ -991,6 +991,10 @@
 /* Define if obsolescent tmpnam is available in <stdio.h>. */
 #undef _GLIBCXX_USE_TMPNAM
 
+/* Define if char8_t functions in <uchar.h> should be imported into namespace
+   std in <cuchar>. */
+#undef _GLIBCXX_USE_UCHAR_CHAR8_T
+
 /* Define if utime is available in <utime.h>. */
 #undef _GLIBCXX_USE_UTIME
 
diff --git a/libstdc++-v3/configure b/libstdc++-v3/configure
index 6f08b65c8ba..8311419a127 100755
--- a/libstdc++-v3/configure
+++ b/libstdc++-v3/configure
@@ -19115,6 +19115,45 @@ $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 char8_t support in <uchar.h>" >&5
+$as_echo_n "checking for char8_t support in <uchar.h>... " >&6; }
+    cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <uchar.h>
+		    char8_t c8;
+		    namespace test
+		    {
+		      using ::c8rtomb;
+		      using ::mbrtoc8;
+		    }
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+  ac_uchar_char8_t=yes
+else
+  ac_uchar_char8_t=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+  else
+    ac_uchar_char8_t=no
+  fi
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_uchar_char8_t" >&5
+$as_echo "$ac_uchar_char8_t" >&6; }
+  if test x"$ac_uchar_char8_t" = x"yes"; then
+
+$as_echo "#define _GLIBCXX_USE_UCHAR_CHAR8_T 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..21812bb44b4 100644
--- a/libstdc++-v3/include/c_compatibility/uchar.h
+++ b/libstdc++-v3/include/c_compatibility/uchar.h
@@ -33,6 +33,11 @@
 
 #ifdef _GLIBCXX_NAMESPACE_C
 
+#if _GLIBCXX_USE_CHAR8_T && _GLIBCXX_USE_UCHAR_CHAR8_T
+using std::mbrtoc8;
+using std::c8rtomb;
+#endif
+
 #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..aab62e57bc8 100644
--- a/libstdc++-v3/include/c_global/cuchar
+++ b/libstdc++-v3/include/c_global/cuchar
@@ -48,10 +48,34 @@
 #include <bits/c++config.h>
 #include <cwchar>
 
-#if _GLIBCXX_USE_C11_UCHAR_CXX11
+#if _GLIBCXX_USE_C11_UCHAR_CXX11 || _GLIBCXX_USE_CHAR8_T
 
 #include <uchar.h>
 
+#endif
+
+
+// Support for mbrtoc8 and c8rtomb is conditioned on support by the C library.
+#if _GLIBCXX_USE_CHAR8_T && _GLIBCXX_USE_UCHAR_CHAR8_T
+
+#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 && _GLIBCXX_USE_UCHAR_CHAR8_T
+
+
+#if _GLIBCXX_USE_C11_UCHAR_CXX11
+
 // Get rid of those macros defined in <uchar.h> 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..13d8b773c49 100644
--- a/libstdc++-v3/include/c_std/cuchar
+++ b/libstdc++-v3/include/c_std/cuchar
@@ -48,10 +48,35 @@
 #include <bits/c++config.h>
 #include <cwchar>
 
-#if _GLIBCXX_USE_C11_UCHAR_CXX11
+#if _GLIBCXX_USE_C11_UCHAR_CXX11 || _GLIBCXX_USE_CHAR8_T
 
 #include <uchar.h>
 
+#endif
+
+
+// Support for mbrtoc8 and c8rtomb is conditioned on support by the C library.
+#if _GLIBCXX_USE_CHAR8_T && _GLIBCXX_USE_UCHAR_CHAR8_T
+
+// Get rid of those macros defined in <uchar.h> 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 && _GLIBCXX_USE_UCHAR_CHAR8_T
+
+
+#if _GLIBCXX_USE_C11_UCHAR_CXX11
+
 // Get rid of those macros defined in <uchar.h> in lieu of real functions.
 #undef mbrtoc16
 #undef c16rtomb
diff --git a/libstdc++-v3/testsuite/21_strings/headers/cuchar/functions_std.cc b/libstdc++-v3/testsuite/21_strings/headers/cuchar/functions_std.cc
new file mode 100644
index 00000000000..192b5a0481d
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/headers/cuchar/functions_std.cc
@@ -0,0 +1,12 @@
+// { dg-do compile }
+// { dg-options "-fchar8_t" }
+
+#include <cuchar>
+
+namespace gnu
+{
+#if _GLIBCXX_USE_UCHAR_CHAR8_T
+  using std::mbrtoc8;
+  using std::c8rtomb;
+#endif
+}



             reply	other threads:[~2021-06-07  2:16 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-07  2:16 Tom Honermann [this message]
2021-06-23 17:27 ` Jonathan Wakely
2021-06-24 19:35   ` Tom Honermann
2021-06-24 19:39     ` Jonathan Wakely
2021-06-25  2:56       ` Tom Honermann
2022-01-08  0:42 Tom Honermann
2022-01-10 13:23 ` Jonathan Wakely
2022-01-10 21:23   ` Tom Honermann
2022-01-10 21:38     ` Jonathan Wakely
2022-01-11 20:13       ` Tom Honermann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c6edf8b0-c2e6-1a18-8d04-e67ca6fc69cc@honermann.net \
    --to=tom@honermann.net \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).