Skip to content

Commit 93c8305

Browse files
authored
[clang][bytecode] Fix integral cast edge case (#161506)
We were converting the `ASInt` to as sign-less `APInt` too early and losing the sign information.
1 parent d5f98f3 commit 93c8305

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
540540
if (const auto *IL = dyn_cast<IntegerLiteral>(SubExpr)) {
541541
if (ToT != PT_IntAP && ToT != PT_IntAPS && FromT != PT_IntAP &&
542542
FromT != PT_IntAPS && !CE->getType()->isEnumeralType())
543-
return this->emitConst(IL->getValue(), CE);
543+
return this->emitConst(APSInt(IL->getValue(), !isSignedType(*FromT)),
544+
CE);
544545
if (!this->emitConst(IL->getValue(), SubExpr))
545546
return false;
546547
} else {
@@ -4541,7 +4542,14 @@ bool Compiler<Emitter>::emitConst(T Value, const Expr *E) {
45414542
template <class Emitter>
45424543
bool Compiler<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
45434544
const Expr *E) {
4544-
return this->emitConst(static_cast<const APInt &>(Value), Ty, E);
4545+
if (Ty == PT_IntAPS)
4546+
return this->emitConstIntAPS(Value, E);
4547+
if (Ty == PT_IntAP)
4548+
return this->emitConstIntAP(Value, E);
4549+
4550+
if (Value.isSigned())
4551+
return this->emitConst(Value.getSExtValue(), Ty, E);
4552+
return this->emitConst(Value.getZExtValue(), Ty, E);
45454553
}
45464554

45474555
template <class Emitter>

clang/test/AST/ByteCode/literals.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ static_assert(number != 10, ""); // both-error{{failed}} \
2828
static_assert(__objc_yes, "");
2929
static_assert(!__objc_no, "");
3030

31+
static_assert((long long)0x00000000FFFF0000 == 4294901760, "");
32+
3133
constexpr bool b = number;
3234
static_assert(b, "");
3335
constexpr int one = true;

0 commit comments

Comments
 (0)