From e2495b5ed039e9fa6d7e1ac8392a1a738ce3db09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= Date: Wed, 1 Oct 2025 13:05:59 +0200 Subject: [PATCH] [clang][bytecode] Fix integral cast edge case --- clang/lib/AST/ByteCode/Compiler.cpp | 12 ++++++++++-- clang/test/AST/ByteCode/literals.cpp | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 0b7b6cd64dd97..c71fd22fe9d7e 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -540,7 +540,8 @@ bool Compiler::VisitCastExpr(const CastExpr *CE) { if (const auto *IL = dyn_cast(SubExpr)) { if (ToT != PT_IntAP && ToT != PT_IntAPS && FromT != PT_IntAP && FromT != PT_IntAPS && !CE->getType()->isEnumeralType()) - return this->emitConst(IL->getValue(), CE); + return this->emitConst(APSInt(IL->getValue(), !isSignedType(*FromT)), + CE); if (!this->emitConst(IL->getValue(), SubExpr)) return false; } else { @@ -4541,7 +4542,14 @@ bool Compiler::emitConst(T Value, const Expr *E) { template bool Compiler::emitConst(const APSInt &Value, PrimType Ty, const Expr *E) { - return this->emitConst(static_cast(Value), Ty, E); + if (Ty == PT_IntAPS) + return this->emitConstIntAPS(Value, E); + if (Ty == PT_IntAP) + return this->emitConstIntAP(Value, E); + + if (Value.isSigned()) + return this->emitConst(Value.getSExtValue(), Ty, E); + return this->emitConst(Value.getZExtValue(), Ty, E); } template diff --git a/clang/test/AST/ByteCode/literals.cpp b/clang/test/AST/ByteCode/literals.cpp index 5bc3f7f4c815c..5028ebfa3de30 100644 --- a/clang/test/AST/ByteCode/literals.cpp +++ b/clang/test/AST/ByteCode/literals.cpp @@ -28,6 +28,8 @@ static_assert(number != 10, ""); // both-error{{failed}} \ static_assert(__objc_yes, ""); static_assert(!__objc_no, ""); +static_assert((long long)0x00000000FFFF0000 == 4294901760, ""); + constexpr bool b = number; static_assert(b, ""); constexpr int one = true;