diff options
author | Timm Baeder <tbaeder@redhat.com> | 2025-03-17 19:02:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-17 19:02:55 +0100 |
commit | cfa07ccdfcf03cbd48086fe9988f97e3a776b02c (patch) | |
tree | 3fee4ef7540836b3ea751f079b960070c8030044 | |
parent | ca1bde0b91a6129e7bacee0fa67e4331b06dd683 (diff) | |
download | llvm-cfa07ccdfcf03cbd48086fe9988f97e3a776b02c.zip llvm-cfa07ccdfcf03cbd48086fe9988f97e3a776b02c.tar.gz llvm-cfa07ccdfcf03cbd48086fe9988f97e3a776b02c.tar.bz2 |
[clang][bytecode] Fix builtin_memchr with non-0 start index (#131633)
-rw-r--r-- | clang/lib/AST/ByteCode/InterpBuiltin.cpp | 8 | ||||
-rw-r--r-- | clang/test/AST/ByteCode/builtin-functions.cpp | 5 |
2 files changed, 10 insertions, 3 deletions
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index 4660c80f..3fa8fbc 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -2045,8 +2045,10 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC, (ID == Builtin::BIstrchr || ID == Builtin::BI__builtin_strchr); size_t Index = Ptr.getIndex(); + size_t Step = 0; for (;;) { - const Pointer &ElemPtr = Index > 0 ? Ptr.atIndex(Index) : Ptr; + const Pointer &ElemPtr = + (Index + Step) > 0 ? Ptr.atIndex(Index + Step) : Ptr; if (!CheckLoad(S, OpPC, ElemPtr)) return false; @@ -2060,8 +2062,8 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC, if (StopAtZero && V == 0) break; - ++Index; - if (MaxLength && Index == MaxLength->getZExtValue()) + ++Step; + if (MaxLength && Step == MaxLength->getZExtValue()) break; } diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp index 2981255..11ff48b 100644 --- a/clang/test/AST/ByteCode/builtin-functions.cpp +++ b/clang/test/AST/ByteCode/builtin-functions.cpp @@ -1459,6 +1459,11 @@ namespace Memchr { constexpr bool b = !memchr("hello", 'h', 3); // both-error {{constant expression}} \ // both-note {{non-constexpr function 'memchr' cannot be used in a constant expression}} + constexpr bool f() { + const char *c = "abcdef"; + return __builtin_char_memchr(c + 1, 'f', 1) == nullptr; + } + static_assert(f()); } namespace Strchr { |