Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / AST / Interp / shifts.cpp
blobcf71e7145c274291b4b7f4d066d9ac617ca9c8cb
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
6 #define INT_MIN (~__INT_MAX__)
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 // expected-error {{constexpr function never produces a constant expression}} \
13 // cxx17-error {{constexpr function never produces a constant expression}} \
15 char c; // cxx17-warning {{uninitialized variable}} \
16 // ref-cxx17-warning {{uninitialized variable}}
18 c = 0 << 0;
19 c = 0 << 1;
20 c = 1 << 0;
21 c = 1 << -0;
22 c = 1 >> -0;
23 c = 1 << -1; // expected-warning {{shift count is negative}} \
24 // expected-note {{negative shift count -1}} \
25 // cxx17-note {{negative shift count -1}} \
26 // cxx17-warning {{shift count is negative}} \
27 // ref-warning {{shift count is negative}} \
28 // ref-note {{negative shift count -1}} \
29 // ref-cxx17-warning {{shift count is negative}} \
30 // ref-cxx17-note {{negative shift count -1}}
32 c = 1 >> -1; // expected-warning {{shift count is negative}} \
33 // cxx17-warning {{shift count is negative}} \
34 // ref-warning {{shift count is negative}} \
35 // ref-cxx17-warning {{shift count is negative}}
36 c = 1 << (unsigned)-1; // expected-warning {{shift count >= width of type}} \
37 // FIXME: 'implicit conversion' warning missing in the new interpreter. \
38 // cxx17-warning {{shift count >= width of type}} \
39 // ref-warning {{shift count >= width of type}} \
40 // ref-warning {{implicit conversion}} \
41 // ref-cxx17-warning {{shift count >= width of type}} \
42 // ref-cxx17-warning {{implicit conversion}}
43 c = 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}} \
44 // cxx17-warning {{shift count >= width of type}} \
45 // ref-warning {{shift count >= width of type}} \
46 // ref-cxx17-warning {{shift count >= width of type}}
47 c = 1 << c;
48 c <<= 0;
49 c >>= 0;
50 c <<= 1;
51 c >>= 1;
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 >>= -1; // expected-warning {{shift count is negative}} \
57 // cxx17-warning {{shift count is negative}} \
58 // ref-warning {{shift count is negative}} \
59 // ref-cxx17-warning {{shift count is negative}}
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 >>= 999999; // 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__; // 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 c >>= __CHAR_BIT__+1; // expected-warning {{shift count >= width of type}} \
81 // cxx17-warning {{shift count >= width of type}} \
82 // ref-warning {{shift count >= width of type}} \
83 // ref-cxx17-warning {{shift count >= width of type}}
84 (void)((long)c << __CHAR_BIT__);
86 int i; // cxx17-warning {{uninitialized variable}} \
87 // ref-cxx17-warning {{uninitialized variable}}
88 i = 1 << (__INT_WIDTH__ - 2);
89 i = 2 << (__INT_WIDTH__ - 1); // cxx17-warning {{bits to represent, but 'int' only has}} \
90 // ref-cxx17-warning {{bits to represent, but 'int' only has}}
91 i = 1 << (__INT_WIDTH__ - 1); // cxx17-warning-not {{sets the sign bit of the shift expression}}
92 i = -1 << (__INT_WIDTH__ - 1); // cxx17-warning {{shifting a negative signed value is undefined}} \
93 // ref-cxx17-warning {{shifting a negative signed value is undefined}}
94 i = -1 << 0; // cxx17-warning {{shifting a negative signed value is undefined}} \
95 // ref-cxx17-warning {{shifting a negative signed value is undefined}}
96 i = 0 << (__INT_WIDTH__ - 1);
97 i = (char)1 << (__INT_WIDTH__ - 2);
99 unsigned u; // cxx17-warning {{uninitialized variable}} \
100 // ref-cxx17-warning {{uninitialized variable}}
101 u = 1U << (__INT_WIDTH__ - 1);
102 u = 5U << (__INT_WIDTH__ - 1);
104 long long int lli; // cxx17-warning {{uninitialized variable}} \
105 // ref-cxx17-warning {{uninitialized variable}}
106 lli = INT_MIN << 2; // cxx17-warning {{shifting a negative signed value is undefined}} \
107 // ref-cxx17-warning {{shifting a negative signed value is undefined}}
108 lli = 1LL << (sizeof(long long) * __CHAR_BIT__ - 2);
111 static_assert(1 << 4 == 16, "");
112 constexpr unsigned m = 2 >> 1;
113 static_assert(m == 1, "");
114 constexpr unsigned char c = 0 << 8;
115 static_assert(c == 0, "");
116 static_assert(true << 1, "");
117 static_assert(1 << (__INT_WIDTH__ +1) == 0, ""); // expected-error {{not an integral constant expression}} \
118 // expected-note {{>= width of type 'int'}} \
119 // cxx17-error {{not an integral constant expression}} \
120 // cxx17-note {{>= width of type 'int'}} \
121 // ref-error {{not an integral constant expression}} \
122 // ref-note {{>= width of type 'int'}} \
123 // ref-cxx17-error {{not an integral constant expression}} \
124 // ref-cxx17-note {{>= width of type 'int'}}
126 constexpr int i1 = 1 << -1; // expected-error {{must be initialized by a constant expression}} \
127 // expected-note {{negative shift count -1}} \
128 // cxx17-error {{must be initialized by a constant expression}} \
129 // cxx17-note {{negative shift count -1}} \
130 // ref-error {{must be initialized by a constant expression}} \
131 // ref-note {{negative shift count -1}} \
132 // ref-cxx17-error {{must be initialized by a constant expression}} \
133 // ref-cxx17-note {{negative shift count -1}}
135 constexpr int i2 = 1 << (__INT_WIDTH__ + 1); // expected-error {{must be initialized by a constant expression}} \
136 // expected-note {{>= width of type}} \
137 // cxx17-error {{must be initialized by a constant expression}} \
138 // cxx17-note {{>= width of type}} \
139 // ref-error {{must be initialized by a constant expression}} \
140 // ref-note {{>= width of type}} \
141 // ref-cxx17-error {{must be initialized by a constant expression}} \
142 // ref-cxx17-note {{>= width of type}}
144 constexpr char c2 = 1;
145 constexpr int i3 = c2 << (__CHAR_BIT__ + 1); // Not ill-formed
147 /// The purpose of these few lines is to test that we can shift more bits
148 /// than an unsigned *of the host* has. There was a bug where we casted
149 /// to host-unsigned. However, we cannot query what a host-unsigned even is
150 /// here, so only test this on platforms where `sizeof(long long) > sizeof(unsigned)`.
151 constexpr long long int L = 1;
152 constexpr signed int R = (sizeof(unsigned) * 8) + 1;
153 constexpr decltype(L) M = (R > 32 && R < 64) ? L << R : 0;
154 constexpr decltype(L) M2 = (R > 32 && R < 64) ? L >> R : 0;
157 constexpr int signedShift() { // cxx17-error {{never produces a constant expression}} \
158 // ref-cxx17-error {{never produces a constant expression}}
159 return 1024 << 31; // cxx17-warning {{signed shift result}} \
160 // ref-cxx17-warning {{signed shift result}} \
161 // cxx17-note {{signed left shift discards bits}} \
162 // ref-cxx17-note {{signed left shift discards bits}}
165 constexpr int negativeShift() { // cxx17-error {{never produces a constant expression}} \
166 // ref-cxx17-error {{never produces a constant expression}}
167 return -1 << 2; // cxx17-warning {{shifting a negative signed value is undefined}} \
168 // ref-cxx17-warning {{shifting a negative signed value is undefined}} \
169 // cxx17-note {{left shift of negative value -1}} \
170 // ref-cxx17-note {{left shift of negative value -1}}
173 constexpr int foo(int a) {
174 return -a << 2; // cxx17-note {{left shift of negative value -10}} \
175 // ref-cxx17-note {{left shift of negative value -10}} \
176 // cxx17-note {{left shift of negative value -2}} \
177 // ref-cxx17-note {{left shift of negative value -2}}
179 static_assert(foo(10)); // cxx17-error {{not an integral constant expression}} \
180 // cxx17-note {{in call to 'foo(10)'}} \
181 // ref-cxx17-error {{not an integral constant expression}} \
182 // ref-cxx17-note {{in call to 'foo(10)'}}
184 constexpr int a = -2;
185 static_assert(foo(a));
186 static_assert(foo(-a)); // cxx17-error {{not an integral constant expression}} \
187 // cxx17-note {{in call to 'foo(2)'}} \
188 // ref-cxx17-error {{not an integral constant expression}} \
189 // ref-cxx17-note {{in call to 'foo(2)'}}