Clang Project

clang_source_code/test/Sema/tautological-constant-compare.c
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 -Wextra -Wno-sign-compare -verify %s
6// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wextra -Wno-sign-compare -verify -x c++ %s
7// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wall -verify %s
8// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wall -verify -x c++ %s
9// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -verify %s
10// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -verify -x c++ %s
11
12int value(void);
13
14#define macro(val) val
15
16#ifdef __cplusplus
17template<typename T>
18void TFunc() {
19  // Make sure that we do warn for normal variables in template functions !
20  unsigned char c = value();
21#ifdef TEST
22  if (c > 255) // expected-warning {{comparison 'unsigned char' > 255 is always false}}
23      return;
24#else
25  if (c > 255)
26      return;
27#endif
28
29  if (c > macro(255))
30      return;
31
32  T v = value();
33  if (v > 255)
34      return;
35  if (v > 32767)
36      return;
37}
38#endif
39
40int main()
41{
42#ifdef __cplusplus
43  TFunc<unsigned char>();
44  TFunc<signed short>();
45#endif
46
47  short s = value();
48
49#ifdef TEST
50  if (s == 32767)
51      return 0;
52  if (s != 32767)
53      return 0;
54  if (s < 32767)
55      return 0;
56  if (s <= 32767) // expected-warning {{comparison 'short' <= 32767 is always true}}
57      return 0;
58  if (s > 32767) // expected-warning {{comparison 'short' > 32767 is always false}}
59      return 0;
60  if (s >= 32767)
61      return 0;
62
63  if (32767 == s)
64      return 0;
65  if (32767 != s)
66      return 0;
67  if (32767 < s) // expected-warning {{comparison 32767 < 'short' is always false}}
68      return 0;
69  if (32767 <= s)
70      return 0;
71  if (32767 > s)
72      return 0;
73  if (32767 >= s) // expected-warning {{comparison 32767 >= 'short' is always true}}
74      return 0;
75
76  // FIXME: assumes two's complement
77  if (s == -32768)
78      return 0;
79  if (s != -32768)
80      return 0;
81  if (s < -32768) // expected-warning {{comparison 'short' < -32768 is always false}}
82      return 0;
83  if (s <= -32768)
84      return 0;
85  if (s > -32768)
86      return 0;
87  if (s >= -32768) // expected-warning {{comparison 'short' >= -32768 is always true}}
88      return 0;
89
90  if (-32768 == s)
91      return 0;
92  if (-32768 != s)
93      return 0;
94  if (-32768 < s)
95      return 0;
96  if (-32768 <= s) // expected-warning {{comparison -32768 <= 'short' is always true}}
97      return 0;
98  if (-32768 > s) // expected-warning {{comparison -32768 > 'short' is always false}}
99      return 0;
100  if (-32768 >= s)
101      return 0;
102
103  // Note: both sides are promoted to unsigned long prior to the comparison, so
104  // it is perfectly possible for a short to compare greater than 32767UL.
105  if (s == 32767UL)
106      return 0;
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
118  if (32767UL == s)
119      return 0;
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
131  enum { ULONG_MAX = (2UL * (unsigned long)__LONG_MAX__ + 1UL) };
132  if (s == 2UL * (unsigned long)__LONG_MAX__ + 1UL)
133      return 0;
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) // expected-warning-re {{comparison 'short' <= {{.*}} is always true}}
139      return 0;
140  if (s > 2UL * (unsigned long)__LONG_MAX__ + 1UL) // expected-warning-re {{comparison 'short' > {{.*}} is always false}}
141      return 0;
142  if (s >= 2UL * (unsigned long)__LONG_MAX__ + 1UL)
143      return 0;
144
145  if (2UL * (unsigned long)__LONG_MAX__ + 1UL == s)
146      return 0;
147  if (2UL * (unsigned long)__LONG_MAX__ + 1UL != s)
148      return 0;
149  if (2UL * (unsigned long)__LONG_MAX__ + 1UL < s) // expected-warning-re {{comparison {{.*}} < 'short' is always false}}
150      return 0;
151  if (2UL * (unsigned long)__LONG_MAX__ + 1UL <= s)
152      return 0;
153  if (2UL * (unsigned long)__LONG_MAX__ + 1UL > s)
154      return 0;
155  if (2UL * (unsigned long)__LONG_MAX__ + 1UL >= s) // expected-warning-re {{comparison {{.*}} >= 'short' is always true}}
156      return 0;
157
158  // FIXME: assumes two's complement
159  if (s == -32768L)
160      return 0;
161  if (s != -32768L)
162      return 0;
163  if (s < -32768L) // expected-warning {{comparison 'short' < -32768 is always false}}
164      return 0;
165  if (s <= -32768L)
166      return 0;
167  if (s > -32768L)
168      return 0;
169  if (s >= -32768L) // expected-warning {{comparison 'short' >= -32768 is always true}}
170      return 0;
171
172  if (-32768L == s)
173      return 0;
174  if (-32768L != s)
175      return 0;
176  if (-32768L < s)
177      return 0;
178  if (-32768L <= s) // expected-warning {{comparison -32768 <= 'short' is always true}}
179      return 0;
180  if (-32768L > s) // expected-warning {{comparison -32768 > 'short' is always false}}
181      return 0;
182  if (-32768L >= s)
183      return 0;
184#else
185  // expected-no-diagnostics
186  if (s == 32767)
187    return 0;
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
199  if (32767 == s)
200    return 0;
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
212  // FIXME: assumes two's complement
213  if (s == -32768)
214    return 0;
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
226  if (-32768 == s)
227    return 0;
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
239  if (s == 32767UL)
240    return 0;
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
252  if (32767UL == s)
253    return 0;
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
265  // FIXME: assumes two's complement
266  if (s == -32768L)
267    return 0;
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
279  if (-32768L == s)
280    return 0;
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#endif
292
293  if (s == 0)
294    return 0;
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
306  if (0 == s)
307    return 0;
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
319  unsigned short us = value();
320
321#ifdef TEST
322  if (us == 65535)
323      return 0;
324  if (us != 65535)
325      return 0;
326  if (us < 65535)
327      return 0;
328  if (us <= 65535) // expected-warning {{comparison 'unsigned short' <= 65535 is always true}}
329      return 0;
330  if (us > 65535) // expected-warning {{comparison 'unsigned short' > 65535 is always false}}
331      return 0;
332  if (us >= 65535)
333      return 0;
334
335  if (65535 == us)
336      return 0;
337  if (65535 != us)
338      return 0;
339  if (65535 < us) // expected-warning {{comparison 65535 < 'unsigned short' is always false}}
340      return 0;
341  if (65535 <= us)
342      return 0;
343  if (65535 > us)
344      return 0;
345  if (65535 >= us) // expected-warning {{comparison 65535 >= 'unsigned short' is always true}}
346      return 0;
347
348  if (us == 65535UL)
349      return 0;
350  if (us != 65535UL)
351      return 0;
352  if (us < 65535UL)
353      return 0;
354  if (us <= 65535UL) // expected-warning {{comparison 'unsigned short' <= 65535 is always true}}
355      return 0;
356  if (us > 65535UL) // expected-warning {{comparison 'unsigned short' > 65535 is always false}}
357      return 0;
358  if (us >= 65535UL)
359      return 0;
360
361  if (65535UL == us)
362      return 0;
363  if (65535UL != us)
364      return 0;
365  if (65535UL < us) // expected-warning {{comparison 65535 < 'unsigned short' is always false}}
366      return 0;
367  if (65535UL <= us)
368      return 0;
369  if (65535UL > us)
370      return 0;
371  if (65535UL >= us) // expected-warning {{comparison 65535 >= 'unsigned short' is always true}}
372      return 0;
373#else
374  // expected-no-diagnostics
375  if (us == 65535)
376      return 0;
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
388  if (65535 == us)
389      return 0;
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
401  if (us == 65535UL)
402      return 0;
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
414  if (65535UL == us)
415      return 0;
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#endif
427
428  if (us == 32767)
429    return 0;
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
441  if (32767 == us)
442    return 0;
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
454  if (us == 32767UL)
455    return 0;
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
467  if (32767UL == us)
468    return 0;
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
480#if __SIZEOF_INT128__
481  __int128 i128 = value();
482  if (i128 == -1) // used to crash
483      return 0;
484#endif
485
486
487  enum E {
488  yes,
489  no,
490  maybe
491  };
492  enum E e = (enum E)value();
493
494  if (e == yes)
495      return 0;
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
507  if (yes == e)
508      return 0;
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
520  if (e == maybe)
521      return 0;
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
533  if (maybe == e)
534      return 0;
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
546  // For the time being, use the declared type of bit-fields rather than their
547  // length when determining whether a value is in-range.
548  // FIXME: Reconsider this.
549  struct A {
550    int a : 3;
551    unsigned b : 3;
552    long c : 3;
553    unsigned long d : 3;
554  } a;
555  if (a.a < 3) {}
556  if (a.a < 4) {}
557  if (a.b < 7) {}
558  if (a.b < 8) {}
559  if (a.c < 3) {}
560  if (a.c < 4) {}
561  if (a.d < 7) {}
562  if (a.d < 8) {}
563
564  return 1;
565}
566