Skip to content

[clang][bytecode] Implement __builtin_wcschr #132708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged

Conversation

tbaederr
Copy link
Contributor

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

This is already almost implemented, just need to enable support for it.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 24, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/132708.diff

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+2-3)
  • (modified) clang/test/AST/ByteCode/builtin-functions.cpp (+21)
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}}
 }

@tbaederr tbaederr merged commit c7f14f6 into llvm:main Mar 24, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants