Skip to content

Commit e80b943

Browse files
authored
[clang][Interp] Fix discarded integral and floating casts (#77295)
We need to handle this at the CastExpr level.
1 parent 51fbab1 commit e80b943

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,17 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
114114
}
115115

116116
case CK_FloatingCast: {
117+
if (DiscardResult)
118+
return this->discard(SubExpr);
117119
if (!this->visit(SubExpr))
118120
return false;
119121
const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
120122
return this->emitCastFP(TargetSemantics, getRoundingMode(CE), CE);
121123
}
122124

123125
case CK_IntegralToFloating: {
126+
if (DiscardResult)
127+
return this->discard(SubExpr);
124128
std::optional<PrimType> FromT = classify(SubExpr->getType());
125129
if (!FromT)
126130
return false;
@@ -135,6 +139,9 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
135139

136140
case CK_FloatingToBoolean:
137141
case CK_FloatingToIntegral: {
142+
if (DiscardResult)
143+
return this->discard(SubExpr);
144+
138145
std::optional<PrimType> ToT = classify(CE->getType());
139146

140147
if (!ToT)

clang/test/AST/Interp/literals.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,10 @@ namespace DiscardExprs {
10241024
__null;
10251025
__builtin_offsetof(A, a);
10261026
1,2;
1027+
(int)1.0;
1028+
(float)1;
1029+
(double)1.0f;
1030+
(signed)4u;
10271031

10281032
return 0;
10291033
}

0 commit comments

Comments
 (0)