[PATCH] libstdc++: Avoid '-Wtype-limits' warnings in 'libstdc++-v3/config/locale/newlib/ctype_members.cc'

Jonathan Wakely jwakely@redhat.com
Mon Nov 9 09:10:55 GMT 2026


On Mon, 9 Nov 2026 at 09:08, Thomas Schwinge <tschwinge@baylibre.com> wrote:
>
> In a '--enable-werror' build for '--target=arm-rtems6', I ran into:
>
>     ctype_members.cc: In member function ‘virtual char std::ctype<wchar_t>::do_narrow(wchar_t, char) const’:
>     ctype_members.cc:216:14: error: comparison of unsigned expression in ‘>= 0’ is always true [-Werror=type-limits]
>       216 |     if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
>           |         ~~~~~^~~~
>     ctype_members.cc: In member function ‘virtual const wchar_t* std::ctype<wchar_t>::do_narrow(const wchar_t*, const wchar_t*, char, char*) const’:
>     ctype_members.cc:230:21: error: comparison of unsigned expression in ‘>= 0’ is always true [-Werror=type-limits]
>       230 |           if (*__lo >= 0 && *__lo < 128)
>           |               ~~~~~~^~~~
>     cc1plus: all warnings being treated as errors
>     make[5]: *** [Makefile:686: ctype_members.lo] Error 1
>     make[5]: Leaving directory '[...]/build-gcc/arm-rtems6/libstdc++-v3/src/c++11'
>
> That's '[...]/ctype_members.cc' ->
> '[...]/source-gcc/libstdc++-v3/config/locale/newlib/ctype_members.cc'.
> Apply to that file the very same patch as had been applied to
> 'libstdc++-v3/config/locale/gnu/ctype_members.cc' in
> commit 975025de350bb8cc264fef0a5f88f71abe5b3791
> "libstdc++: Avoid -Wtype-limits warnings in locale/gnu/ctype_members.cc".

OK thanks


>
>         libstdc++-v3/
>         * config/locale/newlib/ctype_members.cc (use_table): New function.
>         (ctype<wchar_t>::do_narrow): Use use_table.
>
> Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
> ---
>  libstdc++-v3/config/locale/newlib/ctype_members.cc | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/libstdc++-v3/config/locale/newlib/ctype_members.cc b/libstdc++-v3/config/locale/newlib/ctype_members.cc
> index acffeccaf3b1..19191d618058 100644
> --- a/libstdc++-v3/config/locale/newlib/ctype_members.cc
> +++ b/libstdc++-v3/config/locale/newlib/ctype_members.cc
> @@ -209,11 +209,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>      return __hi;
>    }
>
> +  // True if c can be looked up in _M_narrow.
> +  [[gnu::always_inline]]
> +  static inline bool
> +  use_table(wchar_t c)
> +  {
> +    using U = std::make_unsigned<wchar_t>::type;
> +    return U(c) < 128;
> +  }
> +
>    char
>    ctype<wchar_t>::
>    do_narrow(wchar_t __wc, char __dfault) const
>    {
> -    if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
> +    if (use_table(__wc) && _M_narrow_ok)
>        return _M_narrow[__wc];
>      const int __c = wctob(__wc);
>      return (__c == EOF ? __dfault : static_cast<char>(__c));
> @@ -227,7 +236,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>      if (_M_narrow_ok)
>        while (__lo < __hi)
>         {
> -         if (*__lo >= 0 && *__lo < 128)
> +         if (use_table(*__lo))
>             *__dest = _M_narrow[*__lo];
>           else
>             {
> --
> 2.53.0
>



More information about the Libstdc++ mailing list