Skip to content

Commit c316394

Browse files
committed
[clang][bytecode] Fix bitcasting from null pointers
This looks a little ugly now, I should wait for #116843.
1 parent d0d726e commit c316394

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
299299
llvm::sys::IsLittleEndianHost);
300300
bool BigEndianTarget = ASTCtx.getTargetInfo().isBigEndian();
301301

302+
uint64_t PointerSizeInBits =
303+
ASTCtx.getTargetInfo().getPointerWidth(LangAS::Default);
304+
302305
return enumeratePointerFields(
303306
FromPtr, Ctx,
304307
[&](const Pointer &P, PrimType T, size_t BitOffset,
@@ -310,8 +313,15 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
310313

311314
assert(P.isInitialized());
312315
// nullptr_t is a PT_Ptr for us, but it's still not std::is_pointer_v.
313-
if (T == PT_Ptr)
314-
assert(false && "Implement casting to pointer types");
316+
if (T == PT_Ptr) {
317+
assert(P.getType()->isNullPtrType());
318+
std::byte Zeroes[] = {std::byte{0}, std::byte{0}, std::byte{0},
319+
std::byte{0}, std::byte{0}, std::byte{0},
320+
std::byte{0}, std::byte{0}};
321+
assert(PointerSizeInBits <= (8 * 8));
322+
Buffer.pushData(Zeroes, PointerSizeInBits, BigEndianTarget);
323+
return true;
324+
}
315325

316326
CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(P.getType());
317327
unsigned BitWidth = ASTCtx.toBits(ObjectReprChars);

clang/test/AST/ByteCode/builtin-bit-cast.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ namespace simple {
135135
/// diagnoses it.
136136
static_assert(__builtin_bit_cast(intptr_t, nullptr) == 0); // ref-error {{not an integral constant expression}} \
137137
// ref-note {{indeterminate value can only initialize an object}}
138+
139+
constexpr int test_from_nullptr_pass = (__builtin_bit_cast(unsigned char[8], nullptr), 0);
138140
}
139141

140142
namespace Fail {

0 commit comments

Comments
 (0)