Skip to content

Commit

Permalink
[clang][Interp] Fix discarded integral and floating casts (#77295)
Browse files Browse the repository at this point in the history
We need to handle this at the CastExpr level.
  • Loading branch information
tbaederr committed Jan 10, 2024
1 parent 51fbab1 commit e80b943
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Expand Up @@ -114,13 +114,17 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
}

case CK_FloatingCast: {
if (DiscardResult)
return this->discard(SubExpr);
if (!this->visit(SubExpr))
return false;
const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
return this->emitCastFP(TargetSemantics, getRoundingMode(CE), CE);
}

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

case CK_FloatingToBoolean:
case CK_FloatingToIntegral: {
if (DiscardResult)
return this->discard(SubExpr);

std::optional<PrimType> ToT = classify(CE->getType());

if (!ToT)
Expand Down
4 changes: 4 additions & 0 deletions clang/test/AST/Interp/literals.cpp
Expand Up @@ -1024,6 +1024,10 @@ namespace DiscardExprs {
__null;
__builtin_offsetof(A, a);
1,2;
(int)1.0;
(float)1;
(double)1.0f;
(signed)4u;

return 0;
}
Expand Down

0 comments on commit e80b943

Please sign in to comment.