cor3ntin updated this revision to Diff 534328.
cor3ntin added a comment.

Rebase and update feature macro tests.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153702/new/

https://reviews.llvm.org/D153702

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Frontend/InitPreprocessor.cpp
  clang/test/CXX/expr/expr.const/p5-26.cpp
  clang/test/Lexer/cxx-features.cpp
  clang/www/cxx_status.html

Index: clang/www/cxx_status.html
===================================================================
--- clang/www/cxx_status.html
+++ clang/www/cxx_status.html
@@ -125,7 +125,7 @@
  <tr>
   <td>constexpr cast from <tt>void*</tt></td>
   <td><a href="https://wg21.link/P2738R1";>P2738R1</a></td>
-  <td class="none" align="center">No</td>
+  <td class="unreleased" align="center">Clang 17</td>
  </tr>
  <tr>
   <td>On the ignorability of standard attributes</td>
Index: clang/test/Lexer/cxx-features.cpp
===================================================================
--- clang/test/Lexer/cxx-features.cpp
+++ clang/test/Lexer/cxx-features.cpp
@@ -300,7 +300,7 @@
 #error "wrong value for __cpp_lambdas"
 #endif
 
-#if check(constexpr, 0, 200704, 201304, 201603, 201907, 202211, 202211)
+#if check(constexpr, 0, 200704, 201304, 201603, 201907, 202211, 202306)
 #error "wrong value for __cpp_constexpr"
 #endif
 
Index: clang/test/CXX/expr/expr.const/p5-26.cpp
===================================================================
--- /dev/null
+++ clang/test/CXX/expr/expr.const/p5-26.cpp
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++2c -verify %s
+
+struct S {};
+struct T : S {} t;
+
+consteval void test() {
+    void* a = &t;
+    const void* b = &t;
+    volatile void* c = &t;
+    (void)static_cast<T*>(a);
+    (void)static_cast<const T*>(a);
+    (void)static_cast<volatile T*>(a);
+
+    (void)(T*)(a);
+    (void)(const T*)(a);
+    (void)(volatile T*)(a);
+
+    (void)static_cast<T*>(b); // expected-error {{static_cast from 'const void *' to 'T *' casts away qualifiers}}
+    (void)static_cast<volatile T*>(b); // expected-error {{static_cast from 'const void *' to 'volatile T *' casts away qualifiers}}
+    (void)static_cast<const T*>(b);
+    (void)static_cast<volatile const T*>(b);
+
+    (void)static_cast<T*>(c); // expected-error{{static_cast from 'volatile void *' to 'T *' casts away qualifiers}}
+    (void)static_cast<volatile T*>(c);
+    (void)static_cast<const T*>(b);
+    (void)static_cast<volatile const T*>(b);
+}
+
+void err() {
+    constexpr void* a = &t;
+    constexpr auto err1 = static_cast<int*>(a); // expected-error{{constexpr variable 'err1' must be initialized by a constant expression}} \
+                                                // expected-note {{cast from 'void *' is not allowed in a constant expression because the pointed object type 'T' is not similar to the target type 'int'}}
+    constexpr auto err2 = static_cast<S*>(a);   // expected-error{{constexpr variable 'err2' must be initialized by a constant expression}} \
+                                                // expected-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'}}
+}
Index: clang/lib/Frontend/InitPreprocessor.cpp
===================================================================
--- clang/lib/Frontend/InitPreprocessor.cpp
+++ clang/lib/Frontend/InitPreprocessor.cpp
@@ -611,7 +611,8 @@
     Builder.defineMacro("__cpp_unicode_literals", "200710L");
     Builder.defineMacro("__cpp_user_defined_literals", "200809L");
     Builder.defineMacro("__cpp_lambdas", "200907L");
-    Builder.defineMacro("__cpp_constexpr", LangOpts.CPlusPlus23   ? "202211L"
+    Builder.defineMacro("__cpp_constexpr", LangOpts.CPlusPlus26   ? "202306L"
+                                           : LangOpts.CPlusPlus23 ? "202211L"
                                            : LangOpts.CPlusPlus20 ? "201907L"
                                            : LangOpts.CPlusPlus17 ? "201603L"
                                            : LangOpts.CPlusPlus14 ? "201304L"
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -8896,9 +8896,10 @@
     if (!E->getType()->isVoidPointerType()) {
       // In some circumstances, we permit casting from void* to cv1 T*, when the
       // actual pointee object is actually a cv2 T.
+      bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&
+                            !Result.IsNullPtr;
       bool VoidPtrCastMaybeOK =
-          !Result.InvalidBase && !Result.Designator.Invalid &&
-          !Result.IsNullPtr &&
+          HasValidResult &&
           Info.Ctx.hasSameUnqualifiedType(Result.Designator.getType(Info.Ctx),
                                           E->getType()->getPointeeType());
       // 1. We'll allow it in std::allocator::allocate, and anything which that
@@ -8910,16 +8911,23 @@
       //    that back to `const __impl*` in its body.
       if (VoidPtrCastMaybeOK &&
           (Info.getStdAllocatorCaller("allocate") ||
-           IsDeclSourceLocationCurrent(Info.CurrentCall->Callee))) {
+           IsDeclSourceLocationCurrent(Info.CurrentCall->Callee) ||
+           Info.getLangOpts().CPlusPlus26)) {
         // Permitted.
       } else {
-        Result.Designator.setInvalid();
-        if (SubExpr->getType()->isVoidPointerType())
-          CCEDiag(E, diag::note_constexpr_invalid_cast)
-              << 3 << SubExpr->getType();
-        else
+        if (SubExpr->getType()->isVoidPointerType()) {
+          if (HasValidResult)
+            CCEDiag(E, diag::note_constexpr_invalid_void_star_cast)
+                << SubExpr->getType() << Info.getLangOpts().CPlusPlus26
+                << Result.Designator.getType(Info.Ctx).getCanonicalType()
+                << E->getType()->getPointeeType();
+          else
+            CCEDiag(E, diag::note_constexpr_invalid_cast)
+                << 3 << SubExpr->getType();
+        } else
           CCEDiag(E, diag::note_constexpr_invalid_cast)
               << 2 << Info.Ctx.getLangOpts().CPlusPlus;
+        Result.Designator.setInvalid();
       }
     }
     if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr)
Index: clang/include/clang/Basic/DiagnosticASTKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticASTKinds.td
+++ clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -15,6 +15,10 @@
   " performs the conversions of a reinterpret_cast}1|cast from %1}0"
   " is not allowed in a constant expression"
   "%select{| in C++ standards before C++20||}0">;
+def note_constexpr_invalid_void_star_cast : Note<
+  "cast from %0 is not allowed in a constant expression "
+  "%select{in C++ standards before C++2c|because the pointed object "
+  "type %2 is not similar to the target type %3}1">;
 def note_constexpr_invalid_downcast : Note<
   "cannot cast object of dynamic type %0 to type %1">;
 def note_constexpr_overflow : Note<
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -132,6 +132,7 @@
 C++2c Feature Support
 ^^^^^^^^^^^^^^^^^^^^^
 - Compiler flags ``-std=c++2c`` and ``-std=gnu++2c`` have been added for experimental C++2c implementation work.
+- Implemented `P2738R1: constexpr cast from void* <https://wg21.link/P2738R1>`_.
 
 Resolutions to C++ Defect Reports
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to