[clang] [clang] Implement CWG1719 "Layout compatibility and cv-qualification revisited" (PR #82358)
Vlad Serebrennikov via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 21 05:18:06 PST 2024
https://github.com/Endilll updated https://github.com/llvm/llvm-project/pull/82358
>From 15d9428a47ea5d7ba3b4ffd2e766406b16d781e0 Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Tue, 20 Feb 2024 16:44:04 +0300
Subject: [PATCH 1/3] [clang] Implement CWG1719 "Layout compatibility and
cv-qualification revisited"
This patch updates our internal notion of layout-compatible, which in turn fixes `__is_layout_compatible` intrinsic.
---
clang/docs/ReleaseNotes.rst | 9 ++++++---
clang/lib/Sema/SemaChecking.cpp | 13 +++++++------
clang/test/CXX/drs/dr17xx.cpp | 11 +++++------
clang/test/SemaCXX/type-traits.cpp | 14 +++++++-------
clang/www/cxx_dr_status.html | 4 ++--
5 files changed, 27 insertions(+), 24 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8ea4a9a5a4256c..ffdc2a2629e6e9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -98,9 +98,8 @@ C++20 Feature Support
- Implemented the `__is_layout_compatible` intrinsic to support
`P0466R5: Layout-compatibility and Pointer-interconvertibility Traits <https://wg21.link/P0466R5>`_.
- Note: `CWG1719: Layout compatibility and cv-qualification revisited <https://cplusplus.github.io/CWG/issues/1719.html>`_
- and `CWG2759: [[no_unique_address] and common initial sequence <https://cplusplus.github.io/CWG/issues/2759.html>`_
- are not yet implemented.
+ Note: `CWG2759: [[no_unique_address] and common initial sequence <https://cplusplus.github.io/CWG/issues/2759.html>`_
+ is not yet implemented.
C++23 Feature Support
^^^^^^^^^^^^^^^^^^^^^
@@ -120,6 +119,10 @@ Resolutions to C++ Defect Reports
in the template parameters, but is deduced from a previous argument.
(`#78449: <https://github.com/llvm/llvm-project/issues/78449>`_).
+- ``const`` and ``volatile`` qualifiers are now ignored when evaluating
+ layout compatibility of two types.
+ (`CWG1719: Layout compatibility and cv-qualification revisited <https://cplusplus.github.io/CWG/issues/1719.html>`_).
+
C Language Changes
------------------
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index d951c0fc2732d2..e8bfb215a5b4c5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -19124,15 +19124,16 @@ static bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) {
if (T1.isNull() || T2.isNull())
return false;
- // C++11 [basic.types] p11:
- // If two types T1 and T2 are the same type, then T1 and T2 are
- // layout-compatible types.
- if (C.hasSameType(T1, T2))
- return true;
-
+ // C++20 [basic.types] p11:
+ // Two types cv1 T1 and cv2 T2 are layout-compatible types
+ // if T1 and T2 are the same type, layout-compatible enumerations (9.7.1),
+ // or layout-compatible standard-layout class types (11.4).
T1 = T1.getCanonicalType().getUnqualifiedType();
T2 = T2.getCanonicalType().getUnqualifiedType();
+ if (C.hasSameType(T1, T2))
+ return true;
+
const Type::TypeClass TC1 = T1->getTypeClass();
const Type::TypeClass TC2 = T2->getTypeClass();
diff --git a/clang/test/CXX/drs/dr17xx.cpp b/clang/test/CXX/drs/dr17xx.cpp
index e5cee19337ebd4..d933c244aada42 100644
--- a/clang/test/CXX/drs/dr17xx.cpp
+++ b/clang/test/CXX/drs/dr17xx.cpp
@@ -46,7 +46,7 @@ namespace dr1715 { // dr1715: 3.9
#endif
}
-namespace dr1719 { // dr1719: no
+namespace dr1719 { // dr1719: 19
#if __cplusplus >= 201103L
struct CStruct {
int one;
@@ -66,11 +66,10 @@ struct CStructWithQualifiers {
static_assert(__is_layout_compatible(CStruct, const CStruct2), "");
static_assert(__is_layout_compatible(CStruct, volatile CStruct2), "");
static_assert(__is_layout_compatible(const CStruct, volatile CStruct2), "");
-// FIXME: all of the following pairs of types are layout-compatible
-static_assert(!__is_layout_compatible(int, const int), "");
-static_assert(!__is_layout_compatible(int, volatile int), "");
-static_assert(!__is_layout_compatible(const int, volatile int), "");
-static_assert(!__is_layout_compatible(CStruct, CStructWithQualifiers), "");
+static_assert(__is_layout_compatible(int, const int), "");
+static_assert(__is_layout_compatible(int, volatile int), "");
+static_assert(__is_layout_compatible(const int, volatile int), "");
+static_assert(__is_layout_compatible(CStruct, CStructWithQualifiers), "");
#endif
} // namespace dr1719
diff --git a/clang/test/SemaCXX/type-traits.cpp b/clang/test/SemaCXX/type-traits.cpp
index 6ff04b6c8c7223..94889d654abeca 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -1711,13 +1711,13 @@ void is_layout_compatible(int n)
{
static_assert(__is_layout_compatible(void, void), "");
static_assert(!__is_layout_compatible(void, int), "");
- static_assert(!__is_layout_compatible(void, const void), ""); // FIXME: this is CWG1719
- static_assert(!__is_layout_compatible(void, volatile void), ""); // FIXME: this is CWG1719
- static_assert(!__is_layout_compatible(const int, volatile int), ""); // FIXME: this is CWG1719
+ static_assert(__is_layout_compatible(void, const void), "");
+ static_assert(__is_layout_compatible(void, volatile void), "");
+ static_assert(__is_layout_compatible(const int, volatile int), "");
static_assert(__is_layout_compatible(int, int), "");
- static_assert(!__is_layout_compatible(int, const int), ""); // FIXME: this is CWG1719
- static_assert(!__is_layout_compatible(int, volatile int), ""); // FIXME: this is CWG1719
- static_assert(!__is_layout_compatible(const int, volatile int), ""); // FIXME: this is CWG1719
+ static_assert(__is_layout_compatible(int, const int), "");
+ static_assert(__is_layout_compatible(int, volatile int), "");
+ static_assert(__is_layout_compatible(const int, volatile int), "");
static_assert(!__is_layout_compatible(int, unsigned int), "");
static_assert(!__is_layout_compatible(char, unsigned char), "");
static_assert(!__is_layout_compatible(char, signed char), "");
@@ -1758,7 +1758,7 @@ void is_layout_compatible(int n)
static_assert(!__is_layout_compatible(CppStructNonStandardByVirtBase, CppStructNonStandardByVirtBase2), "");
static_assert(!__is_layout_compatible(CppStructNonStandardBySameBase, CppStructNonStandardBySameBase2), "");
static_assert(!__is_layout_compatible(CppStructNonStandardBy2ndVirtBase, CppStructNonStandardBy2ndVirtBase2), "");
- static_assert(!__is_layout_compatible(CStruct, CStructWithQualifiers), ""); // FIXME: this is CWG1719
+ static_assert(__is_layout_compatible(CStruct, CStructWithQualifiers), "");
static_assert(__is_layout_compatible(CStruct, CStructNoUniqueAddress) == bool(__has_cpp_attribute(no_unique_address)), ""); // FIXME: this is CWG2759
static_assert(__is_layout_compatible(CStructNoUniqueAddress, CStructNoUniqueAddress2) == bool(__has_cpp_attribute(no_unique_address)), ""); // FIXME: this is CWG2759
static_assert(__is_layout_compatible(CStruct, CStructAlignment), "");
diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html
index e9b18b1e283e66..38e2cb63142662 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -7812,7 +7812,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td><a href="https://cplusplus.github.io/CWG/issues/1334.html">1334</a></td>
<td>NAD</td>
<td>Layout compatibility and cv-qualification</td>
- <td class="unknown" align="center">Unknown</td>
+ <td class="unreleased" align="center">Superseded by <a href="#1719">1719</a></td>
</tr>
<tr id="1335">
<td><a href="https://cplusplus.github.io/CWG/issues/1335.html">1335</a></td>
@@ -10122,7 +10122,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td><a href="https://cplusplus.github.io/CWG/issues/1719.html">1719</a></td>
<td>CD4</td>
<td>Layout compatibility and cv-qualification revisited</td>
- <td class="unknown" align="center">Unknown</td>
+ <td class="unreleased" align="center">Clang 19</td>
</tr>
<tr id="1720">
<td><a href="https://cplusplus.github.io/CWG/issues/1720.html">1720</a></td>
>From f84eb1aacd061c6838e87726d352bdb8eeb683cd Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Tue, 20 Feb 2024 23:23:14 +0300
Subject: [PATCH 2/3] Address Aaron's feedback
---
clang/docs/ReleaseNotes.rst | 4 ++--
clang/test/CXX/drs/dr17xx.cpp | 1 +
clang/test/SemaCXX/type-traits.cpp | 4 ++++
3 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ffdc2a2629e6e9..b3c412ab570148 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -119,8 +119,8 @@ Resolutions to C++ Defect Reports
in the template parameters, but is deduced from a previous argument.
(`#78449: <https://github.com/llvm/llvm-project/issues/78449>`_).
-- ``const`` and ``volatile`` qualifiers are now ignored when evaluating
- layout compatibility of two types.
+- Type qualifications are now ignored when evaluating layout compatibility
+ of two types.
(`CWG1719: Layout compatibility and cv-qualification revisited <https://cplusplus.github.io/CWG/issues/1719.html>`_).
C Language Changes
diff --git a/clang/test/CXX/drs/dr17xx.cpp b/clang/test/CXX/drs/dr17xx.cpp
index d933c244aada42..d3cb5e58f06b32 100644
--- a/clang/test/CXX/drs/dr17xx.cpp
+++ b/clang/test/CXX/drs/dr17xx.cpp
@@ -70,6 +70,7 @@ static_assert(__is_layout_compatible(int, const int), "");
static_assert(__is_layout_compatible(int, volatile int), "");
static_assert(__is_layout_compatible(const int, volatile int), "");
static_assert(__is_layout_compatible(CStruct, CStructWithQualifiers), "");
+static_assert(__is_layout_compatible(int[], const volatile int[]), "");
#endif
} // namespace dr1719
diff --git a/clang/test/SemaCXX/type-traits.cpp b/clang/test/SemaCXX/type-traits.cpp
index 94889d654abeca..d8d1425cd838a3 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -1718,6 +1718,10 @@ void is_layout_compatible(int n)
static_assert(__is_layout_compatible(int, const int), "");
static_assert(__is_layout_compatible(int, volatile int), "");
static_assert(__is_layout_compatible(const int, volatile int), "");
+ static_assert(__is_layout_compatible(int *, int * __restrict), "");
+ // Note: atomic qualification matters for layout compatibility.
+ static_assert(!__is_layout_compatible(int, _Atomic int), "");
+ static_assert(__is_layout_compatible(_Atomic(int), _Atomic int), "");
static_assert(!__is_layout_compatible(int, unsigned int), "");
static_assert(!__is_layout_compatible(char, unsigned char), "");
static_assert(!__is_layout_compatible(char, signed char), "");
>From 68e20c8a5004fcfdfe56bf3e637db656713b151f Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Tue, 20 Feb 2024 23:27:28 +0300
Subject: [PATCH 3/3] Add alignas test
---
clang/test/SemaCXX/type-traits.cpp | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/clang/test/SemaCXX/type-traits.cpp b/clang/test/SemaCXX/type-traits.cpp
index d8d1425cd838a3..2c35d5ee19a4c6 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -1609,7 +1609,12 @@ struct CStructNoUniqueAddress2 {
[[no_unique_address]] int two;
};
-struct CStructAlignment {
+struct alignas(64) CStructAlignment {
+ int one;
+ int two;
+};
+
+struct CStructAlignedMembers {
int one;
alignas(16) int two;
};
@@ -1766,6 +1771,7 @@ void is_layout_compatible(int n)
static_assert(__is_layout_compatible(CStruct, CStructNoUniqueAddress) == bool(__has_cpp_attribute(no_unique_address)), ""); // FIXME: this is CWG2759
static_assert(__is_layout_compatible(CStructNoUniqueAddress, CStructNoUniqueAddress2) == bool(__has_cpp_attribute(no_unique_address)), ""); // FIXME: this is CWG2759
static_assert(__is_layout_compatible(CStruct, CStructAlignment), "");
+ static_assert(__is_layout_compatible(CStruct, CStructAlignedMembers), ""); // FIXME: alignment of members impact common initial sequence
static_assert(__is_layout_compatible(CStructWithBitfelds, CStructWithBitfelds), "");
static_assert(__is_layout_compatible(CStructWithBitfelds, CStructWithBitfelds2), "");
static_assert(!__is_layout_compatible(CStructWithBitfelds, CStructWithBitfelds3), "");
More information about the cfe-commits
mailing list