Skip to content

Commit

Permalink
[Clang] Implement P2738R1 - constexpr cast from void*
Browse files Browse the repository at this point in the history
Reviewed By: #clang-language-wg, erichkeane

Differential Revision: https://reviews.llvm.org/D153702
  • Loading branch information
cor3ntin committed Jun 26, 2023
1 parent ad99b78 commit f27afed
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 11 deletions.
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ C++23 Feature Support
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/DiagnosticASTKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def note_constexpr_invalid_cast : Note<
" 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<
Expand Down
24 changes: 16 additions & 8 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8897,9 +8897,10 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
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
Expand All @@ -8911,16 +8912,23 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
// 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)
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Frontend/InitPreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,8 @@ static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
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"
Expand Down
39 changes: 39 additions & 0 deletions clang/test/CXX/expr/expr.const/p5-26.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++2c -verify=expected,cxx26 %s
// RUN: %clang_cc1 -fsyntax-only -std=c++2b -verify=expected=cxx23 %s


struct S {};
struct T : S {} t;

consteval void test() { // cxx23-error{{consteval function never produces a constant expression}}
void* a = &t;
const void* b = &t;
volatile void* c = &t;
(void)static_cast<T*>(a); //cxx23-note {{cast from 'void *' is not allowed in a constant expression in C++ standards before C++2c}}
(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}} \
// 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 'int'}}
constexpr auto err2 = static_cast<S*>(a); // expected-error{{constexpr variable 'err2' must be initialized by a constant expression}} \
// 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'}}
}
2 changes: 1 addition & 1 deletion clang/test/Lexer/cxx-features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion clang/www/cxx_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ <h2 id="cxx26">C++2c implementation status</h2>
<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>
Expand Down

0 comments on commit f27afed

Please sign in to comment.