]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Avoid '-Wtype-limits' warnings in 'libstdc++-v3/config/locale/generic...
authorThomas Schwinge <tschwinge@baylibre.com>
Sat, 4 Jul 2026 10:06:41 +0000 (10:06 +0000)
committerThomas Schwinge <tschwinge@baylibre.com>
Mon, 6 Jul 2026 07:38:51 +0000 (09:38 +0200)
In a GCC/AIX build I ran into:

    ctype_members.cc: In member function 'virtual char std::ctype<wchar_t>::do_narrow(wchar_t, char) const':
    ctype_members.cc:212:14: error: comparison is always true due to limited range of data type [-Werror=type-limits]
      212 |     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:226:21: error: comparison is always true due to limited range of data type [-Werror=type-limits]
      226 |           if (*__lo >= 0 && *__lo < 128)
          |               ~~~~~~^~~~
    cc1plus: all warnings being treated as errors
    make[5]: *** [Makefile:685: ctype_members.lo] Error 1
    make[5]: Target 'all' not remade because of errors.
    make[5]: Leaving directory '[...]/build-gcc/powerpc-ibm-aix7.3.1.0/libstdc++-v3/src/c++11'

That's '[...]/ctype_members.cc' ->
'[...]/source-gcc/libstdc++-v3/config/locale/generic/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".

libstdc++-v3/
* config/locale/generic/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/generic/ctype_members.cc

index 2d61347feb1cec00d483efb9c25ae46015946cd6..3f788a63e2047b0516a5564f1258381c7a04bfd2 100644 (file)
@@ -205,11 +205,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));
@@ -223,7 +232,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
            {