Debian Codegraph @

clang/test/AST/Interp/shifts.cpp

raw
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++20 -verify=expected,all %s
2 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -verify=cxx17,all %s
3 // RUN: %clang_cc1 -std=c++20 -verify=ref,all %s
4 // RUN: %clang_cc1 -std=c++17 -verify=ref-cxx17,all %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 // expected-error {{constexpr function never produces a constant expression}} \
13 // cxx17-error {{constexpr function never produces a constant expression}} \
14
15 char c; // cxx17-warning {{uninitialized variable}} \
16 // ref-cxx17-warning {{uninitialized variable}}
17
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}}
31
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 // expected-warning {{implicit conversion from 'int' to 'char' changes value from -2147483648 to 0}} \
38 // cxx17-warning {{shift count >= width of type}} \
39 // cxx17-warning {{implicit conversion from 'int' to 'char' changes value from -2147483648 to 0}} \
40 // ref-warning {{shift count >= width of type}} \
41 // ref-warning {{implicit conversion from 'int' to 'char' changes value from -2147483648 to 0}} \
42 // ref-cxx17-warning {{shift count >= width of type}} \
43 // ref-cxx17-warning {{implicit conversion from 'int' to 'char' changes value from -2147483648 to 0}}
44 c = 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}} \
45 // cxx17-warning {{shift count >= width of type}} \
46 // ref-warning {{shift count >= width of type}} \
47 // ref-cxx17-warning {{shift count >= width of type}}
48 c = 1 << c;
49 c <<= 0;
50 c >>= 0;
51 c <<= 1;
52 c >>= 1;
53 c <<= -1; // expected-warning {{shift count is negative}} \
54 // cxx17-warning {{shift count is negative}} \
55 // ref-warning {{shift count is negative}} \
56 // ref-cxx17-warning {{shift count is negative}}
57 c >>= -1; // expected-warning {{shift count is negative}} \
58 // cxx17-warning {{shift count is negative}} \
59 // ref-warning {{shift count is negative}} \
60 // ref-cxx17-warning {{shift count is negative}}
61 c <<= 999999; // expected-warning {{shift count >= width of type}} \
62 // cxx17-warning {{shift count >= width of type}} \
63 // ref-warning {{shift count >= width of type}} \
64 // ref-cxx17-warning {{shift count >= width of type}}
65 c >>= 999999; // expected-warning {{shift count >= width of type}} \
66 // cxx17-warning {{shift count >= width of type}} \
67 // ref-warning {{shift count >= width of type}} \
68 // ref-cxx17-warning {{shift count >= width of type}}
69 c <<= __CHAR_BIT__; // expected-warning {{shift count >= width of type}} \
70 // cxx17-warning {{shift count >= width of type}} \
71 // ref-warning {{shift count >= width of type}} \
72 // ref-cxx17-warning {{shift count >= width of type}}
73 c >>= __CHAR_BIT__; // expected-warning {{shift count >= width of type}} \
74 // cxx17-warning {{shift count >= width of type}} \
75 // ref-warning {{shift count >= width of type}} \
76 // ref-cxx17-warning {{shift count >= width of type}}
77 c <<= __CHAR_BIT__+1; // expected-warning {{shift count >= width of type}} \
78 // cxx17-warning {{shift count >= width of type}} \
79 // ref-warning {{shift count >= width of type}} \
80 // ref-cxx17-warning {{shift count >= width of type}}
81 c >>= __CHAR_BIT__+1; // expected-warning {{shift count >= width of type}} \
82 // cxx17-warning {{shift count >= width of type}} \
83 // ref-warning {{shift count >= width of type}} \
84 // ref-cxx17-warning {{shift count >= width of type}}
85 (void)((long)c << __CHAR_BIT__);
86
87 int i; // cxx17-warning {{uninitialized variable}} \
88 // ref-cxx17-warning {{uninitialized variable}}
89 i = 1 << (__INT_WIDTH__ - 2);
90 i = 2 << (__INT_WIDTH__ - 1); // cxx17-warning {{bits to represent, but 'int' only has}} \
91 // ref-cxx17-warning {{bits to represent, but 'int' only has}}
92 i = 1 << (__INT_WIDTH__ - 1); // cxx17-warning-not {{sets the sign bit of the shift expression}}
93 i = -1 << (__INT_WIDTH__ - 1); // cxx17-warning {{shifting a negative signed value is undefined}} \
94 // ref-cxx17-warning {{shifting a negative signed value is undefined}}
95 i = -1 << 0; // cxx17-warning {{shifting a negative signed value is undefined}} \
96 // ref-cxx17-warning {{shifting a negative signed value is undefined}}
97 i = 0 << (__INT_WIDTH__ - 1);
98 i = (char)1 << (__INT_WIDTH__ - 2);
99
100 unsigned u; // cxx17-warning {{uninitialized variable}} \
101 // ref-cxx17-warning {{uninitialized variable}}
102 u = 1U << (__INT_WIDTH__ - 1);
103 u = 5U << (__INT_WIDTH__ - 1);
104
105 long long int lli; // cxx17-warning {{uninitialized variable}} \
106 // ref-cxx17-warning {{uninitialized variable}}
107 lli = INT_MIN << 2; // cxx17-warning {{shifting a negative signed value is undefined}} \
108 // ref-cxx17-warning {{shifting a negative signed value is undefined}}
109 lli = 1LL << (sizeof(long long) * __CHAR_BIT__ - 2);
110 }
111
112 static_assert(1 << 4 == 16, "");
113 constexpr unsigned m = 2 >> 1;
114 static_assert(m == 1, "");
115 constexpr unsigned char c = 0 << 8;
116 static_assert(c == 0, "");
117 static_assert(true << 1, "");
118 static_assert(1 << (__INT_WIDTH__ +1) == 0, ""); // expected-error {{not an integral constant expression}} \
119 // expected-note {{>= width of type 'int'}} \
120 // cxx17-error {{not an integral constant expression}} \
121 // cxx17-note {{>= width of type 'int'}} \
122 // ref-error {{not an integral constant expression}} \
123 // ref-note {{>= width of type 'int'}} \
124 // ref-cxx17-error {{not an integral constant expression}} \
125 // ref-cxx17-note {{>= width of type 'int'}}
126
127 constexpr int i1 = 1 << -1; // expected-error {{must be initialized by a constant expression}} \
128 // expected-note {{negative shift count -1}} \
129 // cxx17-error {{must be initialized by a constant expression}} \
130 // cxx17-note {{negative shift count -1}} \
131 // ref-error {{must be initialized by a constant expression}} \
132 // ref-note {{negative shift count -1}} \
133 // ref-cxx17-error {{must be initialized by a constant expression}} \
134 // ref-cxx17-note {{negative shift count -1}}
135
136 constexpr int i2 = 1 << (__INT_WIDTH__ + 1); // expected-error {{must be initialized by a constant expression}} \
137 // expected-note {{>= width of type}} \
138 // cxx17-error {{must be initialized by a constant expression}} \
139 // cxx17-note {{>= width of type}} \
140 // ref-error {{must be initialized by a constant expression}} \
141 // ref-note {{>= width of type}} \
142 // ref-cxx17-error {{must be initialized by a constant expression}} \
143 // ref-cxx17-note {{>= width of type}}
144
145 constexpr char c2 = 1;
146 constexpr int i3 = c2 << (__CHAR_BIT__ + 1); // Not ill-formed
147
148 /// The purpose of these few lines is to test that we can shift more bits
149 /// than an unsigned *of the host* has. There was a bug where we casted
150 /// to host-unsigned. However, we cannot query what a host-unsigned even is
151 /// here, so only test this on platforms where `sizeof(long long) > sizeof(unsigned)`.
152 constexpr long long int L = 1;
153 constexpr signed int R = (sizeof(unsigned) * 8) + 1;
154 constexpr decltype(L) M = (R > 32 && R < 64) ? L << R : 0;
155 constexpr decltype(L) M2 = (R > 32 && R < 64) ? L >> R : 0;
156
157
158 constexpr int signedShift() { // cxx17-error {{never produces a constant expression}} \
159 // ref-cxx17-error {{never produces a constant expression}}
160 return 1024 << 31; // cxx17-warning {{signed shift result}} \
161 // ref-cxx17-warning {{signed shift result}} \
162 // cxx17-note {{signed left shift discards bits}} \
163 // ref-cxx17-note {{signed left shift discards bits}}
164 }
165
166 constexpr int negativeShift() { // cxx17-error {{never produces a constant expression}} \
167 // ref-cxx17-error {{never produces a constant expression}}
168 return -1 << 2; // cxx17-warning {{shifting a negative signed value is undefined}} \
169 // ref-cxx17-warning {{shifting a negative signed value is undefined}} \
170 // cxx17-note {{left shift of negative value -1}} \
171 // ref-cxx17-note {{left shift of negative value -1}}
172 }
173
174 constexpr int foo(int a) {
175 return -a << 2; // cxx17-note {{left shift of negative value -10}} \
176 // ref-cxx17-note {{left shift of negative value -10}} \
177 // cxx17-note {{left shift of negative value -2}} \
178 // ref-cxx17-note {{left shift of negative value -2}}
179 }
180 static_assert(foo(10)); // cxx17-error {{not an integral constant expression}} \
181 // cxx17-note {{in call to 'foo(10)'}} \
182 // ref-cxx17-error {{not an integral constant expression}} \
183 // ref-cxx17-note {{in call to 'foo(10)'}}
184
185 constexpr int a = -2;
186 static_assert(foo(a));
187 static_assert(foo(-a)); // cxx17-error {{not an integral constant expression}} \
188 // cxx17-note {{in call to 'foo(2)'}} \
189 // ref-cxx17-error {{not an integral constant expression}} \
190 // ref-cxx17-note {{in call to 'foo(2)'}}
191 };
192
193 namespace LongInt {
194 constexpr int f() {
195 int a = 1;
196 a <<= (long)0;
197 return 1;
198 }
199 static_assert(f() == 1, "");
200 };
201
202 enum shiftof {
203 X = (1<<-29), // all-error {{expression is not an integral constant expression}} \
204 // all-note {{negative shift count -29}}
205
206 X2 = (-1<<29), // cxx17-error {{expression is not an integral constant expression}} \
207 // cxx17-note {{left shift of negative value -1}} \
208 // ref-cxx17-error {{expression is not an integral constant expression}} \
209 // ref-cxx17-note {{left shift of negative value -1}}
210
211 X3 = (1<<32) // all-error {{expression is not an integral constant expression}} \
212 // all-note {{shift count 32 >= width of type 'int'}}
213 };