Skip to content

Commit 079ca05

Browse files
[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
1 parent 07157db commit 079ca05

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

libc/src/__support/wctype_utils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ LIBC_INLINE cpp::optional<int> wctob(wint_t c) {
2929
// This needs to be translated to EOF at the callsite. This is to avoid
3030
// including stdio.h in this file.
3131
// The standard states that wint_t may either be an alias of wchar_t or
32-
// an alias of an integer type, so we need to keep the c < 0 check.
33-
if (c > 127 || c < 0)
32+
// an alias of an integer type, different platforms define this type with
33+
// different signedness. This is equivalent to `(c > 127) || (c < 0)` but also
34+
// works without -Wtype-limits warnings when `wint_t` is unsigned.
35+
if ((c & ~127) != 0)
3436
return cpp::nullopt;
3537
return static_cast<int>(c);
3638
}

0 commit comments

Comments
 (0)