[libc-commits] [libc] 079ca05 - [libc] fix -Wtype-limits in wctob (#74511)

via libc-commits libc-commits at lists.llvm.org
Tue Dec 5 15:37:30 PST 2023


Author: Nick Desaulniers
Date: 2023-12-05T15:37:27-08:00
New Revision: 079ca05eea35745b2c362c218069dcc1c33982b9

URL: https://github.com/llvm/llvm-project/commit/079ca05eea35745b2c362c218069dcc1c33982b9
DIFF: https://github.com/llvm/llvm-project/commit/079ca05eea35745b2c362c218069dcc1c33982b9.diff

LOG: [libc] fix -Wtype-limits in wctob (#74511)

GCC warns:
```
libc/src/__support/wctype_utils.h:33:20: error: comparison of unsigned
expression in ‘< 0’ is always false [-Werror=type-limits]
   33 |   if (c > 127 || c < 0)
      |                  ~~^~~
```

Looking into the signedness of wint_t, it looks like depending on the
platform,
__WINT_TYPE__ is defined to int or unsigned int depending on the
platform.

Link:
https://lab.llvm.org/buildbot/#/builders/250/builds/14891/steps/6/logs/stdio

Added: 
    

Modified: 
    libc/src/__support/wctype_utils.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/wctype_utils.h b/libc/src/__support/wctype_utils.h
index 6d825499a1b0f..aa1161c777453 100644
--- a/libc/src/__support/wctype_utils.h
+++ b/libc/src/__support/wctype_utils.h
@@ -29,8 +29,10 @@ LIBC_INLINE cpp::optional<int> wctob(wint_t c) {
   // This needs to be translated to EOF at the callsite. This is to avoid
   // including stdio.h in this file.
   // The standard states that wint_t may either be an alias of wchar_t or
-  // an alias of an integer type, so we need to keep the c < 0 check.
-  if (c > 127 || c < 0)
+  // an alias of an integer type, 
diff erent platforms define this type with
+  // 
diff erent signedness. This is equivalent to `(c > 127) || (c < 0)` but also
+  // works without -Wtype-limits warnings when `wint_t` is unsigned.
+  if ((c & ~127) != 0)
     return cpp::nullopt;
   return static_cast<int>(c);
 }


        


More information about the libc-commits mailing list