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

via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 24 07:47:02 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

1) When doing integral casts on a pointer-casted-to-integral, check the bitwidth we're casting _to_, not the one we're casting _from_.
2) When the pointer we're casting to an integral is a dummy pointer, don't forget to check the bitwidth.

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


3 Files Affected:

- (modified) clang/lib/AST/ByteCode/Interp.cpp (+5-3) 
- (modified) clang/lib/AST/ByteCode/Interp.h (+1-1) 
- (modified) clang/test/Sema/static-init.c (+1) 


``````````diff
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;
 

``````````

</details>


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


More information about the cfe-commits mailing list