[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-11-04 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt abandoned this revision.
RIscRIpt added a comment.

As per agreement, migrating to Github: 
https://github.com/llvm/llvm-project/pull/71300


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-10-27 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt marked an inline comment as done.
RIscRIpt added a comment.

In D134475#4655389 , @erichkeane 
wrote:

> If you can properly rebase, submit this to github, and include a summary of 
> the Phab discussion in a way that easy enough to catch back up on, I'll do a 
> deep dive into this in the next week or 3.
> Sorry for the delay, this fell off my radar somehow.

Sure, I'll try to write a summary of comments, and submit it to GitHub.
No problems regarding delays, I couldn't work at this patch in the beginning of 
this year, so it was delayed for so long.




Comment at: clang/test/SemaCXX/ms-constexpr.cpp:30
+/*
+// TODO: Add support for [[msvc::constexpr]] constructor
+struct S2 {

erichkeane wrote:
> I see this is still TODO, part of the reason I have been putting off review, 
> I thought this patch wasn't ready for review.  Whats going on here?  When is 
> this intended to be implemented?
Unfortunately, I don't think it's feasible with reasonable changes. I explained 
it in comment https://reviews.llvm.org/D134475#4288759 - search for "literal 
type".
Basically, support of `[[msvc::constexpr]]` constructors is not needed to be 
able to compile code from Microsoft's C++ Standard Library.

I think, I could create a follow up issue on GitHub, and make a reference to 
the issue here.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-10-27 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

In D134475#4655362 , @RIscRIpt wrote:

> Should I re-submit it to GitHub?

Youch... I'm sorry this hasn't managed to get up to the top of my queue yet! I 
agree with Erich, as unfortunate as it is not to complete the review in Phab, 
we probably should put it up on GitHub so we don't run into the deadline for 
putting phab into read-only mode. I should hopefully be able to dedicate time 
to review to this in the next several weeks.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-10-27 Thread Erich Keane via Phabricator via cfe-commits
erichkeane added a comment.

In D134475#4655362 , @RIscRIpt wrote:

> Should I re-submit it to GitHub?

While I hate that we'll lose all of the Phabriator history on this one, I don't 
see being able to complete this review in the next two weeks before Phab closes 
down.  The C++ Standards meeting is in 2 weeks, which makes it particularly 
difficult.

If you can properly rebase, submit this to github, and include a summary of the 
Phab discussion in a way that easy enough to catch back up on, I'll do a deep 
dive into this in the next week or 3.

Sorry for the delay, this fell off my radar somehow.




Comment at: clang/test/SemaCXX/ms-constexpr.cpp:30
+/*
+// TODO: Add support for [[msvc::constexpr]] constructor
+struct S2 {

I see this is still TODO, part of the reason I have been putting off review, I 
thought this patch wasn't ready for review.  Whats going on here?  When is this 
intended to be implemented?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-10-27 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt added a comment.

Should I re-submit it to GitHub?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-09-23 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt updated this revision to Diff 557270.
RIscRIpt added a comment.

Rebase onto main. Run lit clang/test. Bump.

Any chance we continue the review? I know it looked stale for a while I 
couldn't address review comments, but now I have time to work at this patch.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

Files:
  clang/docs/ReleaseNotes.rst
  clang/docs/UsersManual.rst
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Basic/LangOptions.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Basic/Targets/OSTargets.cpp
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaStmtAttr.cpp
  clang/test/AST/ms-constexpr.cpp
  clang/test/Driver/cl-options.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/ms-constexpr-invalid.cpp
  clang/test/SemaCXX/ms-constexpr-new.cpp
  clang/test/SemaCXX/ms-constexpr.cpp

Index: clang/test/SemaCXX/ms-constexpr.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr.cpp
@@ -0,0 +1,37 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify %s
+// expected-no-diagnostics
+
+[[msvc::constexpr]] int log2(int x) { [[msvc::constexpr]] return x > 1 ? 1 + log2(x / 2) : 0; }
+constexpr bool test_log2() { [[msvc::constexpr]] return log2(32) == 5; }
+static_assert(test_log2());
+
+[[msvc::constexpr]] int get_value(int x)
+{
+  switch (x)
+  {
+case 42: return 1337;
+default:
+ if (x < 0) [[msvc::constexpr]] return log2(-x);
+ else return x;
+  }
+}
+
+constexpr bool test_complex_expr() {
+  [[msvc::constexpr]] return get_value(get_value(42) - 1337 + get_value(-32) - 5 + (get_value(1) ? get_value(0) : get_value(2))) == get_value(0);
+}
+static_assert(test_complex_expr());
+
+constexpr bool get_constexpr_true() { return true; }
+[[msvc::constexpr]] bool get_msconstexpr_true() { return get_constexpr_true(); }
+constexpr bool test_get_msconstexpr_true() { [[msvc::constexpr]] return get_msconstexpr_true(); }
+static_assert(test_get_msconstexpr_true());
+
+/*
+// TODO: Add support for [[msvc::constexpr]] constructor
+struct S2 {
+[[msvc::constexpr]] S2() {}
+[[msvc::constexpr]] bool value() { return true; }
+static constexpr bool check() { [[msvc::constexpr]] return S2{}.value(); }
+};
+static_assert(S2::check());
+*/
Index: clang/test/SemaCXX/ms-constexpr-new.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr-new.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify=supported %s
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.32 -std=c++20 -verify=unsupported %s
+// supported-no-diagnostics
+
+[[nodiscard]]
+[[msvc::constexpr]] // unsupported-warning {{unknown attribute 'constexpr' ignored}}
+inline void* __cdecl operator new(decltype(sizeof(void*)), void* p) noexcept { return p; }
+
+namespace std {
+  constexpr int* construct_at(int* p, int v) {
+[[msvc::constexpr]] return ::new (p) int(v); // unsupported-warning {{unknown attribute 'constexpr' ignored}}
+  }
+}
+
+constexpr bool check_construct_at() { int x; return *std::construct_at(, 42) == 42; }
+static_assert(check_construct_at());
Index: clang/test/SemaCXX/ms-constexpr-invalid.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr-invalid.cpp
@@ -0,0 +1,52 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify %s
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++17 -verify %s
+
+// Check explicitly invalid code
+
+void runtime() {} // expected-note {{declared here}}
+
+[[msvc::constexpr]] void f0() { runtime(); } // expected-error {{[[msvc::constexpr]] function never produces a constant expression}} \
+ // expected-note {{non-constexpr function 'runtime' cannot be used in a constant expression}}
+[[msvc::constexpr]] constexpr void f1() {} // expected-error {{[[msvc::constexpr]] cannot be applied to a constexpr function 'f1'}}
+#if __cplusplus >= 202202L
+[[msvc::constexpr]] consteval void f2() {} // expected-error {{[[msvc::constexpr]] cannot be applied to a consteval function 'f2'}}
+#endif
+
+struct B1 {};
+struct D1 : virtual B1 { // expected-note {{virtual base class declared here}}
+[[msvc::constexpr]] D1() {} // expected-error {{constexpr constructor not allowed in struct with virtual base class}}
+};
+
+struct [[msvc::constexpr]] S2{}; // expected-error {{'constexpr' attribute only applies to functions and statements}}
+

[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-08-11 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt updated this revision to Diff 549298.
RIscRIpt added a comment.

Rebase onto main (bump)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

Files:
  clang/docs/ReleaseNotes.rst
  clang/docs/UsersManual.rst
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Basic/LangOptions.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Basic/Targets/OSTargets.cpp
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaStmtAttr.cpp
  clang/test/AST/ms-constexpr.cpp
  clang/test/Driver/cl-options.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/ms-constexpr-invalid.cpp
  clang/test/SemaCXX/ms-constexpr-new.cpp
  clang/test/SemaCXX/ms-constexpr.cpp

Index: clang/test/SemaCXX/ms-constexpr.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr.cpp
@@ -0,0 +1,37 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify %s
+// expected-no-diagnostics
+
+[[msvc::constexpr]] int log2(int x) { [[msvc::constexpr]] return x > 1 ? 1 + log2(x / 2) : 0; }
+constexpr bool test_log2() { [[msvc::constexpr]] return log2(32) == 5; }
+static_assert(test_log2());
+
+[[msvc::constexpr]] int get_value(int x)
+{
+  switch (x)
+  {
+case 42: return 1337;
+default:
+ if (x < 0) [[msvc::constexpr]] return log2(-x);
+ else return x;
+  }
+}
+
+constexpr bool test_complex_expr() {
+  [[msvc::constexpr]] return get_value(get_value(42) - 1337 + get_value(-32) - 5 + (get_value(1) ? get_value(0) : get_value(2))) == get_value(0);
+}
+static_assert(test_complex_expr());
+
+constexpr bool get_constexpr_true() { return true; }
+[[msvc::constexpr]] bool get_msconstexpr_true() { return get_constexpr_true(); }
+constexpr bool test_get_msconstexpr_true() { [[msvc::constexpr]] return get_msconstexpr_true(); }
+static_assert(test_get_msconstexpr_true());
+
+/*
+// TODO: Add support for [[msvc::constexpr]] constructor
+struct S2 {
+[[msvc::constexpr]] S2() {}
+[[msvc::constexpr]] bool value() { return true; }
+static constexpr bool check() { [[msvc::constexpr]] return S2{}.value(); }
+};
+static_assert(S2::check());
+*/
Index: clang/test/SemaCXX/ms-constexpr-new.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr-new.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify=supported %s
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.32 -std=c++20 -verify=unsupported %s
+// supported-no-diagnostics
+
+[[nodiscard]]
+[[msvc::constexpr]] // unsupported-warning {{unknown attribute 'constexpr' ignored}}
+inline void* __cdecl operator new(decltype(sizeof(void*)), void* p) noexcept { return p; }
+
+namespace std {
+  constexpr int* construct_at(int* p, int v) {
+[[msvc::constexpr]] return ::new (p) int(v); // unsupported-warning {{unknown attribute 'constexpr' ignored}}
+  }
+}
+
+constexpr bool check_construct_at() { int x; return *std::construct_at(, 42) == 42; }
+static_assert(check_construct_at());
Index: clang/test/SemaCXX/ms-constexpr-invalid.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr-invalid.cpp
@@ -0,0 +1,52 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify %s
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++17 -verify %s
+
+// Check explicitly invalid code
+
+void runtime() {} // expected-note {{declared here}}
+
+[[msvc::constexpr]] void f0() { runtime(); } // expected-error {{[[msvc::constexpr]] function never produces a constant expression}} \
+ // expected-note {{non-constexpr function 'runtime' cannot be used in a constant expression}}
+[[msvc::constexpr]] constexpr void f1() {} // expected-error {{[[msvc::constexpr]] cannot be applied to a constexpr function 'f1'}}
+#if __cplusplus >= 202202L
+[[msvc::constexpr]] consteval void f2() {} // expected-error {{[[msvc::constexpr]] cannot be applied to a consteval function 'f2'}}
+#endif
+
+struct B1 {};
+struct D1 : virtual B1 { // expected-note {{virtual base class declared here}}
+[[msvc::constexpr]] D1() {} // expected-error {{constexpr constructor not allowed in struct with virtual base class}}
+};
+
+struct [[msvc::constexpr]] S2{}; // expected-error {{'constexpr' attribute only applies to functions and statements}}
+
+// Check invalid code mixed with valid code
+
+[[msvc::constexpr]] int f4(int x) { return x > 1 ? 1 + f4(x / 2) : 0; } // expected-note {{non-constexpr function 'f4' 

[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-07-20 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt updated this revision to Diff 542441.
RIscRIpt added a comment.

Fix clang/test/SemaCXX/ms-constexpr.cpp


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

Files:
  clang/docs/ReleaseNotes.rst
  clang/docs/UsersManual.rst
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Basic/LangOptions.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Basic/Targets/OSTargets.cpp
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaStmtAttr.cpp
  clang/test/AST/ms-constexpr.cpp
  clang/test/Driver/cl-options.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/ms-constexpr-invalid.cpp
  clang/test/SemaCXX/ms-constexpr-new.cpp
  clang/test/SemaCXX/ms-constexpr.cpp

Index: clang/test/SemaCXX/ms-constexpr.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr.cpp
@@ -0,0 +1,37 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify %s
+// expected-no-diagnostics
+
+[[msvc::constexpr]] int log2(int x) { [[msvc::constexpr]] return x > 1 ? 1 + log2(x / 2) : 0; }
+constexpr bool test_log2() { [[msvc::constexpr]] return log2(32) == 5; }
+static_assert(test_log2());
+
+[[msvc::constexpr]] int get_value(int x)
+{
+  switch (x)
+  {
+case 42: return 1337;
+default:
+ if (x < 0) [[msvc::constexpr]] return log2(-x);
+ else return x;
+  }
+}
+
+constexpr bool test_complex_expr() {
+  [[msvc::constexpr]] return get_value(get_value(42) - 1337 + get_value(-32) - 5 + (get_value(1) ? get_value(0) : get_value(2))) == get_value(0);
+}
+static_assert(test_complex_expr());
+
+constexpr bool get_constexpr_true() { return true; }
+[[msvc::constexpr]] bool get_msconstexpr_true() { return get_constexpr_true(); }
+constexpr bool test_get_msconstexpr_true() { [[msvc::constexpr]] return get_msconstexpr_true(); }
+static_assert(test_get_msconstexpr_true());
+
+/*
+// TODO: Add support for [[msvc::constexpr]] constructor
+struct S2 {
+[[msvc::constexpr]] S2() {}
+[[msvc::constexpr]] bool value() { return true; }
+static constexpr bool check() { [[msvc::constexpr]] return S2{}.value(); }
+};
+static_assert(S2::check());
+*/
Index: clang/test/SemaCXX/ms-constexpr-new.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr-new.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify=supported %s
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.32 -std=c++20 -verify=unsupported %s
+// supported-no-diagnostics
+
+[[nodiscard]]
+[[msvc::constexpr]] // unsupported-warning {{unknown attribute 'constexpr' ignored}}
+inline void* __cdecl operator new(decltype(sizeof(void*)), void* p) noexcept { return p; }
+
+namespace std {
+  constexpr int* construct_at(int* p, int v) {
+[[msvc::constexpr]] return ::new (p) int(v); // unsupported-warning {{unknown attribute 'constexpr' ignored}}
+  }
+}
+
+constexpr bool check_construct_at() { int x; return *std::construct_at(, 42) == 42; }
+static_assert(check_construct_at());
Index: clang/test/SemaCXX/ms-constexpr-invalid.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr-invalid.cpp
@@ -0,0 +1,52 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify %s
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++17 -verify %s
+
+// Check explicitly invalid code
+
+void runtime() {} // expected-note {{declared here}}
+
+[[msvc::constexpr]] void f0() { runtime(); } // expected-error {{[[msvc::constexpr]] function never produces a constant expression}} \
+ // expected-note {{non-constexpr function 'runtime' cannot be used in a constant expression}}
+[[msvc::constexpr]] constexpr void f1() {} // expected-error {{[[msvc::constexpr]] cannot be applied to a constexpr function 'f1'}}
+#if __cplusplus >= 202202L
+[[msvc::constexpr]] consteval void f2() {} // expected-error {{[[msvc::constexpr]] cannot be applied to a consteval function 'f2'}}
+#endif
+
+struct B1 {};
+struct D1 : virtual B1 { // expected-note {{virtual base class declared here}}
+[[msvc::constexpr]] D1() {} // expected-error {{constexpr constructor not allowed in struct with virtual base class}}
+};
+
+struct [[msvc::constexpr]] S2{}; // expected-error {{'constexpr' attribute only applies to functions and statements}}
+
+// Check invalid code mixed with valid code
+
+[[msvc::constexpr]] int f4(int x) { return x > 1 ? 1 + f4(x / 2) : 0; } // expected-note {{non-constexpr 

[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-07-20 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt updated this revision to Diff 542438.
RIscRIpt added a comment.

add HEAD~2 patch


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

Files:
  clang/docs/ReleaseNotes.rst
  clang/docs/UsersManual.rst
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Basic/LangOptions.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Basic/Targets/OSTargets.cpp
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaStmtAttr.cpp
  clang/test/AST/ms-constexpr.cpp
  clang/test/Driver/cl-options.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/ms-constexpr-invalid.cpp
  clang/test/SemaCXX/ms-constexpr-new.cpp
  clang/test/SemaCXX/ms-constexpr.cpp

Index: clang/test/SemaCXX/ms-constexpr.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr.cpp
@@ -0,0 +1,37 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -ast-dump -verify %s | FileCheck %s
+// expected-no-diagnostics
+
+[[msvc::constexpr]] int log2(int x) { [[msvc::constexpr]] return x > 1 ? 1 + log2(x / 2) : 0; }
+constexpr bool test_log2() { [[msvc::constexpr]] return log2(32) == 5; }
+static_assert(test_log2());
+
+[[msvc::constexpr]] int get_value(int x)
+{
+  switch (x)
+  {
+case 42: return 1337;
+default:
+ if (x < 0) [[msvc::constexpr]] return log2(-x);
+ else return x;
+  }
+}
+
+constexpr bool test_complex_expr() {
+  [[msvc::constexpr]] return get_value(get_value(42) - 1337 + get_value(-32) - 5 + (get_value(1) ? get_value(0) : get_value(2))) == get_value(0);
+}
+static_assert(test_complex_expr());
+
+constexpr bool get_constexpr_true() { return true; }
+[[msvc::constexpr]] bool get_msconstexpr_true() { return get_constexpr_true(); }
+constexpr bool test_get_msconstexpr_true() { [[msvc::constexpr]] return get_msconstexpr_true(); }
+static_assert(test_get_msconstexpr_true());
+
+/*
+// TODO: Add support for [[msvc::constexpr]] constructor
+struct S2 {
+[[msvc::constexpr]] S2() {}
+[[msvc::constexpr]] bool value() { return true; }
+static constexpr bool check() { [[msvc::constexpr]] return S2{}.value(); }
+};
+static_assert(S2::check());
+*/
Index: clang/test/SemaCXX/ms-constexpr-new.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr-new.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify=supported %s
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.32 -std=c++20 -verify=unsupported %s
+// supported-no-diagnostics
+
+[[nodiscard]]
+[[msvc::constexpr]] // unsupported-warning {{unknown attribute 'constexpr' ignored}}
+inline void* __cdecl operator new(decltype(sizeof(void*)), void* p) noexcept { return p; }
+
+namespace std {
+  constexpr int* construct_at(int* p, int v) {
+[[msvc::constexpr]] return ::new (p) int(v); // unsupported-warning {{unknown attribute 'constexpr' ignored}}
+  }
+}
+
+constexpr bool check_construct_at() { int x; return *std::construct_at(, 42) == 42; }
+static_assert(check_construct_at());
Index: clang/test/SemaCXX/ms-constexpr-invalid.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/ms-constexpr-invalid.cpp
@@ -0,0 +1,52 @@
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify %s
+// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++17 -verify %s
+
+// Check explicitly invalid code
+
+void runtime() {} // expected-note {{declared here}}
+
+[[msvc::constexpr]] void f0() { runtime(); } // expected-error {{[[msvc::constexpr]] function never produces a constant expression}} \
+ // expected-note {{non-constexpr function 'runtime' cannot be used in a constant expression}}
+[[msvc::constexpr]] constexpr void f1() {} // expected-error {{[[msvc::constexpr]] cannot be applied to a constexpr function 'f1'}}
+#if __cplusplus >= 202202L
+[[msvc::constexpr]] consteval void f2() {} // expected-error {{[[msvc::constexpr]] cannot be applied to a consteval function 'f2'}}
+#endif
+
+struct B1 {};
+struct D1 : virtual B1 { // expected-note {{virtual base class declared here}}
+[[msvc::constexpr]] D1() {} // expected-error {{constexpr constructor not allowed in struct with virtual base class}}
+};
+
+struct [[msvc::constexpr]] S2{}; // expected-error {{'constexpr' attribute only applies to functions and statements}}
+
+// Check invalid code mixed with valid code
+
+[[msvc::constexpr]] int f4(int x) { return x > 1 ? 1 + f4(x / 2) : 0; } // expected-note {{non-constexpr 

[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-07-20 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt added inline comments.



Comment at: clang/include/clang/Basic/AttrDocs.td:3609-3611
+.. Note:: To use a ``[[msvc::constexpr]]`` function in a constant context,
+   one may want to create a ``constexpr`` function-wrapper and invoke the
+   ``[[msvc::constexpr]]`` function within a ``[[msvc::constexpr]] return`` 
statement.

RIscRIpt wrote:
> As per latest comments, maybe I should remove this note?
Removed.



Comment at: clang/include/clang/Basic/DiagnosticSemaKinds.td:2817
+  "[[msvc::constexpr]] has effect only on function definitions and return 
statements">,
+  InGroup;
 

> Given that the intended use case is for usage behind the scenes in the 
> standard library, I don't think we should be changing our diagnostic output 
> at all [there]

How about other places? Theoretically I could re-use existing warnings/errors 
with diagnostics saying `constexpr` instead of `[[msvc::constexpr]]`.



Comment at: clang/lib/AST/ExprConstant.cpp:5615-5627
+if (canEvalMSConstexpr || isMSConstexpr) {
+  // Diagnose invalid usage of [[msvc::constexpr]] function
+  bool isConstructor = isa(Definition);
+  if (canEvalMSConstexpr) { // !isMSConstexpr
+Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1)
+<< /*IsConstexpr*/ 0 << isConstructor << Definition;
+Info.Note(Definition->getLocation(), diag::note_declared_at);

RIscRIpt wrote:
> rsmith wrote:
> > Given that the intended use case is for usage behind the scenes in the 
> > standard library, I don't think we should be changing our diagnostic output 
> > at all here. If the library, as an implementation detail, marks a 
> > non-`constexpr` function as `[[msvc::constexpr]]`, we shouldn't tell the 
> > user to add `[[msvc::constexpr]]` to their code to allow it to be called, 
> > after all, the annotation is an implementation detail of the MS standard 
> > library.
> Sounds fair, I will adjust this code if I'm unable to convince you with my 
> new comments.
Removed, and made the check as a single `if` statement.



Comment at: clang/lib/AST/ExprConstant.cpp:9591-9594
+  bool IsMSConstexpr = Info.CurrentCall->CanEvalMSConstexpr &&
+   OperatorNew->hasAttr();
   if (OperatorNew->isReservedGlobalPlacementOperator() &&
+  (Info.CurrentCall->isStdFunction() || IsMSConstexpr) && !E->isArray()) {

rsmith wrote:
> RIscRIpt wrote:
> > rsmith wrote:
> > > Do we really need this change? Was our existing check of whether the 
> > > caller is in namespace `std` not sufficient for MS' standard library? I'd 
> > > strongly prefer not to have a documented, user-visible attribute that 
> > > gives permission to use placement new directly.
> > Yes, STL's `operator new` is defined in global namespace in [[ 
> > https://gist.github.com/RIscRIpt/9f0991f09f97eafc375fc73ea851a81b#file-vcruntime_new-h-L165
> >  | vcruntime_new.h ]] (and all includes of this file are made from global 
> > namespace).
> The existing code is checking whether the caller of `operator new` (eg, 
> `std::construct_at`) is in namespace `std`, not whether the `operator new` 
> itself is. Does MSVC's `construct_at` need this? It might if it uses a 
> placement new indirectly via a function in a different namespace, but it 
> seems likely to me that it doesn't.
Yes, sorry, my bad. This change is not needed, unless we want to support this 
attribute in user-code, which I guess we don't want to.

Removed.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-07-20 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt updated this revision to Diff 542436.
RIscRIpt marked 3 inline comments as done.
RIscRIpt added a comment.
Herald added a subscriber: MaskRay.

Addressed review comments and rebased onto main.

I have limited support of this attribute to `-fms-compatibility-version` 19.33+ 
(Visual Studio version, when this attribute first appeared).
Added definition of `_MSVC_CONSTEXPR_ATTRIBUTE`.

I am not sure whether and where to mention about adding support of this 
attribute in release notes.

Additionally, bumped `-fms-compatibility-version` to 19.33 in a separate 
commit. Although, I am not aware of all the consequences of this change.

Changed test files their names and locations.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

Files:
  clang/docs/ReleaseNotes.rst
  clang/docs/UsersManual.rst
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/test/Driver/cl-options.c


Index: clang/test/Driver/cl-options.c
===
--- clang/test/Driver/cl-options.c
+++ clang/test/Driver/cl-options.c
@@ -753,7 +753,7 @@
 
 // Validate that the default triple is used when run an empty tools dir is 
specified
 // RUN: %clang_cl -vctoolsdir "" -### -- %s 2>&1 | FileCheck %s --check-prefix 
VCTOOLSDIR
-// VCTOOLSDIR: "-triple" "{{[a-zA-Z0-9_-]*}}-pc-windows-msvc19.20.0"
+// VCTOOLSDIR: "-triple" "{{[a-zA-Z0-9_-]*}}-pc-windows-msvc19.33.0"
 
 // Validate that built-in include paths are based on the supplied path
 // RUN: %clang_cl --target=aarch64-pc-windows-msvc -vctoolsdir "/fake" 
-winsdkdir "/foo" -winsdkversion 10.0.12345.0 -### -- %s 2>&1 | FileCheck %s 
--check-prefix FAKEDIR
@@ -793,7 +793,7 @@
 
 // RUN: %clang_cl -vctoolsdir "" /arm64EC /c -### -- %s 2>&1 | FileCheck 
--check-prefix=ARM64EC %s 
 // ARM64EC-NOT: /arm64EC has been overridden by specified target
-// ARM64EC: "-triple" "arm64ec-pc-windows-msvc19.20.0"
+// ARM64EC: "-triple" "arm64ec-pc-windows-msvc19.33.0"
 
 // RUN: %clang_cl -vctoolsdir "" /arm64EC /c -target x86_64-pc-windows-msvc  
-### -- %s 2>&1 | FileCheck --check-prefix=ARM64EC_OVERRIDE %s
 // ARM64EC_OVERRIDE: warning: /arm64EC has been overridden by specified 
target: x86_64-pc-windows-msvc; option ignored
Index: clang/lib/Driver/ToolChains/MSVC.cpp
===
--- clang/lib/Driver/ToolChains/MSVC.cpp
+++ clang/lib/Driver/ToolChains/MSVC.cpp
@@ -787,11 +787,11 @@
   if (MSVT.empty() &&
   Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
IsWindowsMSVC)) {
-// -fms-compatibility-version=19.20 is default, aka 2019, 16.x
+// -fms-compatibility-version=19.33 is default, aka 2022, 17.3
 // NOTE: when changing this value, also update
 // clang/docs/CommandGuide/clang.rst and clang/docs/UsersManual.rst
 // accordingly.
-MSVT = VersionTuple(19, 20);
+MSVT = VersionTuple(19, 33);
   }
   return MSVT;
 }
Index: clang/docs/UsersManual.rst
===
--- clang/docs/UsersManual.rst
+++ clang/docs/UsersManual.rst
@@ -3314,8 +3314,8 @@
 
 For compatibility with existing code that compiles with MSVC, clang defines the
 ``_MSC_VER`` and ``_MSC_FULL_VER`` macros. When on Windows, these default to
-either the same value as the currently installed version of cl.exe, or ``1920``
-and ``19200`` (respectively). The ``-fms-compatibility-version=`` flag
+either the same value as the currently installed version of cl.exe, or ``1933``
+and ``19330`` (respectively). The ``-fms-compatibility-version=`` flag
 overrides these values.  It accepts a dotted version tuple, such as 
19.00.23506.
 Changing the MSVC compatibility version makes clang behave more like that
 version of MSVC. For example, ``-fms-compatibility-version=19`` will enable
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -262,6 +262,8 @@
 - Added ``__builtin_elementwise_nearbyint`` for floating point
   types. This allows access to ``llvm.nearbyint`` for arbitrary
   floating-point and vector of floating-point types.
+- The default value of `_MSC_VER` was raised from 1920 to 1933.
+  MSVC 19.33 added undocumented attribute ``[[msvc::constexpr]]``.
 
 New Compiler Flags
 --


Index: clang/test/Driver/cl-options.c
===
--- clang/test/Driver/cl-options.c
+++ clang/test/Driver/cl-options.c
@@ -753,7 +753,7 @@
 
 // Validate that the default triple is used when run an empty tools dir is specified
 // RUN: %clang_cl -vctoolsdir "" -### -- %s 2>&1 | FileCheck %s --check-prefix VCTOOLSDIR
-// VCTOOLSDIR: "-triple" "{{[a-zA-Z0-9_-]*}}-pc-windows-msvc19.20.0"
+// VCTOOLSDIR: "-triple" "{{[a-zA-Z0-9_-]*}}-pc-windows-msvc19.33.0"
 
 

[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-07-19 Thread Richard Smith - zygoloid via Phabricator via cfe-commits
rsmith added inline comments.



Comment at: clang/lib/AST/ExprConstant.cpp:9591-9594
+  bool IsMSConstexpr = Info.CurrentCall->CanEvalMSConstexpr &&
+   OperatorNew->hasAttr();
   if (OperatorNew->isReservedGlobalPlacementOperator() &&
+  (Info.CurrentCall->isStdFunction() || IsMSConstexpr) && !E->isArray()) {

RIscRIpt wrote:
> rsmith wrote:
> > Do we really need this change? Was our existing check of whether the caller 
> > is in namespace `std` not sufficient for MS' standard library? I'd strongly 
> > prefer not to have a documented, user-visible attribute that gives 
> > permission to use placement new directly.
> Yes, STL's `operator new` is defined in global namespace in [[ 
> https://gist.github.com/RIscRIpt/9f0991f09f97eafc375fc73ea851a81b#file-vcruntime_new-h-L165
>  | vcruntime_new.h ]] (and all includes of this file are made from global 
> namespace).
The existing code is checking whether the caller of `operator new` (eg, 
`std::construct_at`) is in namespace `std`, not whether the `operator new` 
itself is. Does MSVC's `construct_at` need this? It might if it uses a 
placement new indirectly via a function in a different namespace, but it seems 
likely to me that it doesn't.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-07-19 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt marked an inline comment as done.
RIscRIpt added a comment.

Thank you for your quick response.

> Given that the intended use case is for usage behind the scenes in the 
> standard library...



> I'd strongly prefer not to have a documented, user-visible attribute that 
> gives permission to use placement new directly.

I understand your concerns, and I cannot disagree. My reasoning behind the 
current implementation is to allow clang with `-fms-extensions` to parse and 
compile the same code with the `[[msvc::constexpr]]` attribute as MSVC can. 
Given that any user of the MSVC compiler can use this attribute and compile 
their code with it, it seems to me that clang-cl should also have this 
capability. However, we don't have any guarantees that Microsoft won't modify 
anything related to this attribute. I am not aware of the policies the Clang 
project has in place for such cases. Regardless, I will adjust the patch to 
comply as necessary.

TODO for my next patch: pre-define `_MSVC_CONSTEXPR_ATTRIBUTE`




Comment at: clang/include/clang/Basic/AttrDocs.td:3609-3611
+.. Note:: To use a ``[[msvc::constexpr]]`` function in a constant context,
+   one may want to create a ``constexpr`` function-wrapper and invoke the
+   ``[[msvc::constexpr]]`` function within a ``[[msvc::constexpr]] return`` 
statement.

As per latest comments, maybe I should remove this note?



Comment at: clang/lib/AST/ExprConstant.cpp:5615-5627
+if (canEvalMSConstexpr || isMSConstexpr) {
+  // Diagnose invalid usage of [[msvc::constexpr]] function
+  bool isConstructor = isa(Definition);
+  if (canEvalMSConstexpr) { // !isMSConstexpr
+Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1)
+<< /*IsConstexpr*/ 0 << isConstructor << Definition;
+Info.Note(Definition->getLocation(), diag::note_declared_at);

rsmith wrote:
> Given that the intended use case is for usage behind the scenes in the 
> standard library, I don't think we should be changing our diagnostic output 
> at all here. If the library, as an implementation detail, marks a 
> non-`constexpr` function as `[[msvc::constexpr]]`, we shouldn't tell the user 
> to add `[[msvc::constexpr]]` to their code to allow it to be called, after 
> all, the annotation is an implementation detail of the MS standard library.
Sounds fair, I will adjust this code if I'm unable to convince you with my new 
comments.



Comment at: clang/lib/AST/ExprConstant.cpp:9591-9594
+  bool IsMSConstexpr = Info.CurrentCall->CanEvalMSConstexpr &&
+   OperatorNew->hasAttr();
   if (OperatorNew->isReservedGlobalPlacementOperator() &&
+  (Info.CurrentCall->isStdFunction() || IsMSConstexpr) && !E->isArray()) {

rsmith wrote:
> Do we really need this change? Was our existing check of whether the caller 
> is in namespace `std` not sufficient for MS' standard library? I'd strongly 
> prefer not to have a documented, user-visible attribute that gives permission 
> to use placement new directly.
Yes, STL's `operator new` is defined in global namespace in [[ 
https://gist.github.com/RIscRIpt/9f0991f09f97eafc375fc73ea851a81b#file-vcruntime_new-h-L165
 | vcruntime_new.h ]] (and all includes of this file are made from global 
namespace).


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-07-18 Thread Richard Smith - zygoloid via Phabricator via cfe-commits
rsmith added inline comments.



Comment at: clang/lib/AST/ExprConstant.cpp:9591-9594
+  bool IsMSConstexpr = Info.CurrentCall->CanEvalMSConstexpr &&
+   OperatorNew->hasAttr();
   if (OperatorNew->isReservedGlobalPlacementOperator() &&
+  (Info.CurrentCall->isStdFunction() || IsMSConstexpr) && !E->isArray()) {

Do we really need this change? Was our existing check of whether the caller is 
in namespace `std` not sufficient for MS' standard library? I'd strongly prefer 
not to have a documented, user-visible attribute that gives permission to use 
placement new directly.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-07-18 Thread Richard Smith - zygoloid via Phabricator via cfe-commits
rsmith added inline comments.



Comment at: clang/lib/AST/ExprConstant.cpp:5615-5627
+if (canEvalMSConstexpr || isMSConstexpr) {
+  // Diagnose invalid usage of [[msvc::constexpr]] function
+  bool isConstructor = isa(Definition);
+  if (canEvalMSConstexpr) { // !isMSConstexpr
+Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1)
+<< /*IsConstexpr*/ 0 << isConstructor << Definition;
+Info.Note(Definition->getLocation(), diag::note_declared_at);

Given that the intended use case is for usage behind the scenes in the standard 
library, I don't think we should be changing our diagnostic output at all here. 
If the library, as an implementation detail, marks a non-`constexpr` function 
as `[[msvc::constexpr]]`, we shouldn't tell the user to add 
`[[msvc::constexpr]]` to their code to allow it to be called, after all, the 
annotation is an implementation detail of the MS standard library.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134475: [clang-cl] Add support for [[msvc::constexpr]] C++11 attribute

2023-07-18 Thread Richard Dzenis via Phabricator via cfe-commits
RIscRIpt added a comment.

According to git blame @rsmith has done lots of changes in 
`clang/lib/AST/ExprConstant.cpp`; added as reviewer.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134475

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits