[clang] [clang][bytecode] Allow reinterpret casts from/to the same pointer type (PR #136692)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 22 05:26:38 PDT 2025


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/136692

None

>From dfd494236fd57172cfddfd0db6789321e1662ccb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 22 Apr 2025 14:17:42 +0200
Subject: [PATCH] [clang][bytecode] Allow reinterpret casts from/to the same
 pointer type

---
 clang/lib/AST/ByteCode/Compiler.cpp | 2 ++
 clang/test/AST/ByteCode/cxx11.cpp   | 8 ++++++++
 2 files changed, 10 insertions(+)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 3e53f2a2c8557..7cba0e8a4da19 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2990,6 +2990,8 @@ bool Compiler<Emitter>::VisitCXXReinterpretCastExpr(
     if (PointeeToT && PointeeFromT) {
       if (isIntegralType(*PointeeFromT) && isIntegralType(*PointeeToT))
         Fatal = false;
+    } else {
+      Fatal = SubExpr->getType().getTypePtr() != E->getType().getTypePtr();
     }
 
     if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
diff --git a/clang/test/AST/ByteCode/cxx11.cpp b/clang/test/AST/ByteCode/cxx11.cpp
index 23582e9ab556a..4c69517304ea7 100644
--- a/clang/test/AST/ByteCode/cxx11.cpp
+++ b/clang/test/AST/ByteCode/cxx11.cpp
@@ -185,3 +185,11 @@ namespace InitLinkToRVO {
   constexpr A make() { return A {}; }
   static_assert(make().z == 4, "");
 }
+
+namespace DynamicCast {
+  struct S { int x, y; } s;
+  constexpr S* sptr = &s;
+  struct Str {
+    int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr);
+  };
+}



More information about the cfe-commits mailing list