|
| 1 | +// RUN: %clang_cc1 -verify=ALL_NORMAL,NORMAL14,BOTH14,ALL_PRE20,ALLNORMAL,NORMAL_PRE20,ALL -std=c++14 %s -fcolor-diagnostics |
| 2 | +// RUN: %clang_cc1 -verify=IMPLICIT14,IMPLICIT_PRE20,BOTH14,ALL_PRE20,ALLIMPLICIT,ALL -fimplicit-constexpr -std=c++14 %s -fcolor-diagnostics |
| 3 | + |
| 4 | +// RUN: %clang_cc1 -verify=ALL_NORMAL,NORMAL17,BOTH17,ALL_PRE20,ALLNORMAL,NORMAL_PRE20,ALL -std=c++17 %s -fcolor-diagnostics |
| 5 | +// RUN: %clang_cc1 -verify=IMPLICIT17,IMPLICIT_PRE20,BOTH17,ALL_PRE20,ALLIMPLICIT,ALL -fimplicit-constexpr -std=c++17 %s -fcolor-diagnostics |
| 6 | + |
| 7 | +// RUN: %clang_cc1 -verify=ALL_NORMAL,NORMAL20,BOTH20,ALLNORMAL,ALL -std=c++20 %s -fcolor-diagnostics |
| 8 | +// RUN: %clang_cc1 -verify=IMPLICIT20,BOTH20,ALLIMPLICIT,ALL -fimplicit-constexpr -std=c++20 %s -fcolor-diagnostics |
| 9 | + |
| 10 | +// RUN: %clang_cc1 -verify=ALL_NORMAL,NORMAL23,BOTH23,ALLNORMAL,ALL -std=c++23 %s -fcolor-diagnostics |
| 11 | +// RUN: %clang_cc1 -verify=IMPLICIT23,BOTH23,ALLIMPLICIT,ALL -fimplicit-constexpr -std=c++23 %s -fcolor-diagnostics |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +// ============================================= |
| 17 | +// 1) simple uninlined function |
| 18 | + |
| 19 | +bool noinline_fnc() { |
| 20 | +// ALL-note@-1 {{declared here}} |
| 21 | + return true; |
| 22 | +} |
| 23 | + |
| 24 | +constexpr bool result_noinline_fnc = noinline_fnc(); |
| 25 | +// ALL-error@-1 {{constexpr variable 'result_noinline_fnc' must be initialized by a constant expression}} |
| 26 | +// ALLNORMAL-note@-2 {{non-constexpr function 'noinline_fnc' cannot be used in a constant expression}} |
| 27 | +// ALLIMPLICIT-note@-3 {{non-inline function 'noinline_fnc' is not implicitly constexpr}} |
| 28 | + |
| 29 | + |
| 30 | +// ============================================= |
| 31 | +// 2) simple inlined function |
| 32 | + |
| 33 | +inline bool inline_fnc() { |
| 34 | +// ALLNORMAL-note@-1 {{declared here}} |
| 35 | + return true; |
| 36 | +} |
| 37 | + |
| 38 | +constexpr bool result_inline_fnc = inline_fnc(); |
| 39 | +// ALLNORMAL-error@-1 {{constexpr variable 'result_inline_fnc' must be initialized by a constant expression}} |
| 40 | +// ALLNORMAL-note@-2 {{non-constexpr function 'inline_fnc' cannot be used in a constant expression}} |
| 41 | + |
| 42 | + |
| 43 | +// ============================================= |
| 44 | +// 3) undefined uninlined function |
| 45 | + |
| 46 | +bool noinline_undefined_fnc(); |
| 47 | +// ALL-note@-1 {{declared here}} |
| 48 | + |
| 49 | +constexpr bool result_noinline_undefined_fnc = noinline_undefined_fnc(); |
| 50 | +// ALL-error@-1 {{constexpr variable 'result_noinline_undefined_fnc' must be initialized by a constant expression}} |
| 51 | +// ALLNORMAL-note@-2 {{non-constexpr function 'noinline_undefined_fnc' cannot be used in a constant expression}} |
| 52 | +// ALLIMPLICIT-note@-3 {{undefined function 'noinline_undefined_fnc' cannot be used in a constant expression}} |
| 53 | + |
| 54 | + |
| 55 | +// ============================================= |
| 56 | +// 4) undefined inline function |
| 57 | + |
| 58 | +inline bool inline_undefined_fnc(); |
| 59 | +// ALL-note@-1 {{declared here}} |
| 60 | + |
| 61 | +constexpr bool result_inline_undefined_fnc = inline_undefined_fnc(); |
| 62 | +// ALL-error@-1 {{constexpr variable 'result_inline_undefined_fnc' must be initialized by a constant expression}} |
| 63 | +// ALLNORMAL-note@-2 {{non-constexpr function 'inline_undefined_fnc' cannot be used in a constant expression}} |
| 64 | +// ALLIMPLICIT-note@-3 {{undefined function 'inline_undefined_fnc' cannot be used in a constant expression}} |
| 65 | + |
| 66 | +// ============================================= |
| 67 | +// 5) lambda function |
| 68 | + |
| 69 | +auto lambda = [](int x) { return x > 0; }; |
| 70 | +// NORMAL14-note@-1 {{declared here}} |
| 71 | + |
| 72 | +constexpr bool result_lambda = lambda(10); |
| 73 | +// NORMAL14-error@-1 {{constexpr variable 'result_lambda' must be initialized by a constant expression}} |
| 74 | +// NORMAL14-note@-2 {{non-constexpr function 'operator()' cannot be used in a constant expression}} |
| 75 | + |
| 76 | + |
| 77 | +// ============================================= |
| 78 | +// 6) virtual functions |
| 79 | + |
| 80 | +struct type { |
| 81 | + virtual bool dispatch() const noexcept { |
| 82 | + return false; |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +struct child_of_type: type { |
| 87 | + bool dispatch() const noexcept override { |
| 88 | +// NORMAL20-note@-1 {{declared here}} |
| 89 | +// NORMAL23-note@-2 {{declared here}} |
| 90 | + return true; |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +constexpr bool result_virtual = static_cast<const type &>(child_of_type{}).dispatch(); |
| 95 | +// ALL_NORMAL-error@-1 {{constexpr variable 'result_virtual' must be initialized by a constant expression}} |
| 96 | +// NORMAL_PRE20-note@-2 {{cannot evaluate call to virtual function in a constant expression in C++ standards before C++20}} |
| 97 | +// IMPLICIT_PRE20-error@-3 {{constexpr variable 'result_virtual' must be initialized by a constant expression}} |
| 98 | +// IMPLICIT_PRE20-note@-4 {{cannot evaluate call to virtual function in a constant expression in C++ standards before C++20}} |
| 99 | +// NORMAL20-note@-5 {{non-constexpr function 'dispatch' cannot be used in a constant expression}} |
| 100 | +// NORMAL20-note@-6 {{declared here}} |
| 101 | +// NORMAL23-note@-7 {{non-constexpr function 'dispatch' cannot be used in a constant expression}} |
| 102 | +// NORMAL23-note@-8 {{declared here}} |
| 103 | + |
| 104 | + |
| 105 | +#if defined(__cpp_constexpr) && __cpp_constexpr >= 201907L |
| 106 | +static_assert(result_virtual == true, "virtual should work"); |
| 107 | +// ALL_NORMAL-error@-1 {{static assertion expression is not an integral constant expression}} |
| 108 | +// ALL_NORMAL-note@-2 {{initializer of 'result_virtual' is not a constant expression}} |
| 109 | +// IMPLICIT_PRE20-note@-3 {{initializer of 'result_virtual' is not a constant expression}} |
| 110 | +#endif |
| 111 | + |
| 112 | + |
0 commit comments