1 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtautological-constant-in-range-compare -DTEST -verify %s
2 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtautological-constant-in-range-compare -DTEST -verify -x c++ %s
3 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtautological-type-limit-compare -DTEST -verify %s
4 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtautological-type-limit-compare -DTEST -verify -x c++ %s
5 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtype-limits -DTEST -verify %s
6 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtype-limits -DTEST -verify -x c++ %s
7 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wextra -Wno-sign-compare -verify %s
8 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wextra -Wno-sign-compare -verify -x c++ %s
9 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wall -verify %s
10 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wall -verify -x c++ %s
11 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -verify %s
12 // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -verify -x c++ %s
13 
14 int value(void);
15 
16 #define macro(val) val
17 
18 #ifdef __cplusplus
19 template<typename T>
TFunc()20 void TFunc() {
21   // Make sure that we do warn for normal variables in template functions !
22   unsigned char c = value();
23 #ifdef TEST
24   if (c > 255) // expected-warning {{comparison 'unsigned char' > 255 is always false}}
25       return;
26 #else
27   if (c > 255)
28       return;
29 #endif
30 
31   if (c > macro(255))
32       return;
33 
34   T v = value();
35   if (v > 255)
36       return;
37   if (v > 32767)
38       return;
39 }
40 #endif
41 
main()42 int main()
43 {
44 #ifdef __cplusplus
45   TFunc<unsigned char>();
46   TFunc<signed short>();
47 #endif
48 
49   short s = value();
50 
51 #ifdef TEST
52   if (s == 32767)
53       return 0;
54   if (s != 32767)
55       return 0;
56   if (s < 32767)
57       return 0;
58   if (s <= 32767) // expected-warning {{comparison 'short' <= 32767 is always true}}
59       return 0;
60   if (s > 32767) // expected-warning {{comparison 'short' > 32767 is always false}}
61       return 0;
62   if (s >= 32767)
63       return 0;
64 
65   if (32767 == s)
66       return 0;
67   if (32767 != s)
68       return 0;
69   if (32767 < s) // expected-warning {{comparison 32767 < 'short' is always false}}
70       return 0;
71   if (32767 <= s)
72       return 0;
73   if (32767 > s)
74       return 0;
75   if (32767 >= s) // expected-warning {{comparison 32767 >= 'short' is always true}}
76       return 0;
77 
78   // FIXME: assumes two's complement
79   if (s == -32768)
80       return 0;
81   if (s != -32768)
82       return 0;
83   if (s < -32768) // expected-warning {{comparison 'short' < -32768 is always false}}
84       return 0;
85   if (s <= -32768)
86       return 0;
87   if (s > -32768)
88       return 0;
89   if (s >= -32768) // expected-warning {{comparison 'short' >= -32768 is always true}}
90       return 0;
91 
92   if (-32768 == s)
93       return 0;
94   if (-32768 != s)
95       return 0;
96   if (-32768 < s)
97       return 0;
98   if (-32768 <= s) // expected-warning {{comparison -32768 <= 'short' is always true}}
99       return 0;
100   if (-32768 > s) // expected-warning {{comparison -32768 > 'short' is always false}}
101       return 0;
102   if (-32768 >= s)
103       return 0;
104 
105   // Note: both sides are promoted to unsigned long prior to the comparison, so
106   // it is perfectly possible for a short to compare greater than 32767UL.
107   if (s == 32767UL)
108       return 0;
109   if (s != 32767UL)
110       return 0;
111   if (s < 32767UL)
112       return 0;
113   if (s <= 32767UL)
114       return 0;
115   if (s > 32767UL)
116       return 0;
117   if (s >= 32767UL)
118       return 0;
119 
120   if (32767UL == s)
121       return 0;
122   if (32767UL != s)
123       return 0;
124   if (32767UL < s)
125       return 0;
126   if (32767UL <= s)
127       return 0;
128   if (32767UL > s)
129       return 0;
130   if (32767UL >= s)
131       return 0;
132 
133   enum { ULONG_MAX = (2UL * (unsigned long)__LONG_MAX__ + 1UL) };
134   if (s == 2UL * (unsigned long)__LONG_MAX__ + 1UL)
135       return 0;
136   if (s != 2UL * (unsigned long)__LONG_MAX__ + 1UL)
137       return 0;
138   if (s < 2UL * (unsigned long)__LONG_MAX__ + 1UL)
139       return 0;
140   if (s <= 2UL * (unsigned long)__LONG_MAX__ + 1UL) // expected-warning-re {{comparison 'short' <= {{.*}} is always true}}
141       return 0;
142   if (s > 2UL * (unsigned long)__LONG_MAX__ + 1UL) // expected-warning-re {{comparison 'short' > {{.*}} is always false}}
143       return 0;
144   if (s >= 2UL * (unsigned long)__LONG_MAX__ + 1UL)
145       return 0;
146 
147   if (2UL * (unsigned long)__LONG_MAX__ + 1UL == s)
148       return 0;
149   if (2UL * (unsigned long)__LONG_MAX__ + 1UL != s)
150       return 0;
151   if (2UL * (unsigned long)__LONG_MAX__ + 1UL < s) // expected-warning-re {{comparison {{.*}} < 'short' is always false}}
152       return 0;
153   if (2UL * (unsigned long)__LONG_MAX__ + 1UL <= s)
154       return 0;
155   if (2UL * (unsigned long)__LONG_MAX__ + 1UL > s)
156       return 0;
157   if (2UL * (unsigned long)__LONG_MAX__ + 1UL >= s) // expected-warning-re {{comparison {{.*}} >= 'short' is always true}}
158       return 0;
159 
160   // FIXME: assumes two's complement
161   if (s == -32768L)
162       return 0;
163   if (s != -32768L)
164       return 0;
165   if (s < -32768L) // expected-warning {{comparison 'short' < -32768 is always false}}
166       return 0;
167   if (s <= -32768L)
168       return 0;
169   if (s > -32768L)
170       return 0;
171   if (s >= -32768L) // expected-warning {{comparison 'short' >= -32768 is always true}}
172       return 0;
173 
174   if (-32768L == s)
175       return 0;
176   if (-32768L != s)
177       return 0;
178   if (-32768L < s)
179       return 0;
180   if (-32768L <= s) // expected-warning {{comparison -32768 <= 'short' is always true}}
181       return 0;
182   if (-32768L > s) // expected-warning {{comparison -32768 > 'short' is always false}}
183       return 0;
184   if (-32768L >= s)
185       return 0;
186 #else
187   // expected-no-diagnostics
188   if (s == 32767)
189     return 0;
190   if (s != 32767)
191     return 0;
192   if (s < 32767)
193     return 0;
194   if (s <= 32767)
195     return 0;
196   if (s > 32767)
197     return 0;
198   if (s >= 32767)
199     return 0;
200 
201   if (32767 == s)
202     return 0;
203   if (32767 != s)
204     return 0;
205   if (32767 < s)
206     return 0;
207   if (32767 <= s)
208     return 0;
209   if (32767 > s)
210     return 0;
211   if (32767 >= s)
212     return 0;
213 
214   // FIXME: assumes two's complement
215   if (s == -32768)
216     return 0;
217   if (s != -32768)
218     return 0;
219   if (s < -32768)
220     return 0;
221   if (s <= -32768)
222     return 0;
223   if (s > -32768)
224     return 0;
225   if (s >= -32768)
226     return 0;
227 
228   if (-32768 == s)
229     return 0;
230   if (-32768 != s)
231     return 0;
232   if (-32768 < s)
233     return 0;
234   if (-32768 <= s)
235     return 0;
236   if (-32768 > s)
237     return 0;
238   if (-32768 >= s)
239     return 0;
240 
241   if (s == 32767UL)
242     return 0;
243   if (s != 32767UL)
244     return 0;
245   if (s < 32767UL)
246     return 0;
247   if (s <= 32767UL)
248     return 0;
249   if (s > 32767UL)
250     return 0;
251   if (s >= 32767UL)
252     return 0;
253 
254   if (32767UL == s)
255     return 0;
256   if (32767UL != s)
257     return 0;
258   if (32767UL < s)
259     return 0;
260   if (32767UL <= s)
261     return 0;
262   if (32767UL > s)
263     return 0;
264   if (32767UL >= s)
265     return 0;
266 
267   // FIXME: assumes two's complement
268   if (s == -32768L)
269     return 0;
270   if (s != -32768L)
271     return 0;
272   if (s < -32768L)
273     return 0;
274   if (s <= -32768L)
275     return 0;
276   if (s > -32768L)
277     return 0;
278   if (s >= -32768L)
279     return 0;
280 
281   if (-32768L == s)
282     return 0;
283   if (-32768L != s)
284     return 0;
285   if (-32768L < s)
286     return 0;
287   if (-32768L <= s)
288     return 0;
289   if (-32768L > s)
290     return 0;
291   if (-32768L >= s)
292     return 0;
293 #endif
294 
295   if (s == 0)
296     return 0;
297   if (s != 0)
298     return 0;
299   if (s < 0)
300     return 0;
301   if (s <= 0)
302     return 0;
303   if (s > 0)
304     return 0;
305   if (s >= 0)
306     return 0;
307 
308   if (0 == s)
309     return 0;
310   if (0 != s)
311     return 0;
312   if (0 < s)
313     return 0;
314   if (0 <= s)
315     return 0;
316   if (0 > s)
317     return 0;
318   if (0 >= s)
319     return 0;
320 
321   unsigned short us = value();
322 
323 #ifdef TEST
324   if (us == 65535)
325       return 0;
326   if (us != 65535)
327       return 0;
328   if (us < 65535)
329       return 0;
330   if (us <= 65535) // expected-warning {{comparison 'unsigned short' <= 65535 is always true}}
331       return 0;
332   if (us > 65535) // expected-warning {{comparison 'unsigned short' > 65535 is always false}}
333       return 0;
334   if (us >= 65535)
335       return 0;
336 
337   if (65535 == us)
338       return 0;
339   if (65535 != us)
340       return 0;
341   if (65535 < us) // expected-warning {{comparison 65535 < 'unsigned short' is always false}}
342       return 0;
343   if (65535 <= us)
344       return 0;
345   if (65535 > us)
346       return 0;
347   if (65535 >= us) // expected-warning {{comparison 65535 >= 'unsigned short' is always true}}
348       return 0;
349 
350   if (us == 65535UL)
351       return 0;
352   if (us != 65535UL)
353       return 0;
354   if (us < 65535UL)
355       return 0;
356   if (us <= 65535UL) // expected-warning {{comparison 'unsigned short' <= 65535 is always true}}
357       return 0;
358   if (us > 65535UL) // expected-warning {{comparison 'unsigned short' > 65535 is always false}}
359       return 0;
360   if (us >= 65535UL)
361       return 0;
362 
363   if (65535UL == us)
364       return 0;
365   if (65535UL != us)
366       return 0;
367   if (65535UL < us) // expected-warning {{comparison 65535 < 'unsigned short' is always false}}
368       return 0;
369   if (65535UL <= us)
370       return 0;
371   if (65535UL > us)
372       return 0;
373   if (65535UL >= us) // expected-warning {{comparison 65535 >= 'unsigned short' is always true}}
374       return 0;
375 #else
376   // expected-no-diagnostics
377   if (us == 65535)
378       return 0;
379   if (us != 65535)
380       return 0;
381   if (us < 65535)
382       return 0;
383   if (us <= 65535)
384       return 0;
385   if (us > 65535)
386       return 0;
387   if (us >= 65535)
388       return 0;
389 
390   if (65535 == us)
391       return 0;
392   if (65535 != us)
393       return 0;
394   if (65535 < us)
395       return 0;
396   if (65535 <= us)
397       return 0;
398   if (65535 > us)
399       return 0;
400   if (65535 >= us)
401       return 0;
402 
403   if (us == 65535UL)
404       return 0;
405   if (us != 65535UL)
406       return 0;
407   if (us < 65535UL)
408       return 0;
409   if (us <= 65535UL)
410       return 0;
411   if (us > 65535UL)
412       return 0;
413   if (us >= 65535UL)
414       return 0;
415 
416   if (65535UL == us)
417       return 0;
418   if (65535UL != us)
419       return 0;
420   if (65535UL < us)
421       return 0;
422   if (65535UL <= us)
423       return 0;
424   if (65535UL > us)
425       return 0;
426   if (65535UL >= us)
427       return 0;
428 #endif
429 
430   if (us == 32767)
431     return 0;
432   if (us != 32767)
433     return 0;
434   if (us < 32767)
435     return 0;
436   if (us <= 32767)
437     return 0;
438   if (us > 32767)
439     return 0;
440   if (us >= 32767)
441     return 0;
442 
443   if (32767 == us)
444     return 0;
445   if (32767 != us)
446     return 0;
447   if (32767 < us)
448     return 0;
449   if (32767 <= us)
450     return 0;
451   if (32767 > us)
452     return 0;
453   if (32767 >= us)
454     return 0;
455 
456   if (us == 32767UL)
457     return 0;
458   if (us != 32767UL)
459     return 0;
460   if (us < 32767UL)
461     return 0;
462   if (us <= 32767UL)
463     return 0;
464   if (us > 32767UL)
465     return 0;
466   if (us >= 32767UL)
467     return 0;
468 
469   if (32767UL == us)
470     return 0;
471   if (32767UL != us)
472     return 0;
473   if (32767UL < us)
474     return 0;
475   if (32767UL <= us)
476     return 0;
477   if (32767UL > us)
478     return 0;
479   if (32767UL >= us)
480     return 0;
481 
482 #if __SIZEOF_INT128__
483   __int128 i128 = value();
484   if (i128 == -1) // used to crash
485       return 0;
486 #endif
487 
488 
489   enum E {
490   yes,
491   no,
492   maybe
493   };
494   enum E e = (enum E)value();
495 
496   if (e == yes)
497       return 0;
498   if (e != yes)
499       return 0;
500   if (e < yes)
501       return 0;
502   if (e <= yes)
503       return 0;
504   if (e > yes)
505       return 0;
506   if (e >= yes)
507       return 0;
508 
509   if (yes == e)
510       return 0;
511   if (yes != e)
512       return 0;
513   if (yes < e)
514       return 0;
515   if (yes <= e)
516       return 0;
517   if (yes > e)
518       return 0;
519   if (yes >= e)
520       return 0;
521 
522   if (e == maybe)
523       return 0;
524   if (e != maybe)
525       return 0;
526   if (e < maybe)
527       return 0;
528   if (e <= maybe)
529       return 0;
530   if (e > maybe)
531       return 0;
532   if (e >= maybe)
533       return 0;
534 
535   if (maybe == e)
536       return 0;
537   if (maybe != e)
538       return 0;
539   if (maybe < e)
540       return 0;
541   if (maybe <= e)
542       return 0;
543   if (maybe > e)
544       return 0;
545   if (maybe >= e)
546       return 0;
547 
548   // For the time being, use the declared type of bit-fields rather than their
549   // length when determining whether a value is in-range.
550   // FIXME: Reconsider this.
551   struct A {
552     int a : 3;
553     unsigned b : 3;
554     long c : 3;
555     unsigned long d : 3;
556   } a;
557   if (a.a < 3) {}
558   if (a.a < 4) {}
559   if (a.b < 7) {}
560   if (a.b < 8) {}
561   if (a.c < 3) {}
562   if (a.c < 4) {}
563   if (a.d < 7) {}
564   if (a.d < 8) {}
565 
566   return 1;
567 }
568