[clang] [clang][bytecode] Add `StringPointer` (PR #216736)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 24 08:01:50 PDT 2026


Timm =?utf-8?q?Bäder?= <tbaeder at redhat.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/216736 at github.com>


================
@@ -324,14 +348,33 @@ std::optional<uint64_t> Context::evaluateStrlen(State &Parent, const Expr *E) {
   std::optional<uint64_t> Result;
   auto PtrRes = C.interpretAsPointer(E, [&](InterpState &S, CodePtr OpPC,
                                             const Pointer &Ptr) {
-    if (!Ptr.isBlockPointer())
+    if (!Ptr.isReadablePointerType())
       return false;
 
+    if (Ptr.isPastEnd())
+      return false;
+
+    if (Ptr.isStringPointer()) {
+      const auto *Lit = Ptr.asStringPointer().getLiteral();
+      int64_t Off = Ptr.getByteOffset();
+      if (Off < 0)
+        return false;
+
+      unsigned Length = 0;
+      for (uint64_t I = Off; I != Lit->getLength(); ++I) {
+        if (Lit->getCodeUnit(I) == 0)
+          break;
+        ++Length;
+      }
+      Result = Length;
+      return true;
+    }
----------------
cor3ntin wrote:

We do that in a bunch of places, at some point it might be nice to do it in StringLiteral directly

https://github.com/llvm/llvm-project/pull/216736


More information about the cfe-commits mailing list