Skip to content

Commit c7f14f6

Browse files
authored
[clang][bytecode] Implement __builtin_wcschr (#132708)
This is already almost implemented, just need to enable support for it.
1 parent ad9630d commit c7f14f6

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,8 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC,
20522052
}
20532053

20542054
bool StopAtZero =
2055-
(ID == Builtin::BIstrchr || ID == Builtin::BI__builtin_strchr);
2055+
(ID == Builtin::BIstrchr || ID == Builtin::BI__builtin_strchr ||
2056+
ID == Builtin::BIwcschr || ID == Builtin::BI__builtin_wcschr);
20562057

20572058
PrimType ElemT =
20582059
IsRawByte ? PT_Sint8 : *S.getContext().classify(getElemType(Ptr));
@@ -2574,10 +2575,8 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
25742575
case Builtin::BI__builtin_strchr:
25752576
case Builtin::BIwmemchr:
25762577
case Builtin::BI__builtin_wmemchr:
2577-
#if 0
25782578
case Builtin::BIwcschr:
25792579
case Builtin::BI__builtin_wcschr:
2580-
#endif
25812580
case Builtin::BI__builtin_char_memchr:
25822581
if (!interp__builtin_memchr(S, OpPC, Frame, F, Call))
25832582
return false;

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern "C" {
2121
extern void *memchr(const void *s, int c, size_t n);
2222
extern char *strchr(const char *s, int c);
2323
extern wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n);
24+
extern wchar_t *wcschr(const wchar_t *s, wchar_t c);
2425
}
2526

2627
namespace strcmp {
@@ -1511,4 +1512,24 @@ namespace WMemChr {
15111512

15121513
constexpr wchar_t kStr2[] = {L'f', L'o', L'\xffff', L'o'};
15131514
static_assert(__builtin_wmemchr(kStr2, L'\xffff', 4) == kStr2 + 2);
1515+
1516+
1517+
static_assert(__builtin_wcschr(kStr, L'a') == kStr);
1518+
static_assert(__builtin_wcschr(kStr, L'b') == kStr + 1);
1519+
static_assert(__builtin_wcschr(kStr, L'c') == kStr + 2);
1520+
static_assert(__builtin_wcschr(kStr, L'd') == nullptr);
1521+
static_assert(__builtin_wcschr(kStr, L'e') == nullptr);
1522+
static_assert(__builtin_wcschr(kStr, L'\0') == kStr + 5);
1523+
static_assert(__builtin_wcschr(kStr, L'a' + 256) == nullptr);
1524+
static_assert(__builtin_wcschr(kStr, L'a' - 256) == nullptr);
1525+
static_assert(__builtin_wcschr(kStr, L'\xffff') == kStr + 4);
1526+
static_assert(__builtin_wcschr(kFoo, L'o') == kFoo + 1);
1527+
static_assert(__builtin_wcschr(kFoo, L'x') == nullptr); // both-error {{not an integral constant}} \
1528+
// both-note {{dereferenced one-past-the-end}}
1529+
static_assert(__builtin_wcschr(nullptr, L'x') == nullptr); // both-error {{not an integral constant}} \
1530+
// both-note {{dereferenced null}}
1531+
1532+
1533+
constexpr bool c = !wcschr(L"hello", L'h'); // both-error {{constant expression}} \
1534+
// both-note {{non-constexpr function 'wcschr' cannot be used in a constant expression}}
15141535
}

0 commit comments

Comments
 (0)