blob: d3cbf9c56479df00f2b3528996c3f7e05c897eda [file] [log] [blame]
Argyrios Kyrtzidis8a285ae2011-04-26 17:41:22 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,deadcode.experimental.UnreachableCode -Wno-null-dereference -verify %s
Jordy Rose43fdb7f2010-06-20 04:56:29 +00002
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;
Tom Care7bce3a12010-07-27 23:30:21 +000012 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 Rose43fdb7f2010-06-20 04:56:29 +000018}
19
20void testSelfOperations (int a) {
Tom Care245adab2010-08-18 21:17:24 +000021 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 Rose43fdb7f2010-06-20 04:56:29 +000025}
26
27void testIdempotent (int a) {
Tom Care7bce3a12010-07-27 23:30:21 +000028 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 Rose43fdb7f2010-06-20 04:56:29 +000037}
38
39void testReductionToConstant (int a) {
Tom Care7bce3a12010-07-27 23:30:21 +000040 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 Rose43fdb7f2010-06-20 04:56:29 +000043}
44
45void testSymmetricIntSymOperations (int a) {
Tom Care7bce3a12010-07-27 23:30:21 +000046 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 Rose43fdb7f2010-06-20 04:56:29 +000051}
52
53void testAsymmetricIntSymOperations (int a) {
Tom Care7bce3a12010-07-27 23:30:21 +000054 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 Rose43fdb7f2010-06-20 04:56:29 +000057
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;
Tom Care7bce3a12010-07-27 23:30:21 +000065 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 Care245adab2010-08-18 21:17:24 +000071 if (b-a) WARN; // expected-warning{{never executed}}
Jordy Roseeac4a002010-06-28 08:26:15 +000072}