[libc-commits] [libc] [libc] add wmemchr, wcslen, wcschr, wcsrchr (PR #121183)

Tristan Ross via libc-commits libc-commits at lists.llvm.org
Thu Dec 26 23:03:33 PST 2024


https://github.com/RossComputerGuy created https://github.com/llvm/llvm-project/pull/121183

Adds a few wchar functions which are missing. More functions to come in follow up PR's or may be tacked onto this one. Goal is to start getting LLVM libc to work with libcxx.

>From 58af7c8de18c65a0cd269db3e2d188bb7fed4221 Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 26 Dec 2024 22:30:08 -0800
Subject: [PATCH 1/4] [libc] add wmemchr

---
 libc/config/gpu/entrypoints.txt           |  1 +
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/hdrgen/yaml/wchar.yaml               |  8 +++++++
 libc/src/wchar/CMakeLists.txt             | 12 +++++++++++
 libc/src/wchar/wmemchr.cpp                | 26 +++++++++++++++++++++++
 libc/src/wchar/wmemchr.h                  | 22 +++++++++++++++++++
 8 files changed, 72 insertions(+)
 create mode 100644 libc/src/wchar/wmemchr.cpp
 create mode 100644 libc/src/wchar/wmemchr.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index b008e0e6684fdd..a06db2e822bb45 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.nanosleep
 
     # wchar.h entrypoints
+    libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 
     # locale.h entrypoints
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 00f0c6a8bfb8e4..f4ceae9401cc6a 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 )
 
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 49a8d61b938027..d0226a2fba673a 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 )
 
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7e549607716c02..404003d74fe0be 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wmemchr
     libc.src.wchar.wctob
     libc.src.wchar.btowc
 )
diff --git a/libc/hdrgen/yaml/wchar.yaml b/libc/hdrgen/yaml/wchar.yaml
index bc824b21d8be17..5325e1ce4ede55 100644
--- a/libc/hdrgen/yaml/wchar.yaml
+++ b/libc/hdrgen/yaml/wchar.yaml
@@ -14,3 +14,11 @@ functions:
     return_type: int
     arguments:
       - type: wint_t
+  - name: wmemchr
+    standards:
+      - stdc
+    return_type: const wchar_t *
+    arguments:
+      - type: const wchar_t *
+      - type: wchar_t
+      - type: size_t
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index d4c98ea527a8f9..7ae99f03ceecb0 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -22,3 +22,15 @@ add_entrypoint_object(
     libc.hdr.wchar_macros
     libc.src.__support.wctype_utils
 )
+
+add_entrypoint_object(
+  wmemchr
+  SRCS
+    wmemchr.cpp
+  HDRS
+    wmemchr.h
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.types.wchar_t
+    libc.src.__support.wctype_utils
+)
diff --git a/libc/src/wchar/wmemchr.cpp b/libc/src/wchar/wmemchr.cpp
new file mode 100644
index 00000000000000..62191454343a8a
--- /dev/null
+++ b/libc/src/wchar/wmemchr.cpp
@@ -0,0 +1,26 @@
+//===-- Implementation of wmemchr -----------------------------------------===//
+//
+// 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/wmemchr.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(const wchar_t *, wmemchr,
+                   (const wchar_t *s, wchar_t c, size_t n)) {
+  for (size_t i = 0; i < n; i++) {
+    if (s[i] == c) {
+      return &s[i];
+    }
+  }
+
+  return nullptr;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wmemchr.h b/libc/src/wchar/wmemchr.h
new file mode 100644
index 00000000000000..e47a0a75735939
--- /dev/null
+++ b/libc/src/wchar/wmemchr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for wmemchr -----------------------*- C++ -*-===//
+//
+// 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_WMEMCHR_H
+#define LLVM_LIBC_SRC_WCHAR_WMEMCHR_H
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+const wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WMEMCHR_H

>From ed5869d0744ff3a0ae201a77bb96b41be9a0c9f9 Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 26 Dec 2024 22:42:48 -0800
Subject: [PATCH 2/4] [libc] add wcslen

---
 libc/config/gpu/entrypoints.txt           |  1 +
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/hdrgen/yaml/wchar.yaml               |  6 ++++++
 libc/src/wchar/wcslen.cpp                 | 22 ++++++++++++++++++++++
 libc/src/wchar/wcslen.h                   | 22 ++++++++++++++++++++++
 7 files changed, 54 insertions(+)
 create mode 100644 libc/src/wchar/wcslen.cpp
 create mode 100644 libc/src/wchar/wcslen.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index a06db2e822bb45..4230c9adcea06e 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.nanosleep
 
     # wchar.h entrypoints
+    libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f4ceae9401cc6a..099dc93ad4a213 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 )
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index d0226a2fba673a..a5554cdba8a1a4 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 )
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 404003d74fe0be..9303c744623fb3 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
     libc.src.wchar.btowc
