[clang] e80b943 - [clang][Interp] Fix discarded integral and floating casts (#77295)
    via cfe-commits 
    cfe-commits at lists.llvm.org
       
    Wed Jan 10 11:19:09 PST 2024
    
    
  
Author: Timm Baeder
Date: 2024-01-10T20:19:04+01:00
New Revision: e80b9436476bba714e843461e03227b222185f7b
URL: https://github.com/llvm/llvm-project/commit/e80b9436476bba714e843461e03227b222185f7b
DIFF: https://github.com/llvm/llvm-project/commit/e80b9436476bba714e843461e03227b222185f7b.diff
LOG: [clang][Interp] Fix discarded integral and floating casts (#77295)
We need to handle this at the CastExpr level.
Added: 
    
Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/literals.cpp
Removed: 
    
################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index e6b3097a80d8f7..7f8bbe78732481 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -114,6 +114,8 @@ 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());
@@ -121,6 +123,8 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
   }
 
   case CK_IntegralToFloating: {
+    if (DiscardResult)
+      return this->discard(SubExpr);
     std::optional<PrimType> FromT = classify(SubExpr->getType());
     if (!FromT)
       return false;
@@ -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)
diff  --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index 85adfe551384d2..61825bc11438f6 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -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;
   }
        
    
    
More information about the cfe-commits
mailing list