https://gcc.gnu.org/g:7d199a32ebdee1e242fe6016012ea29a60de54d4
commit r16-6655-g7d199a32ebdee1e242fe6016012ea29a60de54d4 Author: Keith Packard <[email protected]> Date: Thu Jan 8 10:14:07 2026 -0800 libstdc++: Fix C++ 11 ctype when using picolibc (blank vs space) Existing toolchain builds rely on the similarity between picolibc and newlib when building libstdc++ and use --with-newlib. Switch to the picolibc 16-bit _ctype_wide array which provides separate values for ctype_base::blank and ctype_base::space. This fixes a bug where libstdc++ was including '\f', '\n', '\r' and '\v' in the set of 'blank' chars. Afterwards, only ' ' and '\t' are in this set, as specified by C++ 11. libstdc++-v3/ChangeLog: * acinclude.m4 (GLIBCXX_CONFIGURE): Add --with-picolibc. * configure: Regenerate. * configure.ac: Add handling for with_picolibc=yes. * config/os/picolibc/ctype_base.h: New file. * config/os/picolibc/ctype_configure_char.cc: New file. * config/os/picolibc/ctype_inline.h: New file. * config/os/picolibc/os_defines.h: New file. Signed-off-by: Keith Packard <[email protected]> Diff: --- libstdc++-v3/acinclude.m4 | 4 + libstdc++-v3/config/os/picolibc/ctype_base.h | 61 +++++++++ .../config/os/picolibc/ctype_configure_char.cc | 105 ++++++++++++++ libstdc++-v3/config/os/picolibc/ctype_inline.h | 74 ++++++++++ libstdc++-v3/config/os/picolibc/os_defines.h | 36 +++++ libstdc++-v3/configure | 152 ++++++++++++++++++++- libstdc++-v3/configure.ac | 62 +++++++++ 7 files changed, 488 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4 index e6723edf880e..38030e432d81 100644 --- a/libstdc++-v3/acinclude.m4 +++ b/libstdc++-v3/acinclude.m4 @@ -81,6 +81,10 @@ AC_DEFUN([GLIBCXX_CONFIGURE], [ AC_HELP_STRING([--with-newlib], [assume newlib as a system C library])) + AC_ARG_WITH([picolibc], + AC_HELP_STRING([--with-picolibc], + [assume picolibc as a system C library])) + # Will set LN_S to either 'ln -s', 'ln', or 'cp -p' (if linking isn't # available). Uncomment the next line to force a particular method. AC_PROG_LN_S diff --git a/libstdc++-v3/config/os/picolibc/ctype_base.h b/libstdc++-v3/config/os/picolibc/ctype_base.h new file mode 100644 index 000000000000..bcc9e8df4a36 --- /dev/null +++ b/libstdc++-v3/config/os/picolibc/ctype_base.h @@ -0,0 +1,61 @@ +// Locale support for picolibc -*- C++ -*- + +// Copyright (C) 2000-2026 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +// +// ISO C++ 14882: 22.1 Locales +// + +// Information as gleaned from /usr/include/ctype.h + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + /// @brief Base class for ctype. + struct ctype_base + { + // Non-standard typedefs. + typedef const int* __to_type; + + // NB: Offsets into ctype<char>::_M_table force a particular size + // on the mask type. Because of this, we don't use an enum. + typedef short mask; + static const mask upper = mask (__CTYPE_UPPER); + static const mask lower = mask (__CTYPE_LOWER); + static const mask alpha = mask (__CTYPE_UPPER | __CTYPE_LOWER); + static const mask digit = mask (__CTYPE_DIGIT); + static const mask xdigit = mask (__CTYPE_HEX | __CTYPE_DIGIT); + static const mask space = mask (__CTYPE_SPACE); + static const mask print = mask (__CTYPE_PUNCT | __CTYPE_UPPER | __CTYPE_LOWER | __CTYPE_DIGIT | __CTYPE_BLANK); + static const mask graph = mask (__CTYPE_PUNCT | __CTYPE_UPPER | __CTYPE_LOWER | __CTYPE_DIGIT); + static const mask cntrl = mask (__CTYPE_CNTRL); + static const mask punct = mask (__CTYPE_PUNCT); + static const mask alnum = mask (__CTYPE_UPPER | __CTYPE_LOWER | __CTYPE_DIGIT); +#if __cplusplus >= 201103L + static const mask blank = mask (__CTYPE_BLANK | __CTYPE_TAB); +#endif + }; + +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace diff --git a/libstdc++-v3/config/os/picolibc/ctype_configure_char.cc b/libstdc++-v3/config/os/picolibc/ctype_configure_char.cc new file mode 100644 index 000000000000..7b38fac24227 --- /dev/null +++ b/libstdc++-v3/config/os/picolibc/ctype_configure_char.cc @@ -0,0 +1,105 @@ +// Locale support for picolibc -*- C++ -*- + +// Copyright (C) 2011-2026 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +/** @file ctype_configure_char.cc */ + +// +// ISO C++ 14882: 22.1 Locales +// + +#include <locale> +#include <cstdlib> +#include <cstring> + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + +// Information as gleaned from /usr/include/ctype.h + + const ctype_base::mask* + ctype<char>::classic_table() throw() + { return _ctype_wide + 1; } + + ctype<char>::ctype(__c_locale, const mask* __table, bool __del, + size_t __refs) + : facet(__refs), _M_del(__table != 0 && __del), + _M_toupper(NULL), _M_tolower(NULL), + _M_table(__table ? __table : classic_table()) + { + memset(_M_widen, 0, sizeof(_M_widen)); + _M_widen_ok = 0; + memset(_M_narrow, 0, sizeof(_M_narrow)); + _M_narrow_ok = 0; + } + + ctype<char>::ctype(const mask* __table, bool __del, size_t __refs) + : facet(__refs), _M_del(__table != 0 && __del), + _M_toupper(NULL), _M_tolower(NULL), + _M_table(__table ? __table : classic_table()) + { + memset(_M_widen, 0, sizeof(_M_widen)); + _M_widen_ok = 0; + memset(_M_narrow, 0, sizeof(_M_narrow)); + _M_narrow_ok = 0; + } + + char + ctype<char>::do_toupper(char __c) const + { + int __x = __c; + return (this->is(ctype_base::lower, __c) ? (__x - 'a' + 'A') : __x); + } + + const char* + ctype<char>::do_toupper(char* __low, const char* __high) const + { + while (__low < __high) + { + *__low = this->do_toupper(*__low); + ++__low; + } + return __high; + } + + char + ctype<char>::do_tolower(char __c) const + { + int __x = __c; + return (this->is(ctype_base::upper, __c) ? (__x - 'A' + 'a') : __x); + } + + const char* + ctype<char>::do_tolower(char* __low, const char* __high) const + { + while (__low < __high) + { + *__low = this->do_tolower(*__low); + ++__low; + } + return __high; + } + +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace diff --git a/libstdc++-v3/config/os/picolibc/ctype_inline.h b/libstdc++-v3/config/os/picolibc/ctype_inline.h new file mode 100644 index 000000000000..7903214883bc --- /dev/null +++ b/libstdc++-v3/config/os/picolibc/ctype_inline.h @@ -0,0 +1,74 @@ +// Locale support for picolibc -*- C++ -*- + +// Copyright (C) 2000-2026 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +/** @file bits/ctype_inline.h + * This is an internal header file, included by other library headers. + * Do not attempt to use it directly. @headername{locale} + */ + +// +// ISO C++ 14882: 22.1 Locales +// + +// ctype bits to be inlined go here. Non-inlinable (ie virtual do_*) +// functions go in ctype.cc + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + bool + ctype<char>:: + is(mask __m, char __c) const + { return _M_table[static_cast<unsigned char>(__c)] & __m; } + + const char* + ctype<char>:: + is(const char* __low, const char* __high, mask* __vec) const + { + while (__low < __high) + *__vec++ = _M_table[static_cast<unsigned char>(*__low++)]; + return __high; + } + + const char* + ctype<char>:: + scan_is(mask __m, const char* __low, const char* __high) const + { + while (__low < __high && !this->is(__m, *__low)) + ++__low; + return __low; + } + + const char* + ctype<char>:: + scan_not(mask __m, const char* __low, const char* __high) const + { + while (__low < __high && this->is(__m, *__low) != 0) + ++__low; + return __low; + } + +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace diff --git a/libstdc++-v3/config/os/picolibc/os_defines.h b/libstdc++-v3/config/os/picolibc/os_defines.h new file mode 100644 index 000000000000..e9c611b4fb57 --- /dev/null +++ b/libstdc++-v3/config/os/picolibc/os_defines.h @@ -0,0 +1,36 @@ +// Specific definitions for picolibc -*- C++ -*- + +// Copyright (C) 2026 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// <http://www.gnu.org/licenses/>. + +/** @file bits/os_defines.h + * This is an internal header file, included by other library headers. + * Do not attempt to use it directly. @headername{iosfwd} + */ + +#ifndef _GLIBCXX_OS_DEFINES +#define _GLIBCXX_OS_DEFINES 1 + +// System-specific #define, typedefs, corrections, etc, go here. This +// file will come before all others. + +#endif diff --git a/libstdc++-v3/configure b/libstdc++-v3/configure index 3d825a69fa2e..4f144382e1b1 100755 --- a/libstdc++-v3/configure +++ b/libstdc++-v3/configure @@ -922,6 +922,7 @@ enable_largefile with_target_subdir with_cross_host with_newlib +with_picolibc enable_maintainer_mode enable_shared enable_static @@ -1698,6 +1699,7 @@ Optional Packages: configuring in a subdirectory --with-cross-host=HOST configuring with a cross compiler --with-newlib assume newlib as a system C library + --with-picolibc assume picolibc as a system C library --with-pic try to use only PIC/non-PIC objects [default=use both] --with-gnu-ld assume the C compiler uses GNU ld [default=no] @@ -5572,6 +5574,13 @@ if test "${with_newlib+set}" = set; then : fi + +# Check whether --with-picolibc was given. +if test "${with_picolibc+set}" = set; then : + withval=$with_picolibc; +fi + + # Will set LN_S to either 'ln -s', 'ln', or 'cp -p' (if linking isn't # available). Uncomment the next line to force a particular method. { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ln -s works" >&5 @@ -5944,6 +5953,7 @@ $as_echo "$as_me: OS config directory is $os_include_dir" >&6;} # Libtool setup. if test "x${with_newlib}" != "xyes" && + test "x${with_picolibc}" != "xyes" && test "x${with_avrlibc}" != "xyes" && test "x$with_headers" != "xno"; then enable_dlopen=yes @@ -12669,7 +12679,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 12672 "configure" +#line 12682 "configure" #include "confdefs.h" #if HAVE_DLFCN_H @@ -12775,7 +12785,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 12778 "configure" +#line 12788 "configure" #include "confdefs.h" #if HAVE_DLFCN_H @@ -16458,7 +16468,7 @@ $as_echo "$glibcxx_cv_atomic_word" >&6; } # Fake what AC_TRY_COMPILE does. cat > conftest.$ac_ext << EOF -#line 16461 "configure" +#line 16471 "configure" #include "${glibcxx_srcdir}/config/$atomic_word_dir/atomic_word.h" int main() { @@ -16604,7 +16614,7 @@ $as_echo "mutex" >&6; } # unnecessary for this test. cat > conftest.$ac_ext << EOF -#line 16607 "configure" +#line 16617 "configure" int main() { _Decimal32 d1; @@ -16646,7 +16656,7 @@ ac_compiler_gnu=$ac_cv_cxx_compiler_gnu # unnecessary for this test. cat > conftest.$ac_ext << EOF -#line 16649 "configure" +#line 16659 "configure" template<typename T1, typename T2> struct same { typedef T2 type; }; @@ -28831,6 +28841,136 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext glibcxx_cv_mkdir=yes ;; esac + elif test "x${with_picolibc}" = "xyes"; then + os_include_dir="os/picolibc" + $as_echo "#define HAVE_HYPOT 1" >>confdefs.h + + + # GLIBCXX_CHECK_STDLIB_SUPPORT + $as_echo "#define HAVE_STRTOF 1" >>confdefs.h + + + $as_echo "#define HAVE_ACOSF 1" >>confdefs.h + + $as_echo "#define HAVE_ASINF 1" >>confdefs.h + + $as_echo "#define HAVE_ATAN2F 1" >>confdefs.h + + $as_echo "#define HAVE_ATANF 1" >>confdefs.h + + $as_echo "#define HAVE_CEILF 1" >>confdefs.h + + $as_echo "#define HAVE_COSF 1" >>confdefs.h + + $as_echo "#define HAVE_COSHF 1" >>confdefs.h + + $as_echo "#define HAVE_EXPF 1" >>confdefs.h + + $as_echo "#define HAVE_FABSF 1" >>confdefs.h + + $as_echo "#define HAVE_FLOORF 1" >>confdefs.h + + $as_echo "#define HAVE_FMODF 1" >>confdefs.h + + $as_echo "#define HAVE_FREXPF 1" >>confdefs.h + + $as_echo "#define HAVE_HYPOTF 1" >>confdefs.h + + $as_echo "#define HAVE_LDEXPF 1" >>confdefs.h + + $as_echo "#define HAVE_LOG10F 1" >>confdefs.h + + $as_echo "#define HAVE_LOGF 1" >>confdefs.h + + $as_echo "#define HAVE_MODFF 1" >>confdefs.h + + $as_echo "#define HAVE_POWF 1" >>confdefs.h + + $as_echo "#define HAVE_SINF 1" >>confdefs.h + + $as_echo "#define HAVE_SINHF 1" >>confdefs.h + + $as_echo "#define HAVE_SQRTF 1" >>confdefs.h + + $as_echo "#define HAVE_TANF 1" >>confdefs.h + + $as_echo "#define HAVE_TANHF 1" >>confdefs.h + + + # Support for iconv in picolibc is configurable. + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include <picolibc.h> +int +main () +{ + + #ifndef _ICONV_ENABLED + #error + #endif + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + ac_picolibc_iconv_enabled=yes +else + ac_picolibc_iconv_enabled=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + if test "$ac_picolibc_iconv_enabled" = yes; then + $as_echo "#define HAVE_ICONV 1" >>confdefs.h + + fi + + $as_echo "#define HAVE_MEMALIGN 1" >>confdefs.h + + + # Check for TLS + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include <picolibc.h> +int +main () +{ + + #ifndef __THREAD_LOCAL_STORAGE + #error + #endif + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + ac_picolibc_tls_enabled=yes +else + ac_picolibc_tls_enabled=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + if test "$ac_picolibc_tls_enabled" = yes; then + $as_echo "#define HAVE_TLS 1" >>confdefs.h + + fi + + $as_echo "#define HAVE_ALIGNED_ALLOC 1" >>confdefs.h + + $as_echo "#define HAVE_AT_QUICK_EXIT 1" >>confdefs.h + + $as_echo "#define HAVE_LINK 1" >>confdefs.h + + $as_echo "#define HAVE_SYS_STAT_H 1" >>confdefs.h + + $as_echo "#define HAVE_SYS_TYPES_H 1" >>confdefs.h + + $as_echo "#define HAVE_SETENV 1" >>confdefs.h + + $as_echo "#define HAVE_STRERROR_L 1" >>confdefs.h + + $as_echo "#define HAVE_S_ISREG 1" >>confdefs.h + + $as_echo "#define HAVE_UNISTD_H 1" >>confdefs.h + + elif test "x$with_headers" != "xno"; then # Base decisions on target environment. @@ -54005,7 +54145,7 @@ $as_echo "$glibcxx_cv_libbacktrace_atomics" >&6; } CXXFLAGS='-O0 -S' cat > conftest.$ac_ext << EOF -#line 54008 "configure" +#line 54148 "configure" #include <stddef.h> int main() { diff --git a/libstdc++-v3/configure.ac b/libstdc++-v3/configure.ac index 339e7bb17a4d..f81dccdeef70 100644 --- a/libstdc++-v3/configure.ac +++ b/libstdc++-v3/configure.ac @@ -100,6 +100,7 @@ GLIBCXX_CONFIGURE # Libtool setup. if test "x${with_newlib}" != "xyes" && + test "x${with_picolibc}" != "xyes" && test "x${with_avrlibc}" != "xyes" && test "x$with_headers" != "xno"; then AC_LIBTOOL_DLOPEN @@ -428,6 +429,67 @@ dnl # rather than hardcoding that information. glibcxx_cv_mkdir=yes ;; esac + elif test "x${with_picolibc}" = "xyes"; then + os_include_dir="os/picolibc" + AC_DEFINE(HAVE_HYPOT) + + # GLIBCXX_CHECK_STDLIB_SUPPORT + AC_DEFINE(HAVE_STRTOF) + + AC_DEFINE(HAVE_ACOSF) + AC_DEFINE(HAVE_ASINF) + AC_DEFINE(HAVE_ATAN2F) + AC_DEFINE(HAVE_ATANF) + AC_DEFINE(HAVE_CEILF) + AC_DEFINE(HAVE_COSF) + AC_DEFINE(HAVE_COSHF) + AC_DEFINE(HAVE_EXPF) + AC_DEFINE(HAVE_FABSF) + AC_DEFINE(HAVE_FLOORF) + AC_DEFINE(HAVE_FMODF) + AC_DEFINE(HAVE_FREXPF) + AC_DEFINE(HAVE_HYPOTF) + AC_DEFINE(HAVE_LDEXPF) + AC_DEFINE(HAVE_LOG10F) + AC_DEFINE(HAVE_LOGF) + AC_DEFINE(HAVE_MODFF) + AC_DEFINE(HAVE_POWF) + AC_DEFINE(HAVE_SINF) + AC_DEFINE(HAVE_SINHF) + AC_DEFINE(HAVE_SQRTF) + AC_DEFINE(HAVE_TANF) + AC_DEFINE(HAVE_TANHF) + + # Support for iconv in picolibc is configurable. + AC_TRY_COMPILE([#include <picolibc.h>], [ + #ifndef _ICONV_ENABLED + #error + #endif], [ac_picolibc_iconv_enabled=yes], [ac_picolibc_iconv_enabled=no]) + if test "$ac_picolibc_iconv_enabled" = yes; then + AC_DEFINE(HAVE_ICONV) + fi + + AC_DEFINE(HAVE_MEMALIGN) + + # Check for TLS + AC_TRY_COMPILE([#include <picolibc.h>], [ + #ifndef __THREAD_LOCAL_STORAGE + #error + #endif], [ac_picolibc_tls_enabled=yes], [ac_picolibc_tls_enabled=no]) + if test "$ac_picolibc_tls_enabled" = yes; then + AC_DEFINE(HAVE_TLS) + fi + + AC_DEFINE(HAVE_ALIGNED_ALLOC) + AC_DEFINE(HAVE_AT_QUICK_EXIT) + AC_DEFINE(HAVE_LINK) + AC_DEFINE(HAVE_SYS_STAT_H) + AC_DEFINE(HAVE_SYS_TYPES_H) + AC_DEFINE(HAVE_SETENV) + AC_DEFINE(HAVE_STRERROR_L) + AC_DEFINE(HAVE_S_ISREG) + AC_DEFINE(HAVE_UNISTD_H) + elif test "x$with_headers" != "xno"; then GLIBCXX_CROSSCONFIG fi
