673 lines · c
1// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtautological-constant-in-range-compare -DTEST=2 -verify %s2// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtautological-constant-in-range-compare -DTEST=2 -verify -x c++ %s3// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtautological-type-limit-compare -DTEST -verify %s4// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtautological-type-limit-compare -DTEST -verify -x c++ %s5// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtype-limits -DTEST -verify %s6// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wtype-limits -DTEST -verify -x c++ %s7// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wextra -Wno-sign-compare -verify=silent %s8// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wextra -Wno-sign-compare -verify=silent -x c++ %s9// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wall -verify=silent %s10// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -Wall -verify=silent -x c++ %s11// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -verify=silent %s12// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -verify=silent -x c++ %s13 14#ifndef TEST15// silent-no-diagnostics16#endif17 18int value(void);19 20#define macro(val) val21 22#ifdef __cplusplus23template<typename T>24void TFunc() {25 // Make sure that we do warn for normal variables in template functions !26 unsigned char c = value();27#ifdef TEST28 if (c > 255) // expected-warning {{comparison 'unsigned char' > 255 is always false}}29 return;30#else31 if (c > 255)32 return;33#endif34 35 if (c > macro(255))36 return;37 38 T v = value();39 if (v > 255)40 return;41 if (v > 32767)42 return;43}44#endif45 46int main(void)47{48#ifdef __cplusplus49 TFunc<unsigned char>();50 TFunc<signed short>();51#endif52 53 short s = value();54 55#ifdef TEST56 if (s == 32767)57 return 0;58 if (s != 32767)59 return 0;60 if (s < 32767)61 return 0;62 if (s <= 32767) // expected-warning {{comparison 'short' <= 32767 is always true}}63 return 0;64 if (s > 32767) // expected-warning {{comparison 'short' > 32767 is always false}}65 return 0;66 if (s >= 32767)67 return 0;68 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 false}}74 return 0;75 if (32767 <= s)76 return 0;77 if (32767 > s)78 return 0;79 if (32767 >= s) // expected-warning {{comparison 32767 >= 'short' is always true}}80 return 0;81 82 // FIXME: assumes two's complement83 if (s == -32768)84 return 0;85 if (s != -32768)86 return 0;87 if (s < -32768) // expected-warning {{comparison 'short' < -32768 is always false}}88 return 0;89 if (s <= -32768)90 return 0;91 if (s > -32768)92 return 0;93 if (s >= -32768) // expected-warning {{comparison 'short' >= -32768 is always true}}94 return 0;95 96 if (-32768 == s)97 return 0;98 if (-32768 != s)99 return 0;100 if (-32768 < s)101 return 0;102 if (-32768 <= s) // expected-warning {{comparison -32768 <= 'short' is always true}}103 return 0;104 if (-32768 > s) // expected-warning {{comparison -32768 > 'short' is always false}}105 return 0;106 if (-32768 >= s)107 return 0;108 109 // Note: both sides are promoted to unsigned long prior to the comparison, so110 // it is perfectly possible for a short to compare greater than 32767UL.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 if (s > 32767UL)120 return 0;121 if (s >= 32767UL)122 return 0;123 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 if (32767UL > s)133 return 0;134 if (32767UL >= s)135 return 0;136 137 enum { ULONG_MAX = (2UL * (unsigned long)__LONG_MAX__ + 1UL) };138 if (s == 2UL * (unsigned long)__LONG_MAX__ + 1UL)139 return 0;140 if (s != 2UL * (unsigned long)__LONG_MAX__ + 1UL)141 return 0;142 if (s < 2UL * (unsigned long)__LONG_MAX__ + 1UL)143 return 0;144 if (s <= 2UL * (unsigned long)__LONG_MAX__ + 1UL) // expected-warning-re {{comparison 'short' <= {{.*}} is always true}}145 return 0;146 if (s > 2UL * (unsigned long)__LONG_MAX__ + 1UL) // expected-warning-re {{comparison 'short' > {{.*}} is always false}}147 return 0;148 if (s >= 2UL * (unsigned long)__LONG_MAX__ + 1UL)149 return 0;150 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 false}}156 return 0;157 if (2UL * (unsigned long)__LONG_MAX__ + 1UL <= s)158 return 0;159 if (2UL * (unsigned long)__LONG_MAX__ + 1UL > s)160 return 0;161 if (2UL * (unsigned long)__LONG_MAX__ + 1UL >= s) // expected-warning-re {{comparison {{.*}} >= 'short' is always true}}162 return 0;163 164 // FIXME: assumes two's complement165 if (s == -32768L)166 return 0;167 if (s != -32768L)168 return 0;169 if (s < -32768L) // expected-warning {{comparison 'short' < -32768 is always false}}170 return 0;171 if (s <= -32768L)172 return 0;173 if (s > -32768L)174 return 0;175 if (s >= -32768L) // expected-warning {{comparison 'short' >= -32768 is always true}}176 return 0;177 178 if (-32768L == s)179 return 0;180 if (-32768L != s)181 return 0;182 if (-32768L < s)183 return 0;184 if (-32768L <= s) // expected-warning {{comparison -32768 <= 'short' is always true}}185 return 0;186 if (-32768L > s) // expected-warning {{comparison -32768 > 'short' is always false}}187 return 0;188 if (-32768L >= s)189 return 0;190#else191 if (s == 32767)192 return 0;193 if (s != 32767)194 return 0;195 if (s < 32767)196 return 0;197 if (s <= 32767)198 return 0;199 if (s > 32767)200 return 0;201 if (s >= 32767)202 return 0;203 204 if (32767 == s)205 return 0;206 if (32767 != s)207 return 0;208 if (32767 < s)209 return 0;210 if (32767 <= s)211 return 0;212 if (32767 > s)213 return 0;214 if (32767 >= s)215 return 0;216 217 // FIXME: assumes two's complement218 if (s == -32768)219 return 0;220 if (s != -32768)221 return 0;222 if (s < -32768)223 return 0;224 if (s <= -32768)225 return 0;226 if (s > -32768)227 return 0;228 if (s >= -32768)229 return 0;230 231 if (-32768 == s)232 return 0;233 if (-32768 != s)234 return 0;235 if (-32768 < s)236 return 0;237 if (-32768 <= s)238 return 0;239 if (-32768 > s)240 return 0;241 if (-32768 >= s)242 return 0;243 244 if (s == 32767UL)245 return 0;246 if (s != 32767UL)247 return 0;248 if (s < 32767UL)249 return 0;250 if (s <= 32767UL)251 return 0;252 if (s > 32767UL)253 return 0;254 if (s >= 32767UL)255 return 0;256 257 if (32767UL == s)258 return 0;259 if (32767UL != s)260 return 0;261 if (32767UL < s)262 return 0;263 if (32767UL <= s)264 return 0;265 if (32767UL > s)266 return 0;267 if (32767UL >= s)268 return 0;269 270 // FIXME: assumes two's complement271 if (s == -32768L)272 return 0;273 if (s != -32768L)274 return 0;275 if (s < -32768L)276 return 0;277 if (s <= -32768L)278 return 0;279 if (s > -32768L)280 return 0;281 if (s >= -32768L)282 return 0;283 284 if (-32768L == s)285 return 0;286 if (-32768L != s)287 return 0;288 if (-32768L < s)289 return 0;290 if (-32768L <= s)291 return 0;292 if (-32768L > s)293 return 0;294 if (-32768L >= s)295 return 0;296#endif297 298 if (s == 0)299 return 0;300 if (s != 0)301 return 0;302 if (s < 0)303 return 0;304 if (s <= 0)305 return 0;306 if (s > 0)307 return 0;308 if (s >= 0)309 return 0;310 311 if (0 == s)312 return 0;313 if (0 != s)314 return 0;315 if (0 < s)316 return 0;317 if (0 <= s)318 return 0;319 if (0 > s)320 return 0;321 if (0 >= s)322 return 0;323 324 unsigned short us = value();325 326#ifdef TEST327 if (us == 65535)328 return 0;329 if (us != 65535)330 return 0;331 if (us < 65535)332 return 0;333 if (us <= 65535) // expected-warning {{comparison 'unsigned short' <= 65535 is always true}}334 return 0;335 if (us > 65535) // expected-warning {{comparison 'unsigned short' > 65535 is always false}}336 return 0;337 if (us >= 65535)338 return 0;339 340 if (65535 == us)341 return 0;342 if (65535 != us)343 return 0;344 if (65535 < us) // expected-warning {{comparison 65535 < 'unsigned short' is always false}}345 return 0;346 if (65535 <= us)347 return 0;348 if (65535 > us)349 return 0;350 if (65535 >= us) // expected-warning {{comparison 65535 >= 'unsigned short' is always true}}351 return 0;352 353 if (us == 65535UL)354 return 0;355 if (us != 65535UL)356 return 0;357 if (us < 65535UL)358 return 0;359 if (us <= 65535UL) // expected-warning {{comparison 'unsigned short' <= 65535 is always true}}360 return 0;361 if (us > 65535UL) // expected-warning {{comparison 'unsigned short' > 65535 is always false}}362 return 0;363 if (us >= 65535UL)364 return 0;365 366 if (65535UL == us)367 return 0;368 if (65535UL != us)369 return 0;370 if (65535UL < us) // expected-warning {{comparison 65535 < 'unsigned short' is always false}}371 return 0;372 if (65535UL <= us)373 return 0;374 if (65535UL > us)375 return 0;376 if (65535UL >= us) // expected-warning {{comparison 65535 >= 'unsigned short' is always true}}377 return 0;378#else379 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 if (us >= 65535)390 return 0;391 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 if (65535 >= us)403 return 0;404 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 if (us >= 65535UL)416 return 0;417 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 if (65535UL >= us)429 return 0;430#endif431 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 if (us >= 32767)443 return 0;444 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 if (32767 >= us)456 return 0;457 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 if (us >= 32767UL)469 return 0;470 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 if (32767UL >= us)482 return 0;483 484#if __SIZEOF_INT128__485 __int128 i128 = value();486 if (i128 == -1) // used to crash487 return 0;488#endif489 490 491 enum E {492 yes,493 no,494 maybe495 };496 enum E e = (enum E)value();497 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 if (e >= yes)509 return 0;510 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 if (yes >= e)522 return 0;523 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 if (e >= maybe)535 return 0;536 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 if (maybe >= e)548 return 0;549 550 // We only warn on out-of-range bitfields and expressions with limited range551 // under -Wtantological-in-range-compare, not under -Wtype-limits, because552 // the warning is not based on the type alone.553 struct A {554 int a : 3;555 unsigned b : 3;556 long c : 3;557 unsigned long d : 3;558 } a;559 if (a.a < 3) {}560 if (a.a < 4) {} // #bitfield1561 if (a.b < 7) {}562 if (a.b < 8) {} // #bitfield2563 if (a.c < 3) {}564 if (a.c < 4) {} // #bitfield3565 if (a.d < 7) {}566 if (a.d < 8) {} // #bitfield4567#if TEST == 2568 // expected-warning@#bitfield1 {{comparison of 3-bit signed value < 4 is always true}}569 // expected-warning@#bitfield2 {{comparison of 3-bit unsigned value < 8 is always true}}570 // expected-warning@#bitfield3 {{comparison of 3-bit signed value < 4 is always true}}571 // expected-warning@#bitfield4 {{comparison of 3-bit unsigned value < 8 is always true}}572#endif573 574 if ((s & 0xff) < 0) {} // #valuerange1575 if ((s & 0xff) < 1) {}576 if ((s & -3) < -4) {}577 if ((s & -3) < -3) {}578 if ((s & -3) < 4u) {}579 if ((s & -3) > 4u) {}580 if ((s & -3) == 4u) {}581 if ((s & -3) == 3u) {} // FIXME: Impossible.582 if ((s & -3) == -5u) {}583 if ((s & -3) == -4u) {}584#if TEST == 2585 // expected-warning@#valuerange1 {{comparison of 8-bit unsigned value < 0 is always false}}586#endif587 588 // FIXME: Our bit-level width tracking comes unstuck here: the second of the589 // conditions below is also tautological, but we can't tell that because we590 // don't track the actual range, only the bit-width.591 if ((s ? 1 : 0) + (us ? 1 : 0) > 1) {}592 if ((s ? 1 : 0) + (us ? 1 : 0) > 2) {}593 if ((s ? 1 : 0) + (us ? 1 : 0) > 3) {} // #addrange1594#if TEST == 2595 // expected-warning@#addrange1 {{comparison of 2-bit unsigned value > 3 is always false}}596#endif597 598 // FIXME: The second and third comparisons are also tautological; 0x40000000599 // is the greatest value that multiplying two int16s can produce.600 if (s * s > 0x3fffffff) {}601 if (s * s > 0x40000000) {}602 if (s * s > 0x7ffffffe) {}603 if (s * s > 0x7fffffff) {} // expected-warning {{result of comparison 'int' > 2147483647 is always false}}604 605 if ((s & 0x3ff) * (s & 0x1f) > 0x7be0) {}606 if ((s & 0x3ff) * (s & 0x1f) > 0x7be1) {} // FIXME607 if ((s & 0x3ff) * (s & 0x1f) > 0x7ffe) {} // FIXME608 if ((s & 0x3ff) * (s & 0x1f) > 0x7fff) {} // #mulrange1609#if TEST == 2610 // expected-warning@#mulrange1 {{comparison of 15-bit unsigned value > 32767 is always false}}611#endif612 613 if (a.a * a.b > 21) {} // FIXME614 if (a.a * a.b > 31) {} // #mulrange2615#if TEST == 2616 // expected-warning@#mulrange2 {{comparison of 6-bit signed value > 31 is always false}}617#endif618 619 if (a.a - (s & 1) < -4) {}620 if (a.a - (s & 1) < -7) {} // FIXME621 if (a.a - (s & 1) < -8) {} // #subrange1622 if (a.a - (s & 1) > 3) {} // FIXME: Can be < -4 but not > 3.623 if (a.a - (s & 1) > 7) {} // #subrange2624 625 if (a.a - (s & 7) < -8) {}626 if (a.a - (s & 7) > 7) {} // FIXME: Can be < -8 but not > 7.627 if (a.a - (s & 7) < -15) {}628 if (a.a - (s & 7) < -16) {} // #subrange3629 if (a.a - (s & 7) > 15) {} // #subrange4630 631 if (a.b - (s & 1) > 6) {}632 if (a.b - (s & 1) > 7) {} // #subrange5633 if (a.b - (s & 7) < -8) {} // #subrange6634 if (a.b - (s & 15) < -8) {}635 if (a.b - (s & 15) < -16) {} // #subrange7636#if TEST == 2637 // expected-warning@#subrange1 {{comparison of 4-bit signed value < -8 is always false}}638 // expected-warning@#subrange2 {{comparison of 4-bit signed value > 7 is always false}}639 // expected-warning@#subrange3 {{comparison of 5-bit signed value < -16 is always false}}640 // expected-warning@#subrange4 {{comparison of 5-bit signed value > 15 is always false}}641 // expected-warning@#subrange5 {{comparison of 4-bit signed value > 7 is always false}}642 // expected-warning@#subrange6 {{comparison of 4-bit signed value < -8 is always false}}643 // expected-warning@#subrange7 {{comparison of 5-bit signed value < -16 is always false}}644#endif645 646 // a.a % 3 is in range [-2, 2], which we expand to [-4, 4)647 if (a.a % 3 > 2) {}648 if (a.a % 3 > 3) {} // #remrange1649 if (a.a % 3 == -1) {}650 if (a.a % 3 == -2) {}651 if (a.a % 3 < -3) {} // FIXME652 if (a.a % 3 < -4) {} // #remrange2653 654 // a.b % 3 is in range [0, 3), which we expand to [0, 4)655 if (a.b % 3 > 2) {}656 if (a.b % 3 > 3) {} // #remrange3657 if (a.b % 3 < 0) {} // #remrange4658#if TEST == 2659 // expected-warning@#remrange1 {{comparison of 3-bit signed value > 3 is always false}}660 // expected-warning@#remrange2 {{comparison of 3-bit signed value < -4 is always false}}661 // expected-warning@#remrange3 {{comparison of 2-bit unsigned value > 3 is always false}}662 // expected-warning@#remrange4 {{comparison of 2-bit unsigned value < 0 is always false}}663#endif664 665 // Don't warn on non-constant-expression values that end up being a constant666 // 0; we generally only want to warn when one side of the comparison is667 // effectively non-constant.668 if ("x"[1] == 0) {}669 if (((void)s, 0) == 0) {}670 671 return 1;672}673