[libc-commits] [libc] [libc] wcscpy implementation (PR #142228)

via libc-commits libc-commits at lists.llvm.org
Fri May 30 16:23:22 PDT 2025


https://github.com/sribee8 updated https://github.com/llvm/llvm-project/pull/142228

>From 94948e365de92c930d95deecc7f4b584b1b8deeb Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Fri, 30 May 2025 22:41:30 +0000
Subject: [PATCH 1/3] [libc] wcscpy implementation

Implemented wcscpy as well as tests for the function.
---
 libc/config/linux/x86_64/entrypoints.txt |  1 +
 libc/include/wchar.yaml                  |  7 ++++
 libc/src/wchar/CMakeLists.txt            | 12 ++++++
 libc/src/wchar/wcscpy.cpp                | 29 ++++++++++++++
 libc/src/wchar/wcscpy.h                  | 21 +++++++++++
 libc/test/src/wchar/CMakeLists.txt       | 10 +++++
 libc/test/src/wchar/wcscpy_test.cpp      | 48 ++++++++++++++++++++++++
 7 files changed, 128 insertions(+)
 create mode 100644 libc/src/wchar/wcscpy.cpp
 create mode 100644 libc/src/wchar/wcscpy.h
 create mode 100644 libc/test/src/wchar/wcscpy_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7ddeb4d31b466..180e2c88f754d 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -370,6 +370,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.wchar.wcsspn
     libc.src.wchar.wmemcmp
     libc.src.wchar.wmemcpy
+    libc.src.wchar.wcscpy
 
     # sys/uio.h entrypoints
     libc.src.sys.uio.writev
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index bd9105f69222c..2f6ddc0c5672a 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -72,3 +72,10 @@ functions:
       - type: __restricted wchar_t *
       - type: const __ restricted wchar_t *
       - type: size_t
+  - name: wcscpy
+    standards:
+      - stdc
+    return_type: wchar_t *
+    arguments: 
+      - type: __restricted wchar_t *
+      - type: const __ restricted wchar_t *
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 9db121762348b..79c3d0b7418b9 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -104,3 +104,15 @@ add_entrypoint_object(
     libc.hdr.wchar_macros
     libc.src.__support.wctype_utils
 )
+
+add_entrypoint_object(
+  wcscpy
+  SRCS
+    wcscpy.cpp
+  HDRS
+    wcscpy.h
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.wchar_macros
+    libc.src.__support.wctype_utils
+)
diff --git a/libc/src/wchar/wcscpy.cpp b/libc/src/wchar/wcscpy.cpp
new file mode 100644
index 0000000000000..d99034fd6312f
--- /dev/null
+++ b/libc/src/wchar/wcscpy.cpp
@@ -0,0 +1,29 @@
+//===-- Implementation of wcscpy ------------------------------------------===//
+//
+// 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/wcscpy.h"
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+#include "src/string/string_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(wchar_t *, wcscpy,
+                   (wchar_t *__restrict s1, const wchar_t *__restrict s2)) {
+  size_t size = internal::string_length(s2) + 1;
+  inline_memcpy(s1, s2, size * sizeof(wchar_t));
+  return s1;
+}
+
+
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcscpy.h b/libc/src/wchar/wcscpy.h
new file mode 100644
index 0000000000000..c3f0e5f2211c3
--- /dev/null
+++ b/libc/src/wchar/wcscpy.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for wcscpy ----------------------------------===//
+//
+// 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_WCSCPY_H
+#define LLVM_LIBC_SRC_WCHAR_WCSCPY_H
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+wchar_t *wcscpy(wchar_t *__restrict s1, const wchar_t *__restrict s2);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSCPY_H
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 9bc230e0bddf3..5bc48e8aa49ec 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -94,3 +94,13 @@ add_libc_test(
   DEPENDS
     libc.src.wchar.wmemcpy
 )
+
+add_libc_test(
+  wcscpy_test
+  SUITE
+    libc_wchar_unittests
+  SRCS
+    wcscpy_test.cpp
+  DEPENDS
+    libc.src.wchar.wcscpy
+)
diff --git a/libc/test/src/wchar/wcscpy_test.cpp b/libc/test/src/wchar/wcscpy_test.cpp
new file mode 100644
index 0000000000000..7b71b2b30b6a7
--- /dev/null
+++ b/libc/test/src/wchar/wcscpy_test.cpp
@@ -0,0 +1,48 @@
+//===-- Unittests for wcscpy ---------------------------------------------===//
+//
+// 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/wchar_t.h"
+#include "src/wchar/wcscpy.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcWCSCpyTest, EmptySrc) {
+  // Empty src should lead to empty destination.
+  wchar_t dest[4] = {L'a', L'b', L'c', L'\0'};
+  const wchar_t *src = L"";
+  LIBC_NAMESPACE::wcscpy(dest, src);
+  ASSERT_TRUE(dest[0] == src[0]);
+  ASSERT_TRUE(dest[0] == L'\0');
+}
+
+TEST(LlvmLibcWCSCpyTest, EmptyDest) {
+  // Empty dest should result in src
+  const wchar_t *src = L"abc";
+  wchar_t dest[4];
+  LIBC_NAMESPACE::wcscpy(dest, src);
+  ASSERT_TRUE(dest[0] == L'a');
+  ASSERT_TRUE(dest[1] == L'b');
+  ASSERT_TRUE(dest[2] == L'c');
+  ASSERT_TRUE(dest[3] == L'\0');
+}
+
+TEST(LlvmLibcWCSCpyTest, OffsetDest) {
+  // Offsetting should result in a concatenation.
+  const wchar_t *src = L"abc";
+  wchar_t dest[7];
+  dest[0] = L'x';
+  dest[1] = L'y';
+  dest[2] = L'z';
+  LIBC_NAMESPACE::wcscpy(dest + 3, src);
+  ASSERT_TRUE(dest[0] == L'x');
+  ASSERT_TRUE(dest[1] == L'y');
+  ASSERT_TRUE(dest[2] == L'z');
+  ASSERT_TRUE(dest[3] == src[0]);
+  ASSERT_TRUE(dest[4] == src[1]);
+  ASSERT_TRUE(dest[5] == src[2]);
+  ASSERT_TRUE(dest[6] == src[3]);
+}

>From 9d9c2eaaf2c8f97c073b743b82b9b52256599f08 Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Fri, 30 May 2025 22:47:54 +0000
Subject: [PATCH 2/3] Fixed formatting error

---
 libc/src/wchar/wcscpy.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libc/src/wchar/wcscpy.cpp b/libc/src/wchar/wcscpy.cpp
index d99034fd6312f..dc46b972c59f7 100644
--- a/libc/src/wchar/wcscpy.cpp
+++ b/libc/src/wchar/wcscpy.cpp
@@ -24,6 +24,4 @@ LLVM_LIBC_FUNCTION(wchar_t *, wcscpy,
   return s1;
 }
 
-
-
 } // namespace LIBC_NAMESPACE_DECL

>From 6d8e77acb9f402e968f4cafdce50d5d335873191 Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Fri, 30 May 2025 23:23:08 +0000
Subject: [PATCH 3/3] Fixed yaml file

---
 libc/include/wchar.yaml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index 2f6ddc0c5672a..83d81547dd67d 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -77,5 +77,5 @@ functions:
       - stdc
     return_type: wchar_t *
     arguments: 
-      - type: __restricted wchar_t *
-      - type: const __ restricted wchar_t *
+      - type: __restrict wchar_t *
+      - type: const __restrict wchar_t *



More information about the libc-commits mailing list