https://github.com/tbaederr created 
https://github.com/llvm/llvm-project/pull/132708

This is already almost implemented, just need to enable support for it.

>From 2355ce241b16a4b0d9839f010b90f338ba416a4b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com>
Date: Mon, 24 Mar 2025 11:38:42 +0100
Subject: [PATCH] [clang][bytecode] Implement __builtin_wcschr

This is already almost implemented, just need to enable support for it.
---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp      |  5 ++---
 clang/test/AST/ByteCode/builtin-functions.cpp | 21 +++++++++++++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 4a56e73239675..71fd25c183f48 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -2052,7 +2052,8 @@ static bool interp__builtin_memchr(InterpState &S, 
CodePtr OpPC,
   }
 
   bool StopAtZero =
-      (ID == Builtin::BIstrchr || ID == Builtin::BI__builtin_strchr);
+      (ID == Builtin::BIstrchr || ID == Builtin::BI__builtin_strchr ||
+       ID == Builtin::BIwcschr || ID == Builtin::BI__builtin_wcschr);
 
   PrimType ElemT =
       IsRawByte ? PT_Sint8 : *S.getContext().classify(getElemType(Ptr));
@@ -2574,10 +2575,8 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, 
const Function *F,
   case Builtin::BI__builtin_strchr:
   case Builtin::BIwmemchr:
   case Builtin::BI__builtin_wmemchr:
-#if 0
   case Builtin::BIwcschr:
   case Builtin::BI__builtin_wcschr:
-#endif
   case Builtin::BI__builtin_char_memchr:
     if (!interp__builtin_memchr(S, OpPC, Frame, F, Call))
       return false;
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp 
b/clang/test/AST/ByteCode/builtin-functions.cpp
index 13a34f71a6354..c3ea158eae859 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -21,6 +21,7 @@ extern "C" {
   extern void *memchr(const void *s, int c, size_t n);
   extern char *strchr(const char *s, int c);
   extern wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n);
+  extern wchar_t *wcschr(const wchar_t *s, wchar_t c);
 }
 
 namespace strcmp {
@@ -1511,4 +1512,24 @@ namespace WMemChr {
 
   constexpr wchar_t kStr2[] = {L'f', L'o', L'\xffff', L'o'};
   static_assert(__builtin_wmemchr(kStr2, L'\xffff', 4) == kStr2 + 2);
+
+
+  static_assert(__builtin_wcschr(kStr, L'a') == kStr);
+  static_assert(__builtin_wcschr(kStr, L'b') == kStr + 1);
+  static_assert(__builtin_wcschr(kStr, L'c') == kStr + 2);
+  static_assert(__builtin_wcschr(kStr, L'd') == nullptr);
+  static_assert(__builtin_wcschr(kStr, L'e') == nullptr);
+  static_assert(__builtin_wcschr(kStr, L'\0') == kStr + 5);
+  static_assert(__builtin_wcschr(kStr, L'a' + 256) == nullptr);
+  static_assert(__builtin_wcschr(kStr, L'a' - 256) == nullptr);
+  static_assert(__builtin_wcschr(kStr, L'\xffff') == kStr + 4);
+  static_assert(__builtin_wcschr(kFoo, L'o') == kFoo + 1);
+  static_assert(__builtin_wcschr(kFoo, L'x') == nullptr); // both-error {{not 
an integral constant}} \
+                                                          // both-note 
{{dereferenced one-past-the-end}}
+  static_assert(__builtin_wcschr(nullptr, L'x') == nullptr); // both-error 
{{not an integral constant}} \
+                                                             // both-note 
{{dereferenced null}}
+
+
+  constexpr bool c = !wcschr(L"hello", L'h'); // both-error {{constant 
expression}} \
+                                              // both-note {{non-constexpr 
function 'wcschr' cannot be used in a constant expression}}
 }

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to