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