[libc-commits] [libc] [libc] Implemented wmemset and added tests (PR #141691)

Uzair Nawaz via libc-commits libc-commits at lists.llvm.org
Wed May 28 09:08:34 PDT 2025


https://github.com/uzairnawaz updated https://github.com/llvm/llvm-project/pull/141691

>From 7494a421f7f8ce45aeb199e371ac42f37103a62f Mon Sep 17 00:00:00 2001
From: Uzair Nawaz <uzairnawaz at google.com>
Date: Tue, 27 May 2025 22:20:54 +0000
Subject: [PATCH 1/3] wmemset complete; TODO look into build issue

---
 libc/config/linux/x86_64/entrypoints.txt |  1 +
 libc/include/wchar.yaml                  |  8 +++++++
 libc/src/wchar/CMakeLists.txt            | 12 ++++++++++
 libc/src/wchar/wmemset.cpp               | 25 ++++++++++++++++++++
 libc/src/wchar/wmemset.h                 | 24 +++++++++++++++++++
 libc/test/src/wchar/CMakeLists.txt       | 12 ++++++++++
 libc/test/src/wchar/wmemset_test.cpp     | 30 ++++++++++++++++++++++++
 7 files changed, 112 insertions(+)
 create mode 100644 libc/src/wchar/wmemset.cpp
 create mode 100644 libc/src/wchar/wmemset.h
 create mode 100644 libc/test/src/wchar/wmemset_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9f447dd0d35d2..6a0dafd20e828 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -364,6 +364,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.wchar.btowc
     libc.src.wchar.wcslen
     libc.src.wchar.wctob
+    libc.src.wchar.wmemset
 
     # sys/uio.h entrypoints
     libc.src.sys.uio.writev
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index 0ac9aa29f0a18..ad1b2e552f219 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -27,3 +27,11 @@ functions:
     return_type: wint_t
     arguments:
       - type: int
+  - name: wmemset
+    standards:
+      - stdc
+    return_type: wchar_t*
+    arguments:
+      - type: wchar_t*
+      - type: wchar_t
+      - type: size_t
\ No newline at end of file
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 703db75b5b194..6cc6001f82db6 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -33,3 +33,15 @@ add_entrypoint_object(
     libc.hdr.wchar_macros
     libc.src.__support.wctype_utils
 )
+
+add_entrypoint_object(
+  wmemset
+  SRCS
+    wmemset.cpp
+  HDRS
+    wmemset.h
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.types.wchar_t
+    libc.src.__support.wctype_utils
+)
diff --git a/libc/src/wchar/wmemset.cpp b/libc/src/wchar/wmemset.cpp
new file mode 100644
index 0000000000000..e2f654e2a6bc5
--- /dev/null
+++ b/libc/src/wchar/wmemset.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of wmemset -----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/wchar/wmemset.h"
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(wchar_t*, wmemset, (wchar_t *s, wchar_t c, size_t n)) {
+    for (int i = 0; i < n; i++) {
+        s[i] = c;
+    }
+    return s;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
diff --git a/libc/src/wchar/wmemset.h b/libc/src/wchar/wmemset.h
new file mode 100644
index 0000000000000..51c449d16f6f8
--- /dev/null
+++ b/libc/src/wchar/wmemset.h
@@ -0,0 +1,24 @@
+//===-- Implementation header for wmemset ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_WCHAR_WMEMSET_H
+#define LLVM_LIBC_SRC_WCHAR_WMEMSET_H
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n) {
+    
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WMEMSET_H
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index d41e328fc9d90..e6eb51c8e8183 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -32,3 +32,15 @@ add_libc_test(
   DEPENDS
     libc.src.wchar.wctob
 )
+
+add_libc_test(
+  wmemset_test 
+  SUITE
+    libc_wchar_unittests
+  SRCS
+    wmemset_test.cpp
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.types.wchar_t
+    libc.src.wchar.wmemset
+)
\ No newline at end of file
diff --git a/libc/test/src/wchar/wmemset_test.cpp b/libc/test/src/wchar/wmemset_test.cpp
new file mode 100644
index 0000000000000..1dfb905fc1489
--- /dev/null
+++ b/libc/test/src/wchar/wmemset_test.cpp
@@ -0,0 +1,30 @@
+//===-- Unittests for wmemset ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/wchar/wmemset.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcWMemsetTest, SmallStringBoundCheck) {
+  wchar_t* str = new wchar_t[5];
+  for (int i = 0; i < 5; i++) {
+    str[i] = 'A';
+  }
+
+  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, 'B', 3);
+
+  EXPECT_EQ(output, str + 1);
+
+  EXPECT_EQ(str[0], (wchar_t)'A');
+  EXPECT_EQ(str[1], (wchar_t)'B');
+  EXPECT_EQ(str[2], (wchar_t)'B');
+  EXPECT_EQ(str[3], (wchar_t)'B');
+  EXPECT_EQ(str[4], (wchar_t)'A');
+}
+

