Skip to content

Commit 2cc19b2

Browse files
committed
[libc] Remove btowc / wctob from wctype_utils internal header.
They are not used anywhere except for the btowc/wctob entrypoints, so just move the implementation there. Internal code should probably be using a safer mbrtowc variants anyway, if applicable. This allows us to remove the use of wint_t, which is problematic for some uses through `libc/shared/` when a host system doesn't have wide-character support (see PR #165884 comments). There's no such problems with `wchar_t`, since it's a fundamental type in C++.
1 parent 28ac6b3 commit 2cc19b2

File tree

6 files changed

+8
-45
lines changed

6 files changed

+8
-45
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ add_header_library(
162162
wctype_utils.h
163163
DEPENDS
164164
libc.hdr.types.wchar_t
165-
libc.hdr.types.wint_t
166165
)
167166

168167
add_header_library(

libc/src/__support/wctype_utils.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#define LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
1111

1212
#include "hdr/types/wchar_t.h"
13-
#include "hdr/types/wint_t.h"
14-
#include "src/__support/CPP/optional.h"
1513
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1614
#include "src/__support/macros/config.h"
1715

@@ -584,30 +582,6 @@ is_char_or_wchar(wchar_t ch, [[maybe_unused]] char, wchar_t wc_value) {
584582
return (ch == wc_value);
585583
}
586584

587-
// ------------------------------------------------------
588-
// Rationale: Since these classification functions are
589-
// called in other functions, we will avoid the overhead
590-
// of a function call by inlining them.
591-
// ------------------------------------------------------
592-
593-
LIBC_INLINE cpp::optional<int> wctob(wint_t c) {
594-
// This needs to be translated to EOF at the callsite. This is to avoid
595-
// including stdio.h in this file.
596-
// The standard states that wint_t may either be an alias of wchar_t or
597-
// an alias of an integer type, different platforms define this type with
598-
// different signedness. This is equivalent to `(c > 127) || (c < 0)` but also
599-
// works without -Wtype-limits warnings when `wint_t` is unsigned.
600-
if ((c & ~127) != 0)
601-
return cpp::nullopt;
602-
return static_cast<int>(c);
603-
}
604-
605-
LIBC_INLINE cpp::optional<wint_t> btowc(int c) {
606-
if (c > 127 || c < 0)
607-
return cpp::nullopt;
608-
return static_cast<wint_t>(c);
609-
}
610-
611585
} // namespace internal
612586
} // namespace LIBC_NAMESPACE_DECL
613587

libc/src/wchar/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ add_entrypoint_object(
4040
DEPENDS
4141
libc.hdr.types.wint_t
4242
libc.hdr.stdio_macros
43-
libc.src.__support.wctype_utils
4443
)
4544

4645
add_entrypoint_object(
@@ -52,7 +51,6 @@ add_entrypoint_object(
5251
DEPENDS
5352
libc.hdr.types.wint_t
5453
libc.hdr.wchar_macros
55-
libc.src.__support.wctype_utils
5654
)
5755

5856
add_entrypoint_object(

libc/src/wchar/btowc.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@
99
#include "src/wchar/btowc.h"
1010
#include "src/__support/common.h"
1111
#include "src/__support/macros/config.h"
12-
#include "src/__support/wctype_utils.h"
1312

1413
#include "hdr/types/wint_t.h"
1514
#include "hdr/wchar_macros.h" // for WEOF.
1615

1716
namespace LIBC_NAMESPACE_DECL {
1817

1918
LLVM_LIBC_FUNCTION(wint_t, btowc, (int c)) {
20-
auto result = internal::btowc(c);
21-
if (result.has_value()) {
22-
return result.value();
23-
} else {
19+
if (c > 127 || c < 0)
2420
return WEOF;
25-
}
21+
return static_cast<wint_t>(c);
2622
}
2723

2824
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wctob.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
#include "src/wchar/wctob.h"
1010
#include "src/__support/common.h"
1111
#include "src/__support/macros/config.h"
12-
#include "src/__support/wctype_utils.h"
1312

1413
#include "hdr/stdio_macros.h" // for EOF.
1514
#include "hdr/types/wint_t.h"
1615

1716
namespace LIBC_NAMESPACE_DECL {
1817

1918
LLVM_LIBC_FUNCTION(int, wctob, (wint_t c)) {
20-
auto result = internal::wctob(c);
21-
if (result.has_value()) {
22-
return result.value();
23-
} else {
19+
// The standard states that wint_t may either be an alias of wchar_t or
20+
// an alias of an integer type, different platforms define this type with
21+
// different signedness. This is equivalent to `(c > 127) || (c < 0)` but also
22+
// works without -Wtype-limits warnings when `wint_t` is unsigned.
23+
if ((c & ~127) != 0)
2424
return EOF;
25-
}
25+
return static_cast<int>(c);
2626
}
2727

2828
} // namespace LIBC_NAMESPACE_DECL

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,11 +1799,9 @@ libc_support_library(
17991799
name = "__support_wctype_utils",
18001800
hdrs = ["src/__support/wctype_utils.h"],
18011801
deps = [
1802-
":__support_cpp_optional",
18031802
":__support_macros_attributes",
18041803
":__support_macros_config",
18051804
":types_wchar_t",
1806-
":types_wint_t",
18071805
],
18081806
)
18091807

@@ -7472,7 +7470,6 @@ libc_function(
74727470
deps = [
74737471
":__support_common",
74747472
":__support_macros_config",
7475-
":__support_wctype_utils",
74767473
":hdr_wchar_macros",
74777474
":types_wint_t",
74787475
],
@@ -7704,7 +7701,6 @@ libc_function(
77047701
":__support_common",
77057702
":__support_macros_config",
77067703
":__support_macros_null_check",
7707-
":__support_wctype_utils",
77087704
":hdr_stdio_macros",
77097705
":types_wint_t",
77107706
],

0 commit comments

Comments
 (0)