[libc] [llvm] [libc] Migrate wctype_utils to use wchar_t where applicable. (PR #166234)
Alexey Samsonov via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 3 14:51:07 PST 2025
https://github.com/vonosmas updated https://github.com/llvm/llvm-project/pull/166234
>From caca542d4bd7d6949fd5cfe67ff12487b3177c3b Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas at gmail.com>
Date: Mon, 3 Nov 2025 20:44:44 +0000
Subject: [PATCH 1/3] [libc] Migrate wctype_utils to use wchar_t where
applicable.
This is a counterpart of https://github.com/llvm/llvm-project/pull/166225
but for wctype_utils (which are not yet widely used). For now, I'm just
changing the types from wint_t to wchar_t to match the regular ctype_utils
change. The next change may rename most of the functions to match
the name of ctype_utils variants, so that we could be calling them from
the templated code operating on "const char*" and "const wchar_t*"
strings, and the right function signature would be picked up.
---
libc/src/__support/CMakeLists.txt | 1 +
libc/src/__support/wctype_utils.h | 23 ++++++++++++-----------
libc/src/wctype/iswalpha.cpp | 4 +++-
libc/test/src/wchar/WcstolTest.h | 24 ++++++++++++------------
4 files changed, 28 insertions(+), 24 deletions(-)
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index b7af751ec3f27..96874702b1fdf 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -161,6 +161,7 @@ add_header_library(
HDRS
wctype_utils.h
DEPENDS
+ libc.hdr.types.wchar_t
libc.hdr.types.wint_t
)
diff --git a/libc/src/__support/wctype_utils.h b/libc/src/__support/wctype_utils.h
index 2ae5ec93b2a63..60b6afb928475 100644
--- a/libc/src/__support/wctype_utils.h
+++ b/libc/src/__support/wctype_utils.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
#define LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
+#include "hdr/types/wchar_t.h"
#include "hdr/types/wint_t.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
@@ -30,7 +31,7 @@ namespace internal {
// Similarly, do not change these fumarks to show your new solution is faster,
// as well as a way to support non-Anctions to use case ranges. e.g.
-// bool iswlower(wint_t ch) {
+// bool iswlower(wchar_t ch) {
// switch(ch) {
// case L'a'...L'z':
// return true;
@@ -40,7 +41,7 @@ namespace internal {
// EBCDIC. Technically we could use some smaller ranges, but that's even harder
// to read.
-LIBC_INLINE static constexpr bool iswlower(wint_t wch) {
+LIBC_INLINE static constexpr bool iswlower(wchar_t wch) {
switch (wch) {
case L'a':
case L'b':
@@ -74,7 +75,7 @@ LIBC_INLINE static constexpr bool iswlower(wint_t wch) {
}
}
-LIBC_INLINE static constexpr bool iswupper(wint_t wch) {
+LIBC_INLINE static constexpr bool iswupper(wchar_t wch) {
switch (wch) {
case L'A':
case L'B':
@@ -108,7 +109,7 @@ LIBC_INLINE static constexpr bool iswupper(wint_t wch) {
}
}
-LIBC_INLINE static constexpr bool iswdigit(wint_t wch) {
+LIBC_INLINE static constexpr bool iswdigit(wchar_t wch) {
switch (wch) {
case L'0':
case L'1':
@@ -126,7 +127,7 @@ LIBC_INLINE static constexpr bool iswdigit(wint_t wch) {
}
}
-LIBC_INLINE static constexpr wint_t towlower(wint_t wch) {
+LIBC_INLINE static constexpr wchar_t towlower(wchar_t wch) {
switch (wch) {
case L'A':
return L'a';
@@ -185,7 +186,7 @@ LIBC_INLINE static constexpr wint_t towlower(wint_t wch) {
}
}
-LIBC_INLINE static constexpr wint_t towupper(wint_t wch) {
+LIBC_INLINE static constexpr wchar_t towupper(wchar_t wch) {
switch (wch) {
case L'a':
return L'A';
@@ -244,7 +245,7 @@ LIBC_INLINE static constexpr wint_t towupper(wint_t wch) {
}
}
-LIBC_INLINE static constexpr bool iswalpha(wint_t wch) {
+LIBC_INLINE static constexpr bool iswalpha(wchar_t wch) {
switch (wch) {
case L'a':
case L'b':
@@ -304,7 +305,7 @@ LIBC_INLINE static constexpr bool iswalpha(wint_t wch) {
}
}
-LIBC_INLINE static constexpr bool iswalnum(wint_t wch) {
+LIBC_INLINE static constexpr bool iswalnum(wchar_t wch) {
switch (wch) {
case L'a':
case L'b':
@@ -374,7 +375,7 @@ LIBC_INLINE static constexpr bool iswalnum(wint_t wch) {
}
}
-LIBC_INLINE static constexpr int b36_wchar_to_int(wint_t wch) {
+LIBC_INLINE static constexpr int b36_wchar_to_int(wchar_t wch) {
switch (wch) {
case L'0':
return 0;
@@ -479,7 +480,7 @@ LIBC_INLINE static constexpr int b36_wchar_to_int(wint_t wch) {
}
}
-LIBC_INLINE static constexpr wint_t int_to_b36_wchar(int num) {
+LIBC_INLINE static constexpr wchar_t int_to_b36_wchar(int num) {
// Can't actually use LIBC_ASSERT here because it depends on integer_to_string
// which depends on this.
@@ -562,7 +563,7 @@ LIBC_INLINE static constexpr wint_t int_to_b36_wchar(int num) {
}
}
-LIBC_INLINE static constexpr bool iswspace(wint_t wch) {
+LIBC_INLINE static constexpr bool iswspace(wchar_t wch) {
switch (wch) {
case L' ':
case L'\t':
diff --git a/libc/src/wctype/iswalpha.cpp b/libc/src/wctype/iswalpha.cpp
index 09f55d391dbff..fa2feb9c38ec3 100644
--- a/libc/src/wctype/iswalpha.cpp
+++ b/libc/src/wctype/iswalpha.cpp
@@ -14,6 +14,8 @@
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(int, iswalpha, (wint_t c)) { return internal::iswalpha(c); }
+LLVM_LIBC_FUNCTION(int, iswalpha, (wint_t c)) {
+ return internal::iswalpha(static_cast<wchar_t>(c));
+}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/wchar/WcstolTest.h b/libc/test/src/wchar/WcstolTest.h
index 4d5b752e62238..cadf9e0c42b90 100644
--- a/libc/test/src/wchar/WcstolTest.h
+++ b/libc/test/src/wchar/WcstolTest.h
@@ -178,8 +178,8 @@ struct WcstoTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
wchar_t small_string[4] = {L'\0', L'\0', L'\0', L'\0'};
for (int base = 2; base <= 36; ++base) {
for (int first_digit = 0; first_digit <= 36; ++first_digit) {
- small_string[0] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit));
+ small_string[0] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit);
if (first_digit < base) {
ASSERT_EQ(func(small_string, nullptr, base),
static_cast<ReturnT>(first_digit));
@@ -193,11 +193,11 @@ struct WcstoTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
for (int base = 2; base <= 36; ++base) {
for (int first_digit = 0; first_digit <= 36; ++first_digit) {
- small_string[0] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit));
+ small_string[0] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit);
for (int second_digit = 0; second_digit <= 36; ++second_digit) {
- small_string[1] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit));
+ small_string[1] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit);
if (first_digit < base && second_digit < base) {
ASSERT_EQ(
func(small_string, nullptr, base),
@@ -217,14 +217,14 @@ struct WcstoTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
for (int base = 2; base <= 36; ++base) {
for (int first_digit = 0; first_digit <= 36; ++first_digit) {
- small_string[0] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit));
+ small_string[0] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit);
for (int second_digit = 0; second_digit <= 36; ++second_digit) {
- small_string[1] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit));
+ small_string[1] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit);
for (int third_digit = 0; third_digit <= limit; ++third_digit) {
- small_string[2] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(third_digit));
+ small_string[2] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(third_digit);
if (first_digit < base && second_digit < base &&
third_digit < base) {
>From bfcda61dca2f31667a2f1f6a0a9914cff0019e58 Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas at gmail.com>
Date: Mon, 3 Nov 2025 20:47:16 +0000
Subject: [PATCH 2/3] update Bazel build
---
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 +
1 file changed, 1 insertion(+)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 5a1e0b53b021c..7f91ff35b70e6 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1805,6 +1805,7 @@ libc_support_library(
":__support_cpp_optional",
":__support_macros_attributes",
":__support_macros_config",
+ ":types_wchar_t",
":types_wint_t",
],
)
>From fb4f771c5707b4c8cffb6b55cac4556b0391e200 Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas at gmail.com>
Date: Mon, 3 Nov 2025 22:50:48 +0000
Subject: [PATCH 3/3] fix clang-format
---
libc/src/wctype/iswalpha.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/wctype/iswalpha.cpp b/libc/src/wctype/iswalpha.cpp
index fa2feb9c38ec3..e151363b88d0b 100644
--- a/libc/src/wctype/iswalpha.cpp
+++ b/libc/src/wctype/iswalpha.cpp
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, iswalpha, (wint_t c)) {
- return internal::iswalpha(static_cast<wchar_t>(c));
+ return internal::iswalpha(static_cast<wchar_t>(c));
}
} // namespace LIBC_NAMESPACE_DECL
More information about the llvm-commits
mailing list