>From da350d3bffd96ee1d6aae2f122c07790a61e27a8 Mon Sep 17 00:00:00 2001
From: Uzair Nawaz <uzairnawaz at google.com>
Date: Tue, 27 May 2025 23:45:34 +0000
Subject: [PATCH 2/3] updated tests to avoid using expect_eq on widechars

---
 libc/src/wchar/wmemset.cpp           |  2 +-
 libc/src/wchar/wmemset.h             |  5 +-
 libc/test/src/wchar/wmemset_test.cpp | 69 ++++++++++++++++++++++++++--
 3 files changed, 67 insertions(+), 9 deletions(-)

diff --git a/libc/src/wchar/wmemset.cpp b/libc/src/wchar/wmemset.cpp
index e2f654e2a6bc5..4b0c36db045b0 100644
--- a/libc/src/wchar/wmemset.cpp
+++ b/libc/src/wchar/wmemset.cpp
@@ -15,7 +15,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(wchar_t*, wmemset, (wchar_t *s, wchar_t c, size_t n)) {
-    for (int i = 0; i < n; i++) {
+    for (size_t i = 0; i < n; i++) {
         s[i] = c;
     }
     return s;
diff --git a/libc/src/wchar/wmemset.h b/libc/src/wchar/wmemset.h
index 51c449d16f6f8..b7067aaa188e6 100644
--- a/libc/src/wchar/wmemset.h
+++ b/libc/src/wchar/wmemset.h
@@ -15,9 +15,8 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n) {
-    
-}
+wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n);
+
 
 } // namespace LIBC_NAMESPACE_DECL
 
diff --git a/libc/test/src/wchar/wmemset_test.cpp b/libc/test/src/wchar/wmemset_test.cpp
index 1dfb905fc1489..19b8e1d95f8d0 100644
--- a/libc/test/src/wchar/wmemset_test.cpp
+++ b/libc/test/src/wchar/wmemset_test.cpp
@@ -21,10 +21,69 @@ TEST(LlvmLibcWMemsetTest, SmallStringBoundCheck) {
 
   EXPECT_EQ(output, str + 1);
 
-  EXPECT_EQ(str[0], (wchar_t)'A');
-  EXPECT_EQ(str[1], (wchar_t)'B');
-  EXPECT_EQ(str[2], (wchar_t)'B');
-  EXPECT_EQ(str[3], (wchar_t)'B');
-  EXPECT_EQ(str[4], (wchar_t)'A');
+  EXPECT_TRUE(str[0] == (wchar_t)'A');
+  EXPECT_TRUE(str[1] == (wchar_t)'B');
+  EXPECT_TRUE(str[2] == (wchar_t)'B');
+  EXPECT_TRUE(str[3] == (wchar_t)'B');
+  EXPECT_TRUE(str[4] == (wchar_t)'A');
 }
 
+TEST(LlvmLibcWMemsetTest, LargeStringBoundCheck) {
+  const int str_size = 1000;
+  wchar_t* str = new wchar_t[str_size];
+  for (int i = 0; i < str_size; i++) {
+    str[i] = 'A';
+  }
+
+  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, 'B', str_size - 2);
+
+  EXPECT_EQ(output, str + 1);
+
+  EXPECT_TRUE(str[0] == (wchar_t)'A');
+  for (int i = 1; i < str_size - 1; i++) {
+    EXPECT_TRUE(str[i] == (wchar_t)'B');
+  }
+  EXPECT_TRUE(str[str_size - 1] == (wchar_t)'A');
+}
+
+TEST(LlvmLibcWMemsetTest, WChar_Size_Small) {
+  // ensure we can handle 32 bit values 
+  wchar_t* str = new wchar_t[5];
+  const wchar_t magic = INT32_MAX;
+  
+  for (int i = 0; i < 5; i++) {
+    str[i] = 'A';
+  }
+
+  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, magic, 3);
+
+  EXPECT_EQ(output, str + 1);
+
+  EXPECT_TRUE(str[0] == (wchar_t)'A');
+  EXPECT_TRUE(str[1] == magic);
+  EXPECT_TRUE(str[2] == magic);
+  EXPECT_TRUE(str[3] == magic);
+  EXPECT_TRUE(str[4] == (wchar_t)'A');
+}
+
+TEST(LlvmLibcWMemsetTest, WChar_Size_Large) {
+  // ensure we can handle 32 bit values 
+  const int str_size = 1000;
+  const wchar_t magic = INT32_MAX;
+  wchar_t* str = new wchar_t[str_size];
+  for (int i = 0; i < str_size; i++) {
+    str[i] = 'A';
+  }
+
+  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, magic, str_size - 2);
+
+  EXPECT_EQ(output, str + 1);
+
+  EXPECT_TRUE(str[0] == (wchar_t)'A');
+  for (int i = 1; i < str_size - 1; i++) {
+    EXPECT_TRUE(str[i] == magic);
+  }
+  EXPECT_TRUE(str[str_size - 1] == (wchar_t)'A');
+}
+
+