diff --git a/libc/hdrgen/yaml/wchar.yaml b/libc/hdrgen/yaml/wchar.yaml
index 5325e1ce4ede55..ec773d84ff39ea 100644
--- a/libc/hdrgen/yaml/wchar.yaml
+++ b/libc/hdrgen/yaml/wchar.yaml
@@ -22,3 +22,9 @@ functions:
       - type: const wchar_t *
       - type: wchar_t
       - type: size_t
+  - name: wcslen
+    standards:
+      - stdc
+    return_type: size_t
+    arguments:
+      - type: const wchar_t *
diff --git a/libc/src/wchar/wcslen.cpp b/libc/src/wchar/wcslen.cpp
new file mode 100644
index 00000000000000..2e711af8b12bf7
--- /dev/null
+++ b/libc/src/wchar/wcslen.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of wcslen ------------------------------------------===//
+//
+// 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/wcslen.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(size_t, wcslen, (const wchar_t *s)) {
+  size_t length = 0;
+  while (s[length++])
+    ;
+  return length;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcslen.h b/libc/src/wchar/wcslen.h
new file mode 100644
index 00000000000000..f472a1d5e96315
--- /dev/null
+++ b/libc/src/wchar/wcslen.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for wcslen ------------------------*- C++ -*-===//
+//
+// 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_WCSLEN_H
+#define LLVM_LIBC_SRC_WCHAR_WCSLEN_H
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+size_t wcslen(const wchar_t *s);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSLEN_H

>From 36ec86166943589010a67444edf62d855fc2094e Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 26 Dec 2024 22:49:52 -0800
Subject: [PATCH 3/4] [libc] add wcschr

---
 libc/config/gpu/entrypoints.txt           |  1 +
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/hdrgen/yaml/wchar.yaml               |  7 +++++++
 libc/src/wchar/CMakeLists.txt             | 24 +++++++++++++++++++++++
 libc/src/wchar/wcschr.cpp                 | 21 ++++++++++++++++++++
 libc/src/wchar/wcschr.h                   | 21 ++++++++++++++++++++
 8 files changed, 77 insertions(+)
 create mode 100644 libc/src/wchar/wcschr.cpp
 create mode 100644 libc/src/wchar/wcschr.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 4230c9adcea06e..d861a7cdbc2273 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.nanosleep
 
     # wchar.h entrypoints
+    libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 099dc93ad4a213..ca46816991d52a 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index a5554cdba8a1a4..29a0456538bc64 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9303c744623fb3..c2defa7ab7c3aa 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
diff --git a/libc/hdrgen/yaml/wchar.yaml b/libc/hdrgen/yaml/wchar.yaml
index ec773d84ff39ea..73e77f6438bc7a 100644
--- a/libc/hdrgen/yaml/wchar.yaml
+++ b/libc/hdrgen/yaml/wchar.yaml
@@ -28,3 +28,10 @@ functions:
     return_type: size_t
     arguments:
       - type: const wchar_t *
+  - name: wcschr
+    standards:
+      - stdc
+    return_type: const wchar_t *
+    arguments:
+      - type: const wchar_t *
+      - type: wchar_t
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 7ae99f03ceecb0..11a8ac0461464f 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -34,3 +34,27 @@ add_entrypoint_object(
     libc.hdr.types.wchar_t
     libc.src.__support.wctype_utils
 )
