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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions clang/test/AST/ByteCode/builtin-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}}
}