constant-folding.c revision 3e5637f8a1c5b3cc3fa9d4e33a5763883ea97fc6
1// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-experimental-checks -verify %s 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 8void 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; 12 if (!(b==a)) WARN; 13 if (!(b>=a)) WARN; 14 if (!(b<=a)) WARN; 15 if (b!=a) WARN; 16 if (b>a) WARN; 17 if (b<a) WARN; 18} 19 20void testSelfOperations (int a) { 21 if ((a|a) != a) WARN; // expected-warning{{Both operands to '|' always have the same value}} 22 if ((a&a) != a) WARN; // expected-warning{{Both operands to '&' always have the same value}} 23 if ((a^a) != 0) WARN; // expected-warning{{Both operands to '^' always have the same value}} 24 if ((a-a) != 0) WARN; // expected-warning{{Both operands to '-' always have the same value}} 25} 26 27void testIdempotent (int a) { 28 if ((a*1) != a) WARN; 29 if ((a/1) != a) WARN; 30 if ((a+0) != a) WARN; 31 if ((a-0) != a) WARN; 32 if ((a<<0) != a) WARN; 33 if ((a>>0) != a) WARN; 34 if ((a^0) != a) WARN; 35 if ((a&(~0)) != a) WARN; 36 if ((a|0) != a) WARN; 37} 38 39void testReductionToConstant (int a) { 40 if ((a*0) != 0) WARN; 41 if ((a&0) != 0) WARN; 42 if ((a|(~0)) != (~0)) WARN; 43} 44 45void testSymmetricIntSymOperations (int a) { 46 if ((2+a) != (a+2)) WARN; 47 if ((2*a) != (a*2)) WARN; 48 if ((2&a) != (a&2)) WARN; 49 if ((2^a) != (a^2)) WARN; 50 if ((2|a) != (a|2)) WARN; 51} 52 53void testAsymmetricIntSymOperations (int a) { 54 if (((~0) >> a) != (~0)) WARN; 55 if ((0 >> a) != 0) WARN; 56 if ((0 << a) != 0) WARN; 57 58 // Unsigned right shift shifts in zeroes. 59 if ((((unsigned)(~0)) >> ((unsigned) a)) != ((unsigned)(~0))) 60 WARN; // expected-warning{{}} 61} 62 63void testLocations (char *a) { 64 char *b = a; 65 if (!(b==a)) WARN; 66 if (!(b>=a)) WARN; 67 if (!(b<=a)) WARN; 68 if (b!=a) WARN; 69 if (b>a) WARN; 70 if (b<a) WARN; 71 if (b-a) WARN; // expected-warning{{Both operands to '-' always have the same value}} 72} 73