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

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 05:04:33 PST 2025


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/127627

The fixme comment turned out to be true.

>From 7fec903e68bc8a2803b2bfd51d47cc4723e24234 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 18 Feb 2025 14:02:58 +0100
Subject: [PATCH] [clang][bytecode] Move bases and virtual bases in moveRecord

The fixme comment turned out to be true.
---
 clang/lib/AST/ByteCode/Descriptor.cpp | 26 ++++++++++++++++++++++++--
 clang/test/AST/ByteCode/records.cpp   | 19 +++++++++++++++++++
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Descriptor.cpp b/clang/lib/AST/ByteCode/Descriptor.cpp
index 319d1690c1cd0..026f3d96f9a46 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,29 @@ 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));
+
+    if (auto Fn = VBase.Desc->MoveFn)
+      Fn(B, Src + VBaseOffset, Dst + VBaseOffset, VBase.Desc);
+  }
 }
 
 static BlockCtorFn getCtorPrim(PrimType Type) {
diff --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index 9470e7d8e3dcb..a515f69d6903d 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1699,3 +1699,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