[clang] [clang] Allow constexpr cast from `void*` in more cases (PR #89484)
Vlad Serebrennikov via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 29 05:30:19 PDT 2024
https://github.com/Endilll updated https://github.com/llvm/llvm-project/pull/89484
>From 5985dbe47e052505278d60628bbb5ca751cc3b6c Mon Sep 17 00:00:00 2001
From: offsetof <131769984+offsetof at users.noreply.github.com>
Date: Sat, 20 Apr 2024 02:35:09 +0000
Subject: [PATCH 1/2] [clang] Allow constexpr cast from "void*" in more cases
When converting from "cv void*" to "T*" in a constant expression, check
that the actual pointee type is similar to "T" (as opposed to requiring
that the types are the same except top-level qualifiers).
Also implement the resolution of the tentatively ready CWG issue 2819,
adding support for the case when the operand is a null pointer.
---
clang/lib/AST/ExprConstant.cpp | 7 ++++---
clang/test/CXX/drs/dr28xx.cpp | 8 ++++++++
clang/test/CXX/expr/expr.const/p5-26.cpp | 7 +++++++
clang/www/cxx_dr_status.html | 2 +-
4 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 73ae8d8efb23a2..d7aee8c92b83d5 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9237,9 +9237,10 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&
!Result.IsNullPtr;
bool VoidPtrCastMaybeOK =
- HasValidResult &&
- Info.Ctx.hasSameUnqualifiedType(Result.Designator.getType(Info.Ctx),
- E->getType()->getPointeeType());
+ Result.IsNullPtr ||
+ (HasValidResult &&
+ Info.Ctx.hasSimilarType(Result.Designator.getType(Info.Ctx),
+ E->getType()->getPointeeType()));
// 1. We'll allow it in std::allocator::allocate, and anything which that
// calls.
// 2. HACK 2022-03-28: Work around an issue with libstdc++'s
diff --git a/clang/test/CXX/drs/dr28xx.cpp b/clang/test/CXX/drs/dr28xx.cpp
index 4d9b0c76758d53..591bf7abfd872d 100644
--- a/clang/test/CXX/drs/dr28xx.cpp
+++ b/clang/test/CXX/drs/dr28xx.cpp
@@ -10,6 +10,14 @@
// expected-no-diagnostics
#endif
+namespace cwg2819 { // cwg2819: 19 review 2023-12-01
+#if __cpp_constexpr >= 202306L
+ constexpr void* p = nullptr;
+ constexpr int* q = static_cast<int*>(p);
+ static_assert(q == nullptr);
+#endif
+}
+
namespace cwg2847 { // cwg2847: 19
#if __cplusplus >= 202002L
diff --git a/clang/test/CXX/expr/expr.const/p5-26.cpp b/clang/test/CXX/expr/expr.const/p5-26.cpp
index 3624b1e5a3e3df..7513b11c09aa01 100644
--- a/clang/test/CXX/expr/expr.const/p5-26.cpp
+++ b/clang/test/CXX/expr/expr.const/p5-26.cpp
@@ -37,3 +37,10 @@ void err() {
// cxx23-note {{cast from 'void *' is not allowed in a constant expression in C++ standards before C++2c}} \
// cxx26-note {{cast from 'void *' is not allowed in a constant expression because the pointed object type 'T' is not similar to the target type 'S'}}
}
+
+int* p;
+constexpr int** pp = &p;
+constexpr void* vp = pp;
+constexpr auto cvp = static_cast<const int* volatile*>(vp);
+// cxx23-error at -1 {{constant expression}}
+// cxx23-note at -2 {{cast from 'void *' is not allowed in a constant expression}}
diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html
index 83b71e7c122d1e..2677364065b2f2 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -16722,7 +16722,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td><a href="https://cplusplus.github.io/CWG/issues/2819.html">2819</a></td>
<td>review</td>
<td>Cast from null pointer value in a constant expression</td>
- <td align="center">Not resolved</td>
+ <td title="Clang 19 implements 2023-12-01 resolution" align="center">Not Resolved*</td>
</tr>
<tr class="open" id="2820">
<td><a href="https://cplusplus.github.io/CWG/issues/2820.html">2820</a></td>
>From d858d685647780659d249d64ad99e09e00c7ade0 Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Mon, 29 Apr 2024 15:26:55 +0300
Subject: [PATCH 2/2] Fix some 'tentatively ready'-related mess introduced in
#90352
---
clang/test/CXX/drs/dr25xx.cpp | 2 +-
clang/test/CXX/drs/dr28xx.cpp | 4 +--
clang/www/cxx_dr_status.html | 52 +++++++++++++++++------------------
clang/www/make_cxx_dr_status | 2 +-
4 files changed, 30 insertions(+), 30 deletions(-)
diff --git a/clang/test/CXX/drs/dr25xx.cpp b/clang/test/CXX/drs/dr25xx.cpp
index 481ae09cdb77ea..8bca58f44944f7 100644
--- a/clang/test/CXX/drs/dr25xx.cpp
+++ b/clang/test/CXX/drs/dr25xx.cpp
@@ -130,7 +130,7 @@ struct D3 : B {
#endif
#if __cplusplus >= 202302L
-namespace cwg2561 { // cwg2561: no
+namespace cwg2561 { // cwg2561: no tentatively ready 2024-03-18
struct C {
constexpr C(auto) { }
};
diff --git a/clang/test/CXX/drs/dr28xx.cpp b/clang/test/CXX/drs/dr28xx.cpp
index a2248ccdce8427..be35d366bdd614 100644
--- a/clang/test/CXX/drs/dr28xx.cpp
+++ b/clang/test/CXX/drs/dr28xx.cpp
@@ -10,7 +10,7 @@
// expected-no-diagnostics
#endif
-namespace cwg2819 { // cwg2819: 19 review 2023-12-01
+namespace cwg2819 { // cwg2819: 19 tentatively ready 2023-12-01
#if __cpp_constexpr >= 202306L
constexpr void* p = nullptr;
constexpr int* q = static_cast<int*>(p);
@@ -67,7 +67,7 @@ void B<int>::g() requires true;
} // namespace cwg2847
-namespace cwg2858 { // cwg2858: 19
+namespace cwg2858 { // cwg2858: 19 tentatively ready 2024-04-05
#if __cplusplus > 202302L
diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html
index eb043eb3ce099f..875521bd505d5b 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -1433,11 +1433,11 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>Is indirection through a null pointer undefined behavior?</td>
<td class="unknown" align="center">Unknown</td>
</tr>
- <tr id="233">
+ <tr class="open" id="233">
<td><a href="https://cplusplus.github.io/CWG/issues/233.html">233</a></td>
<td>tentatively ready</td>
<td>References vs pointers in UDC overload resolution</td>
- <td class="unknown" align="center">Unknown</td>
+ <td align="center">Not resolved</td>
</tr>
<tr id="234">
<td><a href="https://cplusplus.github.io/CWG/issues/234.html">234</a></td>
@@ -15170,11 +15170,11 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>Parameter type determination in a <I>requirement-parameter-list</I></td>
<td class="unknown" align="center">Unknown</td>
</tr>
- <tr id="2561">
+ <tr class="open" id="2561">
<td><a href="https://cplusplus.github.io/CWG/issues/2561.html">2561</a></td>
<td>tentatively ready</td>
<td>Conversion to function pointer for lambda with explicit object parameter</td>
- <td class="none" align="center">No</td>
+ <td title="Clang does not implement 2024-03-18 resolution" align="center">Not Resolved*</td>
</tr>
<tr class="open" id="2562">
<td><a href="https://cplusplus.github.io/CWG/issues/2562.html">2562</a></td>
@@ -15332,11 +15332,11 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>Visible side effects and initial value of an object</td>
<td align="center">Not resolved</td>
</tr>
- <tr id="2588">
+ <tr class="open" id="2588">
<td><a href="https://cplusplus.github.io/CWG/issues/2588.html">2588</a></td>
<td>tentatively ready</td>
<td>friend declarations and module linkage</td>
- <td class="unknown" align="center">Unknown</td>
+ <td align="center">Not resolved</td>
</tr>
<tr class="open" id="2589">
<td><a href="https://cplusplus.github.io/CWG/issues/2589.html">2589</a></td>
@@ -16172,11 +16172,11 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>Importing header units synthesized from source files</td>
<td align="center">Not resolved</td>
</tr>
- <tr id="2728">
+ <tr class="open" id="2728">
<td><a href="https://cplusplus.github.io/CWG/issues/2728.html">2728</a></td>
<td>tentatively ready</td>
<td>Evaluation of conversions in a <I>delete-expression</I></td>
- <td class="unknown" align="center">Unknown</td>
+ <td align="center">Not resolved</td>
</tr>
<tr id="2729">
<td><a href="https://cplusplus.github.io/CWG/issues/2729.html">2729</a></td>
@@ -16713,17 +16713,17 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>sizeof(abstract class) is underspecified</td>
<td align="center">Not resolved</td>
</tr>
- <tr id="2818">
+ <tr class="open" id="2818">
<td><a href="https://cplusplus.github.io/CWG/issues/2818.html">2818</a></td>
<td>tentatively ready</td>
<td>Use of predefined reserved identifiers</td>
- <td class="unknown" align="center">Unknown</td>
+ <td align="center">Not resolved</td>
</tr>
- <tr id="2819">
+ <tr class="open" id="2819">
<td><a href="https://cplusplus.github.io/CWG/issues/2819.html">2819</a></td>
<td>tentatively ready</td>
<td>Cast from null pointer value in a constant expression</td>
- <td class="unreleased" align="center">Clang 19</td>
+ <td title="Clang 19 implements 2023-12-01 resolution" align="center">Not Resolved*</td>
</tr>
<tr id="2820">
<td><a href="https://cplusplus.github.io/CWG/issues/2820.html">2820</a></td>
@@ -16953,17 +16953,17 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>Argument-dependent lookup with incomplete class types</td>
<td class="unknown" align="center">Unknown</td>
</tr>
- <tr id="2858">
+ <tr class="open" id="2858">
<td><a href="https://cplusplus.github.io/CWG/issues/2858.html">2858</a></td>
<td>tentatively ready</td>
<td>Declarative <I>nested-name-specifier</I>s and <I>pack-index-specifier</I>s</td>
- <td class="unreleased" align="center">Clang 19</td>
+ <td title="Clang 19 implements 2024-04-05 resolution" align="center">Not Resolved*</td>
</tr>
- <tr id="2859">
+ <tr class="open" id="2859">
<td><a href="https://cplusplus.github.io/CWG/issues/2859.html">2859</a></td>
<td>tentatively ready</td>
<td>Value-initialization with multiple default constructors</td>
- <td class="unknown" align="center">Unknown</td>
+ <td align="center">Not resolved</td>
</tr>
<tr id="2860">
<td><a href="https://cplusplus.github.io/CWG/issues/2860.html">2860</a></td>
@@ -16971,29 +16971,29 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>Remove and fix the term "vacuous initialization"</td>
<td class="unknown" align="center">Unknown</td>
</tr>
- <tr id="2861">
+ <tr class="open" id="2861">
<td><a href="https://cplusplus.github.io/CWG/issues/2861.html">2861</a></td>
<td>tentatively ready</td>
<td><TT>dynamic_cast</TT> on bad pointer value</td>
- <td class="unknown" align="center">Unknown</td>
+ <td align="center">Not resolved</td>
</tr>
- <tr id="2862">
+ <tr class="open" id="2862">
<td><a href="https://cplusplus.github.io/CWG/issues/2862.html">2862</a></td>
<td>tentatively ready</td>
<td>Unclear boundaries of template declarations</td>
- <td class="unknown" align="center">Unknown</td>
+ <td align="center">Not resolved</td>
</tr>
- <tr id="2863">
+ <tr class="open" id="2863">
<td><a href="https://cplusplus.github.io/CWG/issues/2863.html">2863</a></td>
<td>tentatively ready</td>
<td>Unclear synchronization requirements for object lifetime rules</td>
- <td class="unknown" align="center">Unknown</td>
+ <td align="center">Not resolved</td>
</tr>
- <tr id="2864">
+ <tr class="open" id="2864">
<td><a href="https://cplusplus.github.io/CWG/issues/2864.html">2864</a></td>
<td>tentatively ready</td>
<td>Narrowing floating-point conversions</td>
- <td class="unknown" align="center">Unknown</td>
+ <td align="center">Not resolved</td>
</tr>
<tr class="open" id="2865">
<td><a href="https://cplusplus.github.io/CWG/issues/2865.html">2865</a></td>
@@ -17031,11 +17031,11 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>Combining absent <I>encoding-prefix</I>es</td>
<td align="center">Not resolved</td>
</tr>
- <tr id="2871">
+ <tr class="open" id="2871">
<td><a href="https://cplusplus.github.io/CWG/issues/2871.html">2871</a></td>
<td>tentatively ready</td>
<td>User-declared constructor templates inhibiting default constructors</td>
- <td class="unknown" align="center">Unknown</td>
+ <td align="center">Not resolved</td>
</tr>
<tr class="open" id="2872">
<td><a href="https://cplusplus.github.io/CWG/issues/2872.html">2872</a></td>
diff --git a/clang/www/make_cxx_dr_status b/clang/www/make_cxx_dr_status
index 57f1dc86fd1251..47c8b3bae4a179 100755
--- a/clang/www/make_cxx_dr_status
+++ b/clang/www/make_cxx_dr_status
@@ -236,7 +236,7 @@ for dr in drs:
avail = 'Extension'
avail_style = ''
- elif dr.status in ('open', 'drafting', 'review'):
+ elif dr.status in ('open', 'drafting', 'review', 'tentatively ready'):
row_style = ' class="open"'
try:
avail, avail_style, unresolved_status = availability(dr.issue)
More information about the cfe-commits
mailing list