|
| 1 | +// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++20 -verify %s |
| 2 | +// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -verify=cxx17 %s |
| 3 | +// RUN: %clang_cc1 -std=c++20 -verify=ref %s |
| 4 | +// RUN: %clang_cc1 -std=c++17 -verify=ref-cxx17 %s |
| 5 | + |
| 6 | +#define INT_MIN (~__INT_MAX__) |
| 7 | + |
| 8 | + |
| 9 | +namespace shifts { |
| 10 | + constexpr void test() { // ref-error {{constexpr function never produces a constant expression}} \ |
| 11 | + // ref-cxx17-error {{constexpr function never produces a constant expression}} |
| 12 | + |
| 13 | + char c; // cxx17-warning {{uninitialized variable}} \ |
| 14 | + // ref-cxx17-warning {{uninitialized variable}} |
| 15 | + |
| 16 | + c = 0 << 0; |
| 17 | + c = 0 << 1; |
| 18 | + c = 1 << 0; |
| 19 | + c = 1 << -0; |
| 20 | + c = 1 >> -0; |
| 21 | + c = 1 << -1; // expected-warning {{shift count is negative}} \ |
| 22 | + // cxx17-warning {{shift count is negative}} \ |
| 23 | + // ref-warning {{shift count is negative}} \ |
| 24 | + // ref-note {{negative shift count -1}} \ |
| 25 | + // ref-cxx17-warning {{shift count is negative}} \ |
| 26 | + // ref-cxx17-note {{negative shift count -1}} |
| 27 | + |
| 28 | + c = 1 >> -1; // expected-warning {{shift count is negative}} \ |
| 29 | + // cxx17-warning {{shift count is negative}} \ |
| 30 | + // ref-warning {{shift count is negative}} \ |
| 31 | + // ref-cxx17-warning {{shift count is negative}} |
| 32 | + c = 1 << (unsigned)-1; // expected-warning {{shift count >= width of type}} \ |
| 33 | + // FIXME: 'implicit conversion' warning missing in the new interpreter. \ |
| 34 | + // cxx17-warning {{shift count >= width of type}} \ |
| 35 | + // ref-warning {{shift count >= width of type}} \ |
| 36 | + // ref-warning {{implicit conversion}} \ |
| 37 | + // ref-cxx17-warning {{shift count >= width of type}} \ |
| 38 | + // ref-cxx17-warning {{implicit conversion}} |
| 39 | + c = 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}} \ |
| 40 | + // cxx17-warning {{shift count >= width of type}} \ |
| 41 | + // ref-warning {{shift count >= width of type}} \ |
| 42 | + // ref-cxx17-warning {{shift count >= width of type}} |
| 43 | + c = 1 << c; |
| 44 | + c <<= 0; |
| 45 | + c >>= 0; |
| 46 | + c <<= 1; |
| 47 | + c >>= 1; |
| 48 | + c <<= -1; // expected-warning {{shift count is negative}} \ |
| 49 | + // cxx17-warning {{shift count is negative}} \ |
| 50 | + // ref-warning {{shift count is negative}} \ |
| 51 | + // ref-cxx17-warning {{shift count is negative}} |
| 52 | + c >>= -1; // expected-warning {{shift count is negative}} \ |
| 53 | + // cxx17-warning {{shift count is negative}} \ |
| 54 | + // ref-warning {{shift count is negative}} \ |
| 55 | + // ref-cxx17-warning {{shift count is negative}} |
| 56 | + c <<= 999999; // expected-warning {{shift count >= width of type}} \ |
| 57 | + // cxx17-warning {{shift count >= width of type}} \ |
| 58 | + // ref-warning {{shift count >= width of type}} \ |
| 59 | + // ref-cxx17-warning {{shift count >= width of type}} |
| 60 | + c >>= 999999; // expected-warning {{shift count >= width of type}} \ |
| 61 | + // cxx17-warning {{shift count >= width of type}} \ |
| 62 | + // ref-warning {{shift count >= width of type}} \ |
| 63 | + // ref-cxx17-warning {{shift count >= width of type}} |
| 64 | + c <<= __CHAR_BIT__; // expected-warning {{shift count >= width of type}} \ |
| 65 | + // cxx17-warning {{shift count >= width of type}} \ |
| 66 | + // ref-warning {{shift count >= width of type}} \ |
| 67 | + // ref-cxx17-warning {{shift count >= width of type}} |
| 68 | + c >>= __CHAR_BIT__; // expected-warning {{shift count >= width of type}} \ |
| 69 | + // cxx17-warning {{shift count >= width of type}} \ |
| 70 | + // ref-warning {{shift count >= width of type}} \ |
| 71 | + // ref-cxx17-warning {{shift count >= width of type}} |
| 72 | + c <<= __CHAR_BIT__+1; // expected-warning {{shift count >= width of type}} \ |
| 73 | + // cxx17-warning {{shift count >= width of type}} \ |
| 74 | + // ref-warning {{shift count >= width of type}} \ |
| 75 | + // ref-cxx17-warning {{shift count >= width of type}} |
| 76 | + c >>= __CHAR_BIT__+1; // expected-warning {{shift count >= width of type}} \ |
| 77 | + // cxx17-warning {{shift count >= width of type}} \ |
| 78 | + // ref-warning {{shift count >= width of type}} \ |
| 79 | + // ref-cxx17-warning {{shift count >= width of type}} |
| 80 | + (void)((long)c << __CHAR_BIT__); |
| 81 | + |
| 82 | + int i; // cxx17-warning {{uninitialized variable}} \ |
| 83 | + // ref-cxx17-warning {{uninitialized variable}} |
| 84 | + i = 1 << (__INT_WIDTH__ - 2); |
| 85 | + i = 2 << (__INT_WIDTH__ - 1); // cxx17-warning {{bits to represent, but 'int' only has}} \ |
| 86 | + // ref-cxx17-warning {{bits to represent, but 'int' only has}} |
| 87 | + i = 1 << (__INT_WIDTH__ - 1); // cxx17-warning-not {{sets the sign bit of the shift expression}} |
| 88 | + i = -1 << (__INT_WIDTH__ - 1); // cxx17-warning {{shifting a negative signed value is undefined}} \ |
| 89 | + // ref-cxx17-warning {{shifting a negative signed value is undefined}} |
| 90 | + i = -1 << 0; // cxx17-warning {{shifting a negative signed value is undefined}} \ |
| 91 | + // ref-cxx17-warning {{shifting a negative signed value is undefined}} |
| 92 | + i = 0 << (__INT_WIDTH__ - 1); |
| 93 | + i = (char)1 << (__INT_WIDTH__ - 2); |
| 94 | + |
| 95 | + unsigned u; // cxx17-warning {{uninitialized variable}} \ |
| 96 | + // ref-cxx17-warning {{uninitialized variable}} |
| 97 | + u = 1U << (__INT_WIDTH__ - 1); |
| 98 | + u = 5U << (__INT_WIDTH__ - 1); |
| 99 | + |
| 100 | + long long int lli; // cxx17-warning {{uninitialized variable}} \ |
| 101 | + // ref-cxx17-warning {{uninitialized variable}} |
| 102 | + lli = INT_MIN << 2; // cxx17-warning {{shifting a negative signed value is undefined}} \ |
| 103 | + // ref-cxx17-warning {{shifting a negative signed value is undefined}} |
| 104 | + lli = 1LL << (sizeof(long long) * __CHAR_BIT__ - 2); |
| 105 | + } |
| 106 | + |
| 107 | + static_assert(1 << 4 == 16, ""); |
| 108 | + constexpr unsigned m = 2 >> 1; |
| 109 | + static_assert(m == 1, ""); |
| 110 | + constexpr unsigned char c = 0 << 8; |
| 111 | + static_assert(c == 0, ""); |
| 112 | + static_assert(true << 1, ""); |
| 113 | + static_assert(1 << (__INT_WIDTH__ +1) == 0, ""); // expected-error {{not an integral constant expression}} \ |
| 114 | + // expected-note {{>= width of type 'int'}} \ |
| 115 | + // cxx17-error {{not an integral constant expression}} \ |
| 116 | + // cxx17-note {{>= width of type 'int'}} \ |
| 117 | + // ref-error {{not an integral constant expression}} \ |
| 118 | + // ref-note {{>= width of type 'int'}} \ |
| 119 | + // ref-cxx17-error {{not an integral constant expression}} \ |
| 120 | + // ref-cxx17-note {{>= width of type 'int'}} |
| 121 | + |
| 122 | + constexpr int i1 = 1 << -1; // expected-error {{must be initialized by a constant expression}} \ |
| 123 | + // expected-note {{negative shift count -1}} \ |
| 124 | + // cxx17-error {{must be initialized by a constant expression}} \ |
| 125 | + // cxx17-note {{negative shift count -1}} \ |
| 126 | + // ref-error {{must be initialized by a constant expression}} \ |
| 127 | + // ref-note {{negative shift count -1}} \ |
| 128 | + // ref-cxx17-error {{must be initialized by a constant expression}} \ |
| 129 | + // ref-cxx17-note {{negative shift count -1}} |
| 130 | + |
| 131 | + constexpr int i2 = 1 << (__INT_WIDTH__ + 1); // expected-error {{must be initialized by a constant expression}} \ |
| 132 | + // expected-note {{>= width of type}} \ |
| 133 | + // cxx17-error {{must be initialized by a constant expression}} \ |
| 134 | + // cxx17-note {{>= width of type}} \ |
| 135 | + // ref-error {{must be initialized by a constant expression}} \ |
| 136 | + // ref-note {{>= width of type}} \ |
| 137 | + // ref-cxx17-error {{must be initialized by a constant expression}} \ |
| 138 | + // ref-cxx17-note {{>= width of type}} |
| 139 | + |
| 140 | + constexpr char c2 = 1; |
| 141 | + constexpr int i3 = c2 << (__CHAR_BIT__ + 1); // Not ill-formed |
| 142 | +}; |
0 commit comments