blob: b1ca3eeec98960c326a261b2294c03770f21b223 [file] [log] [blame]
Jordy Rose43fdb7f2010-06-20 04:56:29 +00001// 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) {
Ted Kremenek3e5637f2010-07-27 18:49:08 +000021 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}}
Jordy Rose43fdb7f2010-06-20 04:56:29 +000025}
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}
Jordy Roseeac4a002010-06-28 08:26:15 +000062
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;
Ted Kremenek3e5637f2010-07-27 18:49:08 +000071 if (b-a) WARN; // expected-warning{{Both operands to '-' always have the same value}}
Jordy Roseeac4a002010-06-28 08:26:15 +000072}