[clang] 6972788 - [clang][bytecode] Fix discarding DerivedToBase casts (#123523)

via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 19 23:55:57 PST 2025


Author: Timm Baeder
Date: 2025-01-20T08:55:54+01:00
New Revision: 6972788bf3d330b7a6136e2ddd840782882b8dd0

URL: https://github.com/llvm/llvm-project/commit/6972788bf3d330b7a6136e2ddd840782882b8dd0
DIFF: https://github.com/llvm/llvm-project/commit/6972788bf3d330b7a6136e2ddd840782882b8dd0.diff

LOG: [clang][bytecode] Fix discarding DerivedToBase casts (#123523)

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/test/AST/ByteCode/records.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 1a0e5ff45587f3..66ab27bdd13da5 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -253,6 +253,9 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
 
   case CK_UncheckedDerivedToBase:
   case CK_DerivedToBase: {
+    if (DiscardResult)
+      return this->discard(SubExpr);
+
     if (!this->delegate(SubExpr))
       return false;
 
@@ -282,6 +285,9 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
   }
 
   case CK_BaseToDerived: {
+    if (DiscardResult)
+      return this->discard(SubExpr);
+
     if (!this->delegate(SubExpr))
       return false;
 

diff  --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index d329219264d893..9470e7d8e3dcbf 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1684,3 +1684,18 @@ namespace ExplicitThisInTemporary {
   constexpr bool g(B b) { return &b == b.p; }
   static_assert(g({}), "");
 }
+
+namespace IgnoredMemberExpr {
+  class A {
+  public:
+    int a;
+  };
+  class B : public A {
+  public:
+    constexpr int foo() {
+      a; // both-warning {{expression result unused}}
+      return 0;
+    }
+  };
+  static_assert(B{}.foo() == 0, "");
+}


        


More information about the cfe-commits mailing list