[clang] [Clang] allow [[msvc::constexpr]] usage outside the std namespace (PR #119153)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 9 03:48:37 PST 2024
https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/119153
>From f19ea82bbe5c00af6a6e261f989c9a89ef4c78ca Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Mon, 9 Dec 2024 01:46:46 +0200
Subject: [PATCH 1/2] [Clang] allow [[msvc::constexpr]] usage outside the std
namespace
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/AST/ExprConstant.cpp | 4 +++-
clang/test/AST/ms-constexpr-new.cpp | 13 +++++++++++++
3 files changed, 18 insertions(+), 1 deletion(-)
create mode 100644 clang/test/AST/ms-constexpr-new.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3f58e64cf0ccbc..71666e26146e92 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -525,6 +525,8 @@ Attribute Changes in Clang
- The ``target_version`` attribute is now only supported for AArch64 and RISC-V architectures.
+- Clang now permits ``[[msvc::constexpr]]`` usage outside of the std namespace. (#GH74924)
+
Improvements to Clang's diagnostics
-----------------------------------
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 6b5b95aee35522..9dbb350be59091 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -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)
diff --git a/clang/test/AST/ms-constexpr-new.cpp b/clang/test/AST/ms-constexpr-new.cpp
new file mode 100644
index 00000000000000..4b534cf0207644
--- /dev/null
+++ b/clang/test/AST/ms-constexpr-new.cpp
@@ -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());
>From 6ec3ea6ec803efddb095ac5b30d6c82ce2fb86a0 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Mon, 9 Dec 2024 13:48:23 +0200
Subject: [PATCH 2/2] add tests to cover unsupported cases
---
clang/test/SemaCXX/ms-constexpr-new.cpp | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/clang/test/SemaCXX/ms-constexpr-new.cpp b/clang/test/SemaCXX/ms-constexpr-new.cpp
index 30567740b2ecbb..096014be803e77 100644
--- a/clang/test/SemaCXX/ms-constexpr-new.cpp
+++ b/clang/test/SemaCXX/ms-constexpr-new.cpp
@@ -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()'}}
More information about the cfe-commits
mailing list