blob: 5577b68b5ebd52201ea9ac7acb80f67a478bc737 [file] [log] [blame]
Roman Lebedevca1aaac2017-10-21 16:44:03 +00001// RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-linux-gnu -fsyntax-only -DUNSIGNED -verify %s
2// RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-win32 -fsyntax-only -DSIGNED -verify %s
3// RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-win32 -fsyntax-only -DSILENCE -Wno-tautological-unsigned-enum-zero-compare -verify %s
Roman Lebedev30d26082017-09-20 13:50:01 +00004
Roman Lebedev30d26082017-09-20 13:50:01 +00005int main() {
Richard Smith371e9e8a2017-12-06 03:00:51 +00006 // On Windows, all enumerations have a fixed underlying type, which is 'int'
7 // if not otherwise specified, so A is identical to C on Windows. Otherwise,
8 // we follow the C++ rules, which say that the only valid values of A are 0
9 // and 1.
Roman Lebedevca1aaac2017-10-21 16:44:03 +000010 enum A { A_foo = 0, A_bar, };
Roman Lebedev30d26082017-09-20 13:50:01 +000011 enum A a;
12
Roman Lebedevca1aaac2017-10-21 16:44:03 +000013 enum B : unsigned { B_foo = 0, B_bar, };
Roman Lebedev30d26082017-09-20 13:50:01 +000014 enum B b;
15
Roman Lebedevca1aaac2017-10-21 16:44:03 +000016 enum C : signed { C_foo = 0, C_bar, };
Roman Lebedev30d26082017-09-20 13:50:01 +000017 enum C c;
18
Roman Lebedevca1aaac2017-10-21 16:44:03 +000019#ifdef UNSIGNED
Roman Lebedev30d26082017-09-20 13:50:01 +000020 if (a < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
21 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000022 if (0 >= a)
23 return 0;
24 if (a > 0)
Roman Lebedev30d26082017-09-20 13:50:01 +000025 return 0;
26 if (0 <= a) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
27 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000028 if (a <= 0)
29 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +000030 if (0 > a) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
31 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000032 if (a >= 0) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
33 return 0;
34 if (0 < a)
35 return 0;
36
Roman Lebedev30d26082017-09-20 13:50:01 +000037 if (a < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
38 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000039 if (0U >= a)
40 return 0;
41 if (a > 0U)
Roman Lebedev30d26082017-09-20 13:50:01 +000042 return 0;
43 if (0U <= a) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
44 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000045 if (a <= 0U)
46 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +000047 if (0U > a) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
48 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000049 if (a >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
50 return 0;
51 if (0U < a)
52 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +000053
54 if (b < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
55 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000056 if (0 >= b)
57 return 0;
58 if (b > 0)
Roman Lebedev30d26082017-09-20 13:50:01 +000059 return 0;
60 if (0 <= b) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
61 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000062 if (b <= 0)
63 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +000064 if (0 > b) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
65 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +000066 if (b >= 0) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
67 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000068 if (0 < b)
Roman Lebedev30d26082017-09-20 13:50:01 +000069 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000070
Roman Lebedev30d26082017-09-20 13:50:01 +000071 if (b < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
72 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000073 if (0U >= b)
74 return 0;
75 if (b > 0U)
Roman Lebedev30d26082017-09-20 13:50:01 +000076 return 0;
77 if (0U <= b) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
78 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000079 if (b <= 0U)
80 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +000081 if (0U > b) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
82 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000083 if (b >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
Roman Lebedev30d26082017-09-20 13:50:01 +000084 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000085 if (0U < b)
Roman Lebedev30d26082017-09-20 13:50:01 +000086 return 0;
87
88 if (c < 0)
89 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +000090 if (0 >= c)
Roman Lebedevca1aaac2017-10-21 16:44:03 +000091 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +000092 if (c > 0)
Roman Lebedev30d26082017-09-20 13:50:01 +000093 return 0;
94 if (0 <= c)
95 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +000096 if (c <= 0)
Roman Lebedevca1aaac2017-10-21 16:44:03 +000097 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +000098 if (0 > c)
99 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000100 if (c >= 0)
101 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000102 if (0 < c)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000103 return 0;
104
Richard Smith371e9e8a2017-12-06 03:00:51 +0000105 // FIXME: These diagnostics are terrible. The issue here is that the signed
106 // enumeration value was promoted to an unsigned type.
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000107 if (c < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
108 return 0;
109 if (0U >= c)
110 return 0;
111 if (c > 0U)
112 return 0;
113 if (0U <= c) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
114 return 0;
115 if (c <= 0U)
116 return 0;
117 if (0U > c) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
118 return 0;
119 if (c >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
120 return 0;
121 if (0U < c)
122 return 0;
123#elif defined(SIGNED)
124 if (a < 0)
125 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000126 if (0 >= a)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000127 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000128 if (a > 0)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000129 return 0;
130 if (0 <= a)
131 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000132 if (a <= 0)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000133 return 0;
134 if (0 > a)
135 return 0;
136 if (a >= 0)
137 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000138 if (0 < a)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000139 return 0;
140
Richard Smith371e9e8a2017-12-06 03:00:51 +0000141 // FIXME: As above, the issue here is that the enumeration is promoted to
142 // unsigned.
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000143 if (a < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
144 return 0;
145 if (0U >= a)
146 return 0;
147 if (a > 0U)
148 return 0;
149 if (0U <= a) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
150 return 0;
151 if (a <= 0U)
152 return 0;
153 if (0U > a) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
154 return 0;
155 if (a >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
156 return 0;
157 if (0U < a)
158 return 0;
159
160 if (b < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
161 return 0;
162 if (0 >= b)
163 return 0;
164 if (b > 0)
165 return 0;
166 if (0 <= b) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
167 return 0;
168 if (b <= 0)
169 return 0;
170 if (0 > b) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
171 return 0;
172 if (b >= 0) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
173 return 0;
174 if (0 < b)
175 return 0;
176
177 if (b < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
178 return 0;
179 if (0U >= b)
180 return 0;
181 if (b > 0U)
182 return 0;
183 if (0U <= b) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
184 return 0;
185 if (b <= 0U)
186 return 0;
187 if (0U > b) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
188 return 0;
189 if (b >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
190 return 0;
191 if (0U < b)
192 return 0;
193
194 if (c < 0)
195 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000196 if (0 >= c)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000197 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000198 if (c > 0)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000199 return 0;
200 if (0 <= c)
201 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000202 if (c <= 0)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000203 return 0;
204 if (0 > c)
205 return 0;
206 if (c >= 0)
207 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000208 if (0 < c)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000209 return 0;
210
211 if (c < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
212 return 0;
213 if (0U >= c)
214 return 0;
215 if (c > 0U)
216 return 0;
217 if (0U <= c) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
218 return 0;
219 if (c <= 0U)
220 return 0;
221 if (0U > c) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
222 return 0;
223 if (c >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
224 return 0;
225 if (0U < c)
226 return 0;
227#else
Richard Smith371e9e8a2017-12-06 03:00:51 +0000228 // expected-no-diagnostics
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000229 if (a < 0)
230 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000231 if (0 >= a)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000232 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000233 if (a > 0)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000234 return 0;
235 if (0 <= a)
236 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000237 if (a <= 0)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000238 return 0;
239 if (0 > a)
240 return 0;
241 if (a >= 0)
242 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000243 if (0 < a)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000244 return 0;
245
246 if (a < 0U)
247 return 0;
248 if (0U >= a)
249 return 0;
250 if (a > 0U)
251 return 0;
252 if (0U <= a)
253 return 0;
254 if (a <= 0U)
255 return 0;
256 if (0U > a)
257 return 0;
258 if (a >= 0U)
259 return 0;
260 if (0U < a)
261 return 0;
262
263 if (b < 0)
264 return 0;
265 if (0 >= b)
266 return 0;
267 if (b > 0)
268 return 0;
269 if (0 <= b)
270 return 0;
271 if (b <= 0)
272 return 0;
273 if (0 > b)
274 return 0;
275 if (b >= 0)
276 return 0;
277 if (0 < b)
278 return 0;
279
280 if (b < 0U)
281 return 0;
282 if (0U >= b)
283 return 0;
284 if (b > 0U)
285 return 0;
286 if (0U <= b)
287 return 0;
288 if (b <= 0U)
289 return 0;
290 if (0U > b)
291 return 0;
292 if (b >= 0U)
293 return 0;
294 if (0U < b)
295 return 0;
296
297 if (c < 0)
298 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000299 if (0 >= c)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000300 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000301 if (c > 0)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000302 return 0;
303 if (0 <= c)
304 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000305 if (c <= 0)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000306 return 0;
307 if (0 > c)
308 return 0;
309 if (c >= 0)
310 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000311 if (0 < c)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000312 return 0;
313
Roman Lebedev30d26082017-09-20 13:50:01 +0000314 if (c < 0U)
315 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000316 if (0U >= c)
317 return 0;
318 if (c > 0U)
Roman Lebedev30d26082017-09-20 13:50:01 +0000319 return 0;
320 if (0U <= c)
321 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000322 if (c <= 0U)
323 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +0000324 if (0U > c)
325 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000326 if (c >= 0U)
327 return 0;
328 if (0U < c)
329 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +0000330#endif
331
332 return 1;
333}
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000334
335namespace crash_enum_zero_width {
336int test() {
337 enum A : unsigned {
338 A_foo = 0
339 };
340 enum A a;
341
342 // used to crash in llvm::APSInt::getMaxValue()
343#ifndef SILENCE
344 if (a < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
345#else
346 if (a > 0)
347#endif
348 return 0;
349
350 return 1;
351}
352} // namespace crash_enum_zero_width