>From 2860250b4216e27761ab1d180565f128beeb8b74 Mon Sep 17 00:00:00 2001
From: Uzair Nawaz <uzairnawaz at google.com>
Date: Wed, 28 May 2025 16:07:53 +0000
Subject: [PATCH 3/3] formatting fix

---
 libc/src/wchar/wmemset.cpp           | 11 +++++------
 libc/src/wchar/wmemset.h             |  4 ++--
 libc/test/src/wchar/wmemset_test.cpp | 24 +++++++++++-------------
 3 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/libc/src/wchar/wmemset.cpp b/libc/src/wchar/wmemset.cpp
index 4b0c36db045b0..f1a3e85cb6cd5 100644
--- a/libc/src/wchar/wmemset.cpp
+++ b/libc/src/wchar/wmemset.cpp
@@ -14,12 +14,11 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(wchar_t*, wmemset, (wchar_t *s, wchar_t c, size_t n)) {
-    for (size_t i = 0; i < n; i++) {
-        s[i] = c;
-    }
-    return s;
+LLVM_LIBC_FUNCTION(wchar_t *, wmemset, (wchar_t * s, wchar_t c, size_t n)) {
+  for (size_t i = 0; i < n; i++) {
+    s[i] = c;
+  }
+  return s;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
-
diff --git a/libc/src/wchar/wmemset.h b/libc/src/wchar/wmemset.h
index b7067aaa188e6..075a561e06488 100644
--- a/libc/src/wchar/wmemset.h
+++ b/libc/src/wchar/wmemset.h
@@ -1,4 +1,5 @@
-//===-- Implementation header for wmemset ----------------------------------===//
+//===-- Implementation header for wmemset
+//----------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -17,7 +18,6 @@ namespace LIBC_NAMESPACE_DECL {
 
 wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n);
 
-
 } // namespace LIBC_NAMESPACE_DECL
 
 #endif // LLVM_LIBC_SRC_WCHAR_WMEMSET_H
diff --git a/libc/test/src/wchar/wmemset_test.cpp b/libc/test/src/wchar/wmemset_test.cpp
index 19b8e1d95f8d0..1e156b28ac50a 100644
--- a/libc/test/src/wchar/wmemset_test.cpp
+++ b/libc/test/src/wchar/wmemset_test.cpp
@@ -12,12 +12,12 @@
 #include "test/UnitTest/Test.h"
 
 TEST(LlvmLibcWMemsetTest, SmallStringBoundCheck) {
-  wchar_t* str = new wchar_t[5];
+  wchar_t *str = new wchar_t[5];
   for (int i = 0; i < 5; i++) {
     str[i] = 'A';
   }
 
-  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, 'B', 3);
+  wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, 'B', 3);
 
   EXPECT_EQ(output, str + 1);
 
@@ -30,12 +30,12 @@ TEST(LlvmLibcWMemsetTest, SmallStringBoundCheck) {
 
 TEST(LlvmLibcWMemsetTest, LargeStringBoundCheck) {
   const int str_size = 1000;
-  wchar_t* str = new wchar_t[str_size];
+  wchar_t *str = new wchar_t[str_size];
   for (int i = 0; i < str_size; i++) {
     str[i] = 'A';
   }
 
-  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, 'B', str_size - 2);
+  wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, 'B', str_size - 2);
 
   EXPECT_EQ(output, str + 1);
 
@@ -47,15 +47,15 @@ TEST(LlvmLibcWMemsetTest, LargeStringBoundCheck) {
 }
 
 TEST(LlvmLibcWMemsetTest, WChar_Size_Small) {
-  // ensure we can handle 32 bit values 
-  wchar_t* str = new wchar_t[5];
+  // ensure we can handle 32 bit values
+  wchar_t *str = new wchar_t[5];
   const wchar_t magic = INT32_MAX;
-  
+
   for (int i = 0; i < 5; i++) {
     str[i] = 'A';
   }
 
-  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, magic, 3);
+  wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, magic, 3);
 
   EXPECT_EQ(output, str + 1);
 
@@ -67,15 +67,15 @@ TEST(LlvmLibcWMemsetTest, WChar_Size_Small) {
 }
 
 TEST(LlvmLibcWMemsetTest, WChar_Size_Large) {
-  // ensure we can handle 32 bit values 
+  // ensure we can handle 32 bit values
   const int str_size = 1000;
   const wchar_t magic = INT32_MAX;
-  wchar_t* str = new wchar_t[str_size];
+  wchar_t *str = new wchar_t[str_size];
   for (int i = 0; i < str_size; i++) {
     str[i] = 'A';
   }
 
-  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, magic, str_size - 2);
+  wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, magic, str_size - 2);
 
   EXPECT_EQ(output, str + 1);
 
@@ -85,5 +85,3 @@ TEST(LlvmLibcWMemsetTest, WChar_Size_Large) {
   }
   EXPECT_TRUE(str[str_size - 1] == (wchar_t)'A');
 }
-
-



More information about the libc-commits mailing list