[clang] [clang][bytecode] Check pointer data type for bitcast eligibility (PR #146552)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 1 08:45:44 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
So we get the proper type for a heap-allocated value.
---
Full diff: https://github.com/llvm/llvm-project/pull/146552.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp (+4-3)
- (modified) clang/test/AST/ByteCode/placement-new.cpp (+21)
``````````diff
diff --git a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
index 2569cac018b31..feac97d4b1a69 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
@@ -335,7 +335,8 @@ bool clang::interp::DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
BitcastBuffer Buffer(FullBitWidth);
size_t BuffSize = FullBitWidth.roundToBytes();
- if (!CheckBitcastType(S, OpPC, Ptr.getType(), /*IsToType=*/false))
+ QualType DataType = Ptr.getFieldDesc()->getDataType(S.getASTContext());
+ if (!CheckBitcastType(S, OpPC, DataType, /*IsToType=*/false))
return false;
bool Success = readPointerToBuffer(S.getContext(), Ptr, Buffer,
@@ -370,8 +371,8 @@ bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
assert(FromPtr.isBlockPointer());
assert(ToPtr.isBlockPointer());
- QualType FromType = FromPtr.getType();
- QualType ToType = ToPtr.getType();
+ QualType FromType = FromPtr.getFieldDesc()->getDataType(S.getASTContext());
+ QualType ToType = ToPtr.getFieldDesc()->getDataType(S.getASTContext());
if (!CheckBitcastType(S, OpPC, ToType, /*IsToType=*/true))
return false;
diff --git a/clang/test/AST/ByteCode/placement-new.cpp b/clang/test/AST/ByteCode/placement-new.cpp
index f23d71510602c..670def2d5870e 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -1,6 +1,7 @@
// RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -fexperimental-new-constant-interpreter -verify=expected,both %s -DBYTECODE
// RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -verify=ref,both %s
+typedef __INT64_TYPE__ int64_t;
namespace std {
using size_t = decltype(sizeof(0));
template<typename T> struct allocator {
@@ -465,3 +466,23 @@ namespace ArrayRoot {
static_assert(foo() == 0);
}
+
+namespace bitcast {
+ template <typename F, typename T>
+ constexpr T bit_cast(const F &f) {
+ return __builtin_bit_cast(T, f);
+ }
+ constexpr int foo() {
+ double *d = std::allocator<double>{}.allocate(2);
+ std::construct_at<double>(d, 0);
+
+ double &dd = *d;
+
+ int64_t i = bit_cast<double, int64_t>(*d);
+
+
+ std::allocator<double>{}.deallocate(d);
+ return i;
+ }
+ static_assert(foo() == 0);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/146552
More information about the cfe-commits
mailing list