Skip to content

[libc] fix -Wtype-limits in wctob #74511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 5, 2023
Merged

Conversation

nickdesaulniers
Copy link
Member

@nickdesaulniers nickdesaulniers commented Dec 5, 2023

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

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
@llvmbot llvmbot added the libc label Dec 5, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 5, 2023

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/74511.diff

1 Files Affected:

  • (modified) libc/src/__support/wctype_utils.h (+6-1)
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);
 }
 

@nickdesaulniers
Copy link
Member Author

my SFINAE skills are rusty, but I wonder if std::is_signed can be used here?

@jhuber6
Copy link
Contributor

jhuber6 commented Dec 5, 2023

my SFINAE skills are rusty, but I wonder if std::is_signed can be used here?

I was just about to comment the same thing. We should be able to do something like this

if constexpr (cpp::is_signed<wint_t>) {
} else {
}

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nit

@SchrodingerZhu
Copy link
Contributor

SchrodingerZhu commented Dec 5, 2023

@michaelrj-google sure! I only wanted to use that to remove the signedness warning :D

@nickdesaulniers
Copy link
Member Author

my SFINAE skills are rusty, but I wonder if std::is_signed can be used here?

I was just about to comment the same thing. We should be able to do something like this

if constexpr (cpp::is_signed<wint_t>) {
} else {
}

cool! Done in f3a22d4

@nickdesaulniers nickdesaulniers changed the title [libc] disable -Wtype-limits in wctob [libc] fix -Wtype-limits in wctob Dec 5, 2023
@nickdesaulniers nickdesaulniers marked this pull request as draft December 5, 2023 20:35
@nickdesaulniers nickdesaulniers marked this pull request as ready for review December 5, 2023 21:04
Copy link

github-actions bot commented Dec 5, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version is fine, though we could also static cast to an unsigned type and just check if the resulting value is less than 127. Either is fine, feel free to land.

@nickdesaulniers nickdesaulniers merged commit 079ca05 into llvm:main Dec 5, 2023
@nickdesaulniers nickdesaulniers deleted the wint_t branch December 5, 2023 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants