diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 76f5313cd8fa5..055aa9008b03b 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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* `_. Resolutions to C++ Defect Reports ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td b/clang/include/clang/Basic/DiagnosticASTKinds.td index 73118891c1899..856293dd45d08 100644 --- a/clang/include/clang/Basic/DiagnosticASTKinds.td +++ b/clang/include/clang/Basic/DiagnosticASTKinds.td @@ -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< diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 65d69d9b49438..19d8e24fef63c 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -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 @@ -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) diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 76392c104de26..431b9ea36c434 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -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" diff --git a/clang/test/CXX/expr/expr.const/p5-26.cpp b/clang/test/CXX/expr/expr.const/p5-26.cpp new file mode 100644 index 0000000000000..03d28d39fd3bb --- /dev/null +++ b/clang/test/CXX/expr/expr.const/p5-26.cpp @@ -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(a); //cxx23-note {{cast from 'void *' is not allowed in a constant expression in C++ standards before C++2c}} + (void)static_cast(a); + (void)static_cast(a); + + (void)(T*)(a); + (void)(const T*)(a); + (void)(volatile T*)(a); + + (void)static_cast(b); // expected-error {{static_cast from 'const void *' to 'T *' casts away qualifiers}} + (void)static_cast(b); // expected-error {{static_cast from 'const void *' to 'volatile T *' casts away qualifiers}} + (void)static_cast(b); + (void)static_cast(b); + + (void)static_cast(c); // expected-error{{static_cast from 'volatile void *' to 'T *' casts away qualifiers}} + (void)static_cast(c); + (void)static_cast(b); + (void)static_cast(b); +} + +void err() { + constexpr void* a = &t; + constexpr auto err1 = static_cast(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(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'}} +} diff --git a/clang/test/Lexer/cxx-features.cpp b/clang/test/Lexer/cxx-features.cpp index 1b6c793e1defb..5c5408c29f879 100644 --- a/clang/test/Lexer/cxx-features.cpp +++ b/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 diff --git a/clang/www/cxx_status.html b/clang/www/cxx_status.html index 4eea21cb9da47..94c96ba0614e2 100755 --- a/clang/www/cxx_status.html +++ b/clang/www/cxx_status.html @@ -125,7 +125,7 @@

C++2c implementation status

constexpr cast from void* P2738R1 - No + Clang 17 On the ignorability of standard attributes