Skip to content

[Clang] allow usage of placement new operator in [[msvc::constexpr]] context outside of the std namespace #119153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,9 @@ Attribute Changes in Clang

- The ``target_version`` attribute is now only supported for AArch64 and RISC-V architectures.

- Clang now permits the usage of the placement new operator in ``[[msvc::constexpr]]``
context outside of the std namespace. (#GH74924)

Improvements to Clang's diagnostics
-----------------------------------

Expand Down
4 changes: 3 additions & 1 deletion clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10172,7 +10172,9 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {
return false;
IsNothrow = true;
} else if (OperatorNew->isReservedGlobalPlacementOperator()) {
if (Info.CurrentCall->isStdFunction() || Info.getLangOpts().CPlusPlus26) {
if (Info.CurrentCall->isStdFunction() || Info.getLangOpts().CPlusPlus26 ||
(Info.CurrentCall->CanEvalMSConstexpr &&
OperatorNew->hasAttr<MSConstexprAttr>())) {
if (!EvaluatePointer(E->getPlacementArg(0), Result, Info))
return false;
if (Result.Designator.Invalid)
Expand Down
13 changes: 13 additions & 0 deletions clang/test/AST/ms-constexpr-new.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -ast-dump %s | FileCheck %s

// CHECK: used operator new
// CHECK: MSConstexprAttr 0x{{[0-9a-f]+}} <col:17, col:23>
[[nodiscard]] [[msvc::constexpr]] inline void* __cdecl operator new(decltype(sizeof(void*)), void* p) noexcept { return p; }

// CHECK: used constexpr construct_at
// CHECK: AttributedStmt 0x{{[0-9a-f]+}} <col:46, col:88>
// CHECK-NEXT: MSConstexprAttr 0x{{[0-9a-f]+}} <col:48, col:54>
// CHECK-NEXT: ReturnStmt 0x{{[0-9a-f]+}} <col:66, col:88>
constexpr int* construct_at(int* p, int v) { [[msvc::constexpr]] return ::new (p) int(v); }
constexpr bool check_construct_at() { int x; return *construct_at(&x, 42) == 42; }
static_assert(check_construct_at());
11 changes: 9 additions & 2 deletions clang/test/SemaCXX/ms-constexpr-new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ namespace std {
}
}

constexpr bool check_construct_at() { int x; return *std::construct_at(&x, 42) == 42; }
static_assert(check_construct_at());
constexpr bool check_std_construct_at() { int x; return *std::construct_at(&x, 42) == 42; }
static_assert(check_std_construct_at());

constexpr int* construct_at(int* p, int v) { [[msvc::constexpr]] return ::new (p) int(v); } // unsupported-error {{constexpr function never produces a constant expression}} \
// unsupported-warning {{unknown attribute 'constexpr' ignored}} \
// unsupported-note 2{{this placement new expression is not supported in constant expressions before C++2c}}
constexpr bool check_construct_at() { int x; return *construct_at(&x, 42) == 42; } // unsupported-note {{in call to 'construct_at(&x, 42)'}}
static_assert(check_construct_at()); // unsupported-error {{static assertion expression is not an integral constant expression}}\
// unsupported-note {{in call to 'check_construct_at()'}}
Loading