[clang] [clang][bytecode] Fix some problems with ptr-to-int casts (PR #193988)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 25 10:48:02 PDT 2026


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/193988

>From b4efe320f35a559fb4db95eff848b2826951fe8b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 24 Apr 2026 16:28:52 +0200
Subject: [PATCH] [clang

---
 clang/lib/AST/ByteCode/Interp.cpp | 8 +++++---
 clang/lib/AST/ByteCode/Interp.h   | 2 +-
 clang/test/Sema/static-init.c     | 1 +
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 249a282482749..21a75b8fb4d45 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -2234,13 +2234,15 @@ bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC,
   if (Ptr.isIntegralPointer())
     return true;
 
-  if (Ptr.isDummy())
+  if (Ptr.isDummy()) {
+    if (!CheckIntegralAddressCast(S, OpPC, BitWidth))
+      return false;
     return Ptr.getIndex() == 0;
+  }
 
   if (!Ptr.isZero()) {
     // Only allow based lvalue casts if they are lossless.
-    if (S.getASTContext().getTargetInfo().getPointerWidth(LangAS::Default) !=
-        BitWidth)
+    if (!CheckIntegralAddressCast(S, OpPC, BitWidth))
       return Invalid(S, OpPC);
   }
   return true;
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 80662cc8a7186..1301edb2be2e4 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2697,7 +2697,7 @@ template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
   if constexpr (isIntegralOrPointer<T>()) {
     if (In.getKind() != IntegralKind::Number &&
         In.getKind() != IntegralKind::AddrLabelDiff) {
-      if (!CheckIntegralAddressCast(S, OpPC, In.bitWidth()))
+      if (!CheckIntegralAddressCast(S, OpPC, U::bitWidth()))
         return Invalid(S, OpPC);
     } else if (In.getKind() == IntegralKind::AddrLabelDiff) {
       // Allow casts of address-of-label differences if they are no-ops
diff --git a/clang/test/Sema/static-init.c b/clang/test/Sema/static-init.c
index 5333b522bde16..ecd7b5c78d0d5 100644
--- a/clang/test/Sema/static-init.c
+++ b/clang/test/Sema/static-init.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-pointer-to-int-cast -Wno-bool-conversion %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-pointer-to-int-cast -Wno-bool-conversion %s -fexperimental-new-constant-interpreter
 
 typedef __typeof((int*) 0 - (int*) 0) intptr_t;
 



More information about the cfe-commits mailing list