+
+add_entrypoint_object(
+  wcslen
+  SRCS
+    wcslen.cpp
+  HDRS
+    wcslen.h
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.types.wchar_t
+    libc.src.__support.wctype_utils
+)
+
+add_entrypoint_object(
+  wcschr
+  SRCS
+    wcschr.cpp
+  HDRS
+    wcschr.h
+  DEPENDS
+    .wcslen
+    .wmemchr
+    libc.hdr.types.wchar_t
+)
diff --git a/libc/src/wchar/wcschr.cpp b/libc/src/wchar/wcschr.cpp
new file mode 100644
index 00000000000000..ebb24a64f639e1
--- /dev/null
+++ b/libc/src/wchar/wcschr.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of wcschr ------------------------------------------===//
+//
+// 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/wcschr.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "wcslen.h"
+#include "wmemchr.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(const wchar_t *, wcschr, (const wchar_t *s, wchar_t c)) {
+  return wmemchr(s, c, wcslen(s));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcschr.h b/libc/src/wchar/wcschr.h
new file mode 100644
index 00000000000000..6466d8e2ec2d62
--- /dev/null
+++ b/libc/src/wchar/wcschr.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for wmemchr -----------------------*- C++ -*-===//
+//
+// 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_WCSCHR_H
+#define LLVM_LIBC_SRC_WCHAR_WCSCHR_H
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+const wchar_t *wcschr(const wchar_t *s, wchar_t c);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSCHR_H

>From a32e5026d8e51bf45a74f94c5141e69015ff8dae Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 26 Dec 2024 23:01:12 -0800
Subject: [PATCH 4/4] [libc] add wcsrchr

---
 libc/config/gpu/entrypoints.txt           |  1 +
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/hdrgen/yaml/wchar.yaml               |  7 ++++++
 libc/src/wchar/CMakeLists.txt             | 11 ++++++++++
 libc/src/wchar/wcsrchr.cpp                | 26 +++++++++++++++++++++++
 libc/src/wchar/wcsrchr.h                  | 21 ++++++++++++++++++
 8 files changed, 69 insertions(+)
 create mode 100644 libc/src/wchar/wcsrchr.cpp
 create mode 100644 libc/src/wchar/wcsrchr.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index d861a7cdbc2273..2720ecc2f9f772 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.nanosleep
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ca46816991d52a..bfc8224660dad0 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 29a0456538bc64..50f1789bc60677 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index c2defa7ab7c3aa..82e6fe688e619f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
diff --git a/libc/hdrgen/yaml/wchar.yaml b/libc/hdrgen/yaml/wchar.yaml
index 73e77f6438bc7a..a1ef7c41e1492f 100644
--- a/libc/hdrgen/yaml/wchar.yaml
+++ b/libc/hdrgen/yaml/wchar.yaml
@@ -35,3 +35,10 @@ functions:
     arguments:
       - type: const wchar_t *
       - type: wchar_t
+  - name: wcsrchr
+    standards:
+      - stdc
+    return_type: const wchar_t *
+    arguments:
+      - type: const wchar_t *
+      - type: wchar_t
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 11a8ac0461464f..f5a9158d28e330 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -58,3 +58,14 @@ add_entrypoint_object(
     .wmemchr
     libc.hdr.types.wchar_t
 )
+
+add_entrypoint_object(
+  wcsrchr
+  SRCS
+    wcsrchr.cpp
+  HDRS
+    wcsrchr.h
+  DEPENDS
+    .wcslen
+    libc.hdr.types.wchar_t
+)
diff --git a/libc/src/wchar/wcsrchr.cpp b/libc/src/wchar/wcsrchr.cpp
new file mode 100644
index 00000000000000..10894bcb987032
--- /dev/null
+++ b/libc/src/wchar/wcsrchr.cpp
@@ -0,0 +1,26 @@
+//===-- Implementation of wcsrchr
+//------------------------------------------===//
+//
+// 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/wcsrchr.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "wcslen.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(const wchar_t *, wcsrchr, (const wchar_t *s, wchar_t c)) {
+  size_t length = wcslen(s);
+  for (size_t i = 0; i < ; i++) {
+    if (s[length - i] == c)
+      return &s[length - i];
+  }
+  return nullptr;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcsrchr.h b/libc/src/wchar/wcsrchr.h
new file mode 100644
index 00000000000000..8b4a3ef11c6b49
--- /dev/null
+++ b/libc/src/wchar/wcsrchr.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for wcsrchr -----------------------*- C++ -*-===//
+//
+// 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_WCSRCHR_H
+#define LLVM_LIBC_SRC_WCHAR_WCSRCHR_H
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+const wchar_t *wcsrchr(const wchar_t *s, wchar_t c);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSRCHR_H



More information about the libc-commits mailing list