[clang] c7f14f6 - [clang][bytecode] Implement __builtin_wcschr (#132708)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 24 04:28:27 PDT 2025
Author: Timm Baeder
Date: 2025-03-24T12:28:23+01:00
New Revision: c7f14f601f36f8fdabb9182e253add18a471308f
URL: https://github.com/llvm/llvm-project/commit/c7f14f601f36f8fdabb9182e253add18a471308f
DIFF: https://github.com/llvm/llvm-project/commit/c7f14f601f36f8fdabb9182e253add18a471308f.diff
LOG: [clang][bytecode] Implement __builtin_wcschr (#132708)
This is already almost implemented, just need to enable support for it.
Added:
Modified:
clang/lib/AST/ByteCode/InterpBuiltin.cpp
clang/test/AST/ByteCode/builtin-functions.cpp
Removed:
################################################################################
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}}
}
More information about the cfe-commits
mailing list