Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,deadcode.experimental.UnreachableCode -Wno-null-dereference -verify %s |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 2 | |
| 3 | // Trigger a warning if the analyzer reaches this point in the control flow. |
| 4 | #define WARN ((void)*(char*)0) |
| 5 | |
| 6 | // There should be no warnings unless otherwise indicated. |
| 7 | |
| 8 | void testComparisons (int a) { |
| 9 | // Sema can already catch the simple comparison a==a, |
| 10 | // since that's usually a logic error (and not path-dependent). |
| 11 | int b = a; |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 12 | if (!(b==a)) WARN; // expected-warning{{never executed}} |
| 13 | if (!(b>=a)) WARN; // expected-warning{{never executed}} |
| 14 | if (!(b<=a)) WARN; // expected-warning{{never executed}} |
| 15 | if (b!=a) WARN; // expected-warning{{never executed}} |
| 16 | if (b>a) WARN; // expected-warning{{never executed}} |
| 17 | if (b<a) WARN; // expected-warning{{never executed}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | void testSelfOperations (int a) { |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 21 | if ((a|a) != a) WARN; // expected-warning{{never executed}} |
| 22 | if ((a&a) != a) WARN; // expected-warning{{never executed}} |
| 23 | if ((a^a) != 0) WARN; // expected-warning{{never executed}} |
| 24 | if ((a-a) != 0) WARN; // expected-warning{{never executed}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void testIdempotent (int a) { |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 28 | if ((a*1) != a) WARN; // expected-warning{{never executed}} |
| 29 | if ((a/1) != a) WARN; // expected-warning{{never executed}} |
| 30 | if ((a+0) != a) WARN; // expected-warning{{never executed}} |
| 31 | if ((a-0) != a) WARN; // expected-warning{{never executed}} |
| 32 | if ((a<<0) != a) WARN; // expected-warning{{never executed}} |
| 33 | if ((a>>0) != a) WARN; // expected-warning{{never executed}} |
| 34 | if ((a^0) != a) WARN; // expected-warning{{never executed}} |
| 35 | if ((a&(~0)) != a) WARN; // expected-warning{{never executed}} |
| 36 | if ((a|0) != a) WARN; // expected-warning{{never executed}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void testReductionToConstant (int a) { |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 40 | if ((a*0) != 0) WARN; // expected-warning{{never executed}} |
| 41 | if ((a&0) != 0) WARN; // expected-warning{{never executed}} |
| 42 | if ((a|(~0)) != (~0)) WARN; // expected-warning{{never executed}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void testSymmetricIntSymOperations (int a) { |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 46 | if ((2+a) != (a+2)) WARN; // expected-warning{{never executed}} |
| 47 | if ((2*a) != (a*2)) WARN; // expected-warning{{never executed}} |
| 48 | if ((2&a) != (a&2)) WARN; // expected-warning{{never executed}} |
| 49 | if ((2^a) != (a^2)) WARN; // expected-warning{{never executed}} |
| 50 | if ((2|a) != (a|2)) WARN; // expected-warning{{never executed}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void testAsymmetricIntSymOperations (int a) { |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 54 | if (((~0) >> a) != (~0)) WARN; // expected-warning{{never executed}} |
| 55 | if ((0 >> a) != 0) WARN; // expected-warning{{never executed}} |
| 56 | if ((0 << a) != 0) WARN; // expected-warning{{never executed}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 57 | |
| 58 | // Unsigned right shift shifts in zeroes. |
| 59 | if ((((unsigned)(~0)) >> ((unsigned) a)) != ((unsigned)(~0))) |
| 60 | WARN; // expected-warning{{}} |
| 61 | } |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 62 | |
| 63 | void testLocations (char *a) { |
| 64 | char *b = a; |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 65 | if (!(b==a)) WARN; // expected-warning{{never executed}} |
| 66 | if (!(b>=a)) WARN; // expected-warning{{never executed}} |
| 67 | if (!(b<=a)) WARN; // expected-warning{{never executed}} |
| 68 | if (b!=a) WARN; // expected-warning{{never executed}} |
| 69 | if (b>a) WARN; // expected-warning{{never executed}} |
| 70 | if (b<a) WARN; // expected-warning{{never executed}} |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 71 | if (b-a) WARN; // expected-warning{{never executed}} |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 72 | } |