Skip to content
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

[clang][bytecode] Fix builtin_memchr with non-0 start index #131633

Merged
merged 1 commit into from
Mar 17, 2025

Conversation

tbaederr
Copy link
Contributor

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+5-3)
  • (modified) clang/test/AST/ByteCode/builtin-functions.cpp (+5)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 4660c80fc90db..3fa8fbc22ec03 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 29812553af9f0..11ff48bfa7102 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 {

@tbaederr tbaederr merged commit cfa07cc into llvm:main Mar 17, 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