[libc-commits] [libc] [libc] disable -Wtype-limits in wctob (PR #74511)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue Dec 5 10:33:38 PST 2023


https://github.com/nickdesaulniers created https://github.com/llvm/llvm-project/pull/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


>From 4d7a8b10788ecd6bd747f7d4c99a491e3fd19b59 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 5 Dec 2023 10:31:00 -0800
Subject: [PATCH] [libc] disable -Wtype-limits in wctob
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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
---
 libc/src/__support/wctype_utils.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/wctype_utils.h b/libc/src/__support/wctype_utils.h
index 6d825499a1b0f..367f5a86647fd 100644
--- a/libc/src/__support/wctype_utils.h
+++ b/libc/src/__support/wctype_utils.h
@@ -29,9 +29,14 @@ 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.
+  // an alias of an integer type where different platforms define this type with
+  // different signedness, so we need to keep the c < 0 check, hence the
+  // pragmas.
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wtype-limits"
   if (c > 127 || c < 0)
     return cpp::nullopt;
+#pragma GCC diagnostic pop
   return static_cast<int>(c);
 }
 



More information about the libc-commits mailing list