[libc-commits] [libc] [libc] Make cpp::byte alias-safe (PR #194171)
via libc-commits
libc-commits at lists.llvm.org
Sat Apr 25 12:15:18 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Mikhail R. Gadelha (mikhailramalho)
<details>
<summary>Changes</summary>
Change LIBC_NAMESPACE::cpp::byte from an enum-backed type to unsigned char so libc’s raw-memory utilities and sorting code can legally access object representations without violating C++ strict-aliasing rules.
---
Full diff: https://github.com/llvm/llvm-project/pull/194171.diff
2 Files Affected:
- (modified) libc/src/__support/CPP/cstddef.h (+1-25)
- (modified) libc/test/src/__support/CPP/cstddef_test.cpp (+2-2)
``````````diff
diff --git a/libc/src/__support/CPP/cstddef.h b/libc/src/__support/CPP/cstddef.h
index ed6c9d03362f4..873f4a37c5b36 100644
--- a/libc/src/__support/CPP/cstddef.h
+++ b/libc/src/__support/CPP/cstddef.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace cpp {
-enum class byte : unsigned char {};
+using byte = unsigned char;
template <class IntegerType>
LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte>
@@ -38,30 +38,6 @@ LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
operator<<=(byte &b, IntegerType shift) noexcept {
return b = b << shift;
}
-LIBC_INLINE constexpr byte operator|(byte l, byte r) noexcept {
- return static_cast<byte>(static_cast<unsigned char>(l) |
- static_cast<unsigned char>(r));
-}
-LIBC_INLINE constexpr byte &operator|=(byte &l, byte r) noexcept {
- return l = l | r;
-}
-LIBC_INLINE constexpr byte operator&(byte l, byte r) noexcept {
- return static_cast<byte>(static_cast<unsigned char>(l) &
- static_cast<unsigned char>(r));
-}
-LIBC_INLINE constexpr byte &operator&=(byte &l, byte r) noexcept {
- return l = l & r;
-}
-LIBC_INLINE constexpr byte operator^(byte l, byte r) noexcept {
- return static_cast<byte>(static_cast<unsigned char>(l) ^
- static_cast<unsigned char>(r));
-}
-LIBC_INLINE constexpr byte &operator^=(byte &l, byte r) noexcept {
- return l = l ^ r;
-}
-LIBC_INLINE constexpr byte operator~(byte b) noexcept {
- return static_cast<byte>(~static_cast<unsigned char>(b));
-}
template <typename IntegerType>
LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, IntegerType>
to_integer(byte b) noexcept {
diff --git a/libc/test/src/__support/CPP/cstddef_test.cpp b/libc/test/src/__support/CPP/cstddef_test.cpp
index eceabaa815983..db24bcc705f50 100644
--- a/libc/test/src/__support/CPP/cstddef_test.cpp
+++ b/libc/test/src/__support/CPP/cstddef_test.cpp
@@ -30,8 +30,8 @@ TEST(LlvmLibcByteTest, bitwise) {
ASSERT_EQ(b, byte{0b01010100});
b >>= 1;
- ASSERT_EQ((b << 1), byte{0b01010100});
- ASSERT_EQ((b >> 1), byte{0b00010101});
+ ASSERT_EQ(static_cast<unsigned int>(b << 1), 0b01010100U);
+ ASSERT_EQ(static_cast<unsigned int>(b >> 1), 0b00010101U);
b |= byte{0b11110000};
ASSERT_EQ(b, byte{0b11111010});
``````````
</details>
https://github.com/llvm/llvm-project/pull/194171
More information about the libc-commits
mailing list