Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -verify %s |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 2 | |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 3 | void clang_analyzer_eval(int); |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 4 | |
| 5 | // There should be no warnings unless otherwise indicated. |
| 6 | |
| 7 | void testComparisons (int a) { |
| 8 | // Sema can already catch the simple comparison a==a, |
| 9 | // since that's usually a logic error (and not path-dependent). |
| 10 | int b = a; |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 11 | clang_analyzer_eval(b == a); // expected-warning{{TRUE}} |
| 12 | clang_analyzer_eval(b >= a); // expected-warning{{TRUE}} |
| 13 | clang_analyzer_eval(b <= a); // expected-warning{{TRUE}} |
| 14 | clang_analyzer_eval(b != a); // expected-warning{{FALSE}} |
| 15 | clang_analyzer_eval(b > a); // expected-warning{{FALSE}} |
| 16 | clang_analyzer_eval(b < a); // expected-warning{{FALSE}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | void testSelfOperations (int a) { |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 20 | clang_analyzer_eval((a|a) == a); // expected-warning{{TRUE}} |
| 21 | clang_analyzer_eval((a&a) == a); // expected-warning{{TRUE}} |
| 22 | clang_analyzer_eval((a^a) == 0); // expected-warning{{TRUE}} |
| 23 | clang_analyzer_eval((a-a) == 0); // expected-warning{{TRUE}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void testIdempotent (int a) { |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 27 | clang_analyzer_eval((a*1) == a); // expected-warning{{TRUE}} |
| 28 | clang_analyzer_eval((a/1) == a); // expected-warning{{TRUE}} |
| 29 | clang_analyzer_eval((a+0) == a); // expected-warning{{TRUE}} |
| 30 | clang_analyzer_eval((a-0) == a); // expected-warning{{TRUE}} |
| 31 | clang_analyzer_eval((a<<0) == a); // expected-warning{{TRUE}} |
| 32 | clang_analyzer_eval((a>>0) == a); // expected-warning{{TRUE}} |
| 33 | clang_analyzer_eval((a^0) == a); // expected-warning{{TRUE}} |
| 34 | clang_analyzer_eval((a&(~0)) == a); // expected-warning{{TRUE}} |
| 35 | clang_analyzer_eval((a|0) == a); // expected-warning{{TRUE}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void testReductionToConstant (int a) { |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 39 | clang_analyzer_eval((a*0) == 0); // expected-warning{{TRUE}} |
| 40 | clang_analyzer_eval((a&0) == 0); // expected-warning{{TRUE}} |
| 41 | clang_analyzer_eval((a|(~0)) == (~0)); // expected-warning{{TRUE}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void testSymmetricIntSymOperations (int a) { |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 45 | clang_analyzer_eval((2+a) == (a+2)); // expected-warning{{TRUE}} |
| 46 | clang_analyzer_eval((2*a) == (a*2)); // expected-warning{{TRUE}} |
| 47 | clang_analyzer_eval((2&a) == (a&2)); // expected-warning{{TRUE}} |
| 48 | clang_analyzer_eval((2^a) == (a^2)); // expected-warning{{TRUE}} |
| 49 | clang_analyzer_eval((2|a) == (a|2)); // expected-warning{{TRUE}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void testAsymmetricIntSymOperations (int a) { |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 53 | clang_analyzer_eval(((~0) >> a) == (~0)); // expected-warning{{TRUE}} |
| 54 | clang_analyzer_eval((0 >> a) == 0); // expected-warning{{TRUE}} |
| 55 | clang_analyzer_eval((0 << a) == 0); // expected-warning{{TRUE}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 56 | |
| 57 | // Unsigned right shift shifts in zeroes. |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 58 | clang_analyzer_eval(((~0U) >> a) != (~0U)); // expected-warning{{UNKNOWN}} |
Jordy Rose | 43fdb7f | 2010-06-20 04:56:29 +0000 | [diff] [blame] | 59 | } |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 60 | |
| 61 | void testLocations (char *a) { |
| 62 | char *b = a; |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 63 | clang_analyzer_eval(b == a); // expected-warning{{TRUE}} |
| 64 | clang_analyzer_eval(b >= a); // expected-warning{{TRUE}} |
| 65 | clang_analyzer_eval(b <= a); // expected-warning{{TRUE}} |
| 66 | clang_analyzer_eval(b != a); // expected-warning{{FALSE}} |
| 67 | clang_analyzer_eval(b > a); // expected-warning{{FALSE}} |
| 68 | clang_analyzer_eval(b < a); // expected-warning{{FALSE}} |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 69 | } |
Jordy Rose | c838fd2 | 2012-05-06 23:40:02 +0000 | [diff] [blame] | 70 | |
| 71 | void testMixedTypeComparisons (char a, unsigned long b) { |
| 72 | if (a != 0) return; |
| 73 | if (b != 0x100) return; |
| 74 | |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 75 | clang_analyzer_eval(a <= b); // expected-warning{{TRUE}} |
| 76 | clang_analyzer_eval(b >= a); // expected-warning{{TRUE}} |
| 77 | clang_analyzer_eval(a != b); // expected-warning{{TRUE}} |
Jordy Rose | c838fd2 | 2012-05-06 23:40:02 +0000 | [diff] [blame] | 78 | } |