[clang] [clang] Allow constexpr cast from `void*` in more cases (PR #89484)
github-actions[bot] wrote: @offsetof Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our [build bots](https://lab.llvm.org/buildbot/). If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail [here](https://llvm.org/docs/MyFirstTypoFix.html#myfirsttypofix-issues-after-landing-your-pr). If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of [LLVM development](https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy). You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! https://github.com/llvm/llvm-project/pull/89484 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Allow constexpr cast from `void*` in more cases (PR #89484)
https://github.com/Endilll closed https://github.com/llvm/llvm-project/pull/89484 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Allow constexpr cast from `void*` in more cases (PR #89484)
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+offse...@users.noreply.github.com> Date: Sat, 20 Apr 2024 02:35:09 + 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(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 = +constexpr void* vp = pp; +constexpr auto cvp = static_cast(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 83b71e7c122d1e..2677364065b2f2 100755 --- a/clang/www/cxx_dr_status.html +++ b/clang/www/cxx_dr_status.html @@ -16722,7 +16722,7 @@ C++ defect report implementation status https://cplusplus.github.io/CWG/issues/2819.html;>2819 review Cast from null pointer value in a constant expression -Not resolved +Not Resolved* https://cplusplus.github.io/CWG/issues/2820.html;>2820 >From d858d685647780659d249d64ad99e09e00c7ade0 Mon Sep 17 00:00:00 2001 From: Vlad Serebrennikov 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
[clang] [clang] Allow constexpr cast from `void*` in more cases (PR #89484)
github-actions[bot] wrote: ⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo. Please turn off [Keep my email addresses private](https://github.com/settings/emails) setting in your account. See [LLVM Discourse](https://discourse.llvm.org/t/hidden-emails-on-github-should-we-do-something-about-it) for more information. https://github.com/llvm/llvm-project/pull/89484 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Allow constexpr cast from `void*` in more cases (PR #89484)
offsetof wrote: Yes, that would be great, thank you. https://github.com/llvm/llvm-project/pull/89484 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Allow constexpr cast from `void*` in more cases (PR #89484)
https://github.com/cor3ntin approved this pull request. LGTM, thanks! https://github.com/llvm/llvm-project/pull/89484 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Allow constexpr cast from `void*` in more cases (PR #89484)
llvmbot wrote: @llvm/pr-subscribers-clang Author: None (offsetof) Changes [[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). --- Full diff: https://github.com/llvm/llvm-project/pull/89484.diff 4 Files Affected: - (modified) clang/lib/AST/ExprConstant.cpp (+4-3) - (modified) clang/test/CXX/drs/dr28xx.cpp (+8) - (modified) clang/test/CXX/expr/expr.const/p5-26.cpp (+7) - (modified) clang/www/cxx_dr_status.html (+1-1) ``diff 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(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 = +constexpr void* vp = pp; +constexpr auto cvp = static_cast(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 83b71e7c122d1e..2677364065b2f2 100755 --- a/clang/www/cxx_dr_status.html +++ b/clang/www/cxx_dr_status.html @@ -16722,7 +16722,7 @@ C++ defect report implementation status https://cplusplus.github.io/CWG/issues/2819.html;>2819 review Cast from null pointer value in a constant expression -Not resolved +Not Resolved* https://cplusplus.github.io/CWG/issues/2820.html;>2820 `` https://github.com/llvm/llvm-project/pull/89484 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Allow constexpr cast from `void*` in more cases (PR #89484)
github-actions[bot] wrote: Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using `@` followed by their GitHub username. If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the [LLVM GitHub User Guide](https://llvm.org/docs/GitHub.html). You can also ask questions in a comment on this PR, on the [LLVM Discord](https://discord.com/invite/xS7Z362) or on the [forums](https://discourse.llvm.org/). https://github.com/llvm/llvm-project/pull/89484 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Allow constexpr cast from `void*` in more cases (PR #89484)
https://github.com/offsetof created https://github.com/llvm/llvm-project/pull/89484 [[expr.const]/5.14](https://eel.is/c++draft/expr.const#5.14) says that constexpr cast from *cv* void\* 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). >From 5985dbe47e052505278d60628bbb5ca751cc3b6c Mon Sep 17 00:00:00 2001 From: offsetof <131769984+offse...@users.noreply.github.com> Date: Sat, 20 Apr 2024 02:35:09 + Subject: [PATCH] [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(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 = +constexpr void* vp = pp; +constexpr auto cvp = static_cast(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 83b71e7c122d1e..2677364065b2f2 100755 --- a/clang/www/cxx_dr_status.html +++ b/clang/www/cxx_dr_status.html @@ -16722,7 +16722,7 @@ C++ defect report implementation status https://cplusplus.github.io/CWG/issues/2819.html;>2819 review Cast from null pointer value in a constant expression -Not resolved +Not Resolved* https://cplusplus.github.io/CWG/issues/2820.html;>2820 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits