[clang] 31abb20 - [clang][bytecode] Move bases and virtual bases in moveRecord (#127627)

via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 06:36:51 PST 2025


Author: Timm Baeder
Date: 2025-02-18T15:36:46+01:00
New Revision: 31abb20162a7d035841c9506b1cd18d3ae8df10c

URL: https://github.com/llvm/llvm-project/commit/31abb20162a7d035841c9506b1cd18d3ae8df10c
DIFF: https://github.com/llvm/llvm-project/commit/31abb20162a7d035841c9506b1cd18d3ae8df10c.diff

LOG: [clang][bytecode] Move bases and virtual bases in moveRecord (#127627)

The fixme comment turned out to be true.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Descriptor.cpp b/clang/lib/AST/ByteCode/Descriptor.cpp
index 319d1690c1cd0..6017f6dd61cb3 100644
--- a/clang/lib/AST/ByteCode/Descriptor.cpp
+++ b/clang/lib/AST/ByteCode/Descriptor.cpp
@@ -237,8 +237,7 @@ static void moveRecord(Block *B, std::byte *Src, std::byte *Dst,
   assert(D);
   assert(D->ElemRecord);
 
-  // FIXME: There might be cases where we need to move over the (v)bases as
-  // well.
+  // FIXME: Code duplication.
   for (const auto &F : D->ElemRecord->fields()) {
     auto FieldOffset = F.Offset;
     const auto *SrcDesc =
@@ -250,6 +249,26 @@ static void moveRecord(Block *B, std::byte *Src, std::byte *Dst,
     if (auto Fn = F.Desc->MoveFn)
       Fn(B, Src + FieldOffset, Dst + FieldOffset, F.Desc);
   }
+
+  for (const auto &Base : D->ElemRecord->bases()) {
+    auto BaseOffset = Base.Offset;
+    const auto *SrcDesc =
+        reinterpret_cast<const InlineDescriptor *>(Src + BaseOffset) - 1;
+    auto *DestDesc = reinterpret_cast<InlineDescriptor *>(Dst + BaseOffset) - 1;
+    std::memcpy(DestDesc, SrcDesc, sizeof(InlineDescriptor));
+
+    if (auto Fn = Base.Desc->MoveFn)
+      Fn(B, Src + BaseOffset, Dst + BaseOffset, Base.Desc);
+  }
+
+  for (const auto &VBase : D->ElemRecord->virtual_bases()) {
+    auto VBaseOffset = VBase.Offset;
+    const auto *SrcDesc =
+        reinterpret_cast<const InlineDescriptor *>(Src + VBaseOffset) - 1;
+    auto *DestDesc =
+        reinterpret_cast<InlineDescriptor *>(Dst + VBaseOffset) - 1;
+    std::memcpy(DestDesc, SrcDesc, sizeof(InlineDescriptor));
+  }
 }
 
 static BlockCtorFn getCtorPrim(PrimType Type) {

diff  --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index 3cc3210841e0f..a13f30cd23119 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1715,3 +1715,22 @@ namespace IgnoredMemberExpr {
   };
   static_assert(B{}.foo() == 0, "");
 }
+
+#if __cplusplus >= 202002L
+namespace DeadUpcast {
+  struct A {};
+  struct B : A{};
+  constexpr bool foo() {
+
+    B *pb;
+    {
+      B b;
+      pb = &b;
+    }
+    A *pa = pb;
+
+    return true;
+  }
+  static_assert(foo(), "");
+}
+#endif


        


More information about the cfe-commits mailing list