xref: /llvm-project/clang/test/SemaCXX/ms-constexpr.cpp (revision b3e6ff331925dde24a4707452d657da0fdf7f588)
1 // RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -verify %s
2 
log2(int x)3 [[msvc::constexpr]] int log2(int x) { [[msvc::constexpr]] return x > 1 ? 1 + log2(x / 2) : 0; }
test_log2()4 constexpr bool test_log2() { [[msvc::constexpr]] return log2(32) == 5; }
5 static_assert(test_log2());
6 
get_value(int x)7 [[msvc::constexpr]] int get_value(int x)
8 {
9   switch (x)
10   {
11     case 42: return 1337;
12     default:
13              if (x < 0) [[msvc::constexpr]] return log2(-x);
14              else return x;
15   }
16 }
17 
test_complex_expr()18 constexpr bool test_complex_expr() {
19   [[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);
20 }
21 static_assert(test_complex_expr());
22 
get_constexpr_true()23 constexpr bool get_constexpr_true() { return true; }
get_msconstexpr_true()24 [[msvc::constexpr]] bool get_msconstexpr_true() { return get_constexpr_true(); }
test_get_msconstexpr_true()25 constexpr bool test_get_msconstexpr_true() { [[msvc::constexpr]] return get_msconstexpr_true(); }
26 static_assert(test_get_msconstexpr_true());
27 
28 // TODO (#72149): Add support for [[msvc::constexpr]] constructor; this code is valid for MSVC.
29 struct S2 {
S2S230     [[msvc::constexpr]] S2() {}
valueS231     [[msvc::constexpr]] bool value() { return true; }
checkS232     static constexpr bool check() { [[msvc::constexpr]] return S2{}.value(); } // expected-error {{constexpr function never produces a constant expression}} \
33                                                                                // expected-note {{non-literal type 'S2' cannot be used in a constant expression}} \
34                                                                                // expected-note {{non-literal type 'S2' cannot be used in a constant expression}}
35 };
36 static_assert(S2::check()); // expected-error {{static assertion expression is not an integral constant expression}} \
37                             // expected-note {{in call to 'check()'}}
38