[clang] fix: constexpr bit_cast with empty base classes (PR #82383)

via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 22 09:14:05 PST 2024


https://github.com/sethp updated https://github.com/llvm/llvm-project/pull/82383

>From ed6a576ba0c9492f8aa4c995ea7316263a979fae Mon Sep 17 00:00:00 2001
From: sethp <seth.pellegrino at gmail.com>
Date: Tue, 20 Feb 2024 08:57:13 -0800
Subject: [PATCH 1/2] fix: constexpr bit_cast with empty base classes

Prior to this commit, clang would fail to produce a constant value for `b` in:

```c++
struct base {
};

struct s : base {
    int z;
};

constexpr auto b = std::bit_cast<s>(0x12);
```

e.g. https://godbolt.org/z/srrbTMPq4
---
 clang/lib/AST/ExprConstant.cpp                    | 3 ---
 clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp | 6 +++++-
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index fcf8f6591a7923..e1863cbf6c317c 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -7312,9 +7312,6 @@ class BufferToAPValueConverter {
       for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
         const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
         CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
-        if (BaseDecl->isEmpty() ||
-            Info.Ctx.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero())
-          continue;
 
         std::optional<APValue> SubObj = visitType(
             BS.getType(), Layout.getBaseClassOffset(BaseDecl) + Offset);
diff --git a/clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp b/clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
index c5b8032f40b131..7520b43a194aba 100644
--- a/clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
+++ b/clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
@@ -90,7 +90,8 @@ void test_record() {
   struct tuple4 {
     unsigned x, y, z, doublez;
 
-    constexpr bool operator==(tuple4 const &other) const {
+    bool operator==(tuple4 const &other) const = default;
+    constexpr bool operator==(bases const &other) const {
       return x == other.x && y == other.y &&
              z == other.z && doublez == other.doublez;
     }
@@ -99,6 +100,9 @@ void test_record() {
   constexpr tuple4 t4 = bit_cast<tuple4>(b);
   static_assert(t4 == tuple4{1, 2, 3, 4});
   static_assert(round_trip<tuple4>(b));
+
+  constexpr auto b2 = bit_cast<bases>(t4);
+  static_assert(t4 == b2);
 }
 
 void test_partially_initialized() {

>From 8d1351a4e82f2cdbef86b48e3e0ce765431da251 Mon Sep 17 00:00:00 2001
From: sethp <seth at codecopse.net>
Date: Thu, 22 Feb 2024 08:56:07 -0800
Subject: [PATCH 2/2] Update ReleaseNotes.rst

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 762e8133f5d536..f7f862260fc21c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -280,6 +280,8 @@ Bug Fixes to C++ Support
   was only accepted at namespace scope but not at local function scope.
 - Clang no longer tries to call consteval constructors at runtime when they appear in a member initializer.
   (`#782154 <https://github.com/llvm/llvm-project/issues/82154>`_`)
+- Clang's __builtin_bit_cast will now produce a constant value for records with empty bases. See:
+  (`#82383 <https://github.com/llvm/llvm-project/pull/82383>`_`)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^



More information about the cfe-commits mailing list