aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoroffsetof <131769984+offsetof@users.noreply.github.com>2024-04-29 13:23:50 +0000
committerGitHub <noreply@github.com>2024-04-29 17:23:50 +0400
commitcaa902613a96f63c3855b3a0bcd82d1b1db49408 (patch)
tree9fa8de082e87c4c2250f0be0103a6f5ced89df48
parente57b8724140664da408da92d5d50f9a1e0c9828a (diff)
downloadllvm-caa902613a96f63c3855b3a0bcd82d1b1db49408.zip
llvm-caa902613a96f63c3855b3a0bcd82d1b1db49408.tar.gz
llvm-caa902613a96f63c3855b3a0bcd82d1b1db49408.tar.bz2
[clang] Allow constexpr cast from `void*` in more cases (#89484)
[[expr.const]/5.14](https://eel.is/c++draft/expr.const#5.14) says that constexpr cast from <code>*cv* void\*</code> to `T*` is OK if the pointee type is similar to `T`, but Clang currently only permits the conversion if the types are the same except top-level cv-qualifiers. This patch also allows casting `(void*)nullptr`, implementing the resolution of [CWG2819](https://cplusplus.github.io/CWG/issues/2819). --------- Co-authored-by: Vlad Serebrennikov <serebrennikov.vladislav@gmail.com>
-rw-r--r--clang/lib/AST/ExprConstant.cpp7
-rw-r--r--clang/test/CXX/drs/dr25xx.cpp2
-rw-r--r--clang/test/CXX/drs/dr28xx.cpp10
-rw-r--r--clang/test/CXX/expr/expr.const/p5-26.cpp7
-rwxr-xr-xclang/www/cxx_dr_status.html82
-rwxr-xr-xclang/www/make_cxx_dr_status2
6 files changed, 78 insertions, 32 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index ea3e730..71b5f91 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/dr25xx.cpp b/clang/test/CXX/drs/dr25xx.cpp
index 481ae09..8bca58f 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 1967e8b..be35d36 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 tentatively ready 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 review 2024-03-01
#if __cplusplus >= 202002L
@@ -59,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/test/CXX/expr/expr.const/p5-26.cpp b/clang/test/CXX/expr/expr.const/p5-26.cpp
index 3624b1e..7513b11 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@-1 {{constant expression}}
+// cxx23-note@-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 19d29cb..875521b 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -1433,11 +1433,11 @@ accessible?</td>
<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 @@ and <I>POD class</I></td>
<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 @@ and <I>POD class</I></td>
<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 @@ and <I>POD class</I></td>
<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 @@ objects</td>
<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="unknown" align="center">Unknown</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 @@ objects</td>
<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 @@ objects</td>
<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 @@ objects</td>
<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>
@@ -17096,6 +17096,36 @@ objects</td>
<td>open</td>
<td>Type restrictions for the explicit object parameter of a lambda</td>
<td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="2882">
+ <td><a href="https://cplusplus.github.io/CWG/issues/2882.html">2882</a></td>
+ <td>open</td>
+ <td>Unclear treatment of conversion to <TT>void</TT></td>
+ <td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="2883">
+ <td><a href="https://cplusplus.github.io/CWG/issues/2883.html">2883</a></td>
+ <td>open</td>
+ <td>Definition of "odr-usable" ignores lambda scopes</td>
+ <td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="2884">
+ <td><a href="https://cplusplus.github.io/CWG/issues/2884.html">2884</a></td>
+ <td>open</td>
+ <td>Qualified declarations of partial specializations</td>
+ <td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="2885">
+ <td><a href="https://cplusplus.github.io/CWG/issues/2885.html">2885</a></td>
+ <td>open</td>
+ <td>Non-eligible trivial default constructors</td>
+ <td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="2886">
+ <td><a href="https://cplusplus.github.io/CWG/issues/2886.html">2886</a></td>
+ <td>open</td>
+ <td>Temporaries and trivial potentially-throwing special member functions</td>
+ <td align="center">Not resolved</td>
</tr></table>
</div>
diff --git a/clang/www/make_cxx_dr_status b/clang/www/make_cxx_dr_status
index 57f1dc8..47c8b3b 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)