2011-08-04 07:14:55 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode -Wno-null-dereference -verify %s
|
2010-06-20 12:56:29 +08:00
|
|
|
|
|
|
|
// Trigger a warning if the analyzer reaches this point in the control flow.
|
|
|
|
#define WARN ((void)*(char*)0)
|
|
|
|
|
|
|
|
// There should be no warnings unless otherwise indicated.
|
|
|
|
|
|
|
|
void testComparisons (int a) {
|
|
|
|
// Sema can already catch the simple comparison a==a,
|
|
|
|
// since that's usually a logic error (and not path-dependent).
|
|
|
|
int b = a;
|
2010-07-28 07:30:21 +08:00
|
|
|
if (!(b==a)) WARN; // expected-warning{{never executed}}
|
|
|
|
if (!(b>=a)) WARN; // expected-warning{{never executed}}
|
|
|
|
if (!(b<=a)) WARN; // expected-warning{{never executed}}
|
|
|
|
if (b!=a) WARN; // expected-warning{{never executed}}
|
|
|
|
if (b>a) WARN; // expected-warning{{never executed}}
|
|
|
|
if (b<a) WARN; // expected-warning{{never executed}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testSelfOperations (int a) {
|
2010-08-19 05:17:24 +08:00
|
|
|
if ((a|a) != a) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a&a) != a) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a^a) != 0) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a-a) != 0) WARN; // expected-warning{{never executed}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testIdempotent (int a) {
|
2010-07-28 07:30:21 +08:00
|
|
|
if ((a*1) != a) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a/1) != a) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a+0) != a) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a-0) != a) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a<<0) != a) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a>>0) != a) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a^0) != a) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a&(~0)) != a) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a|0) != a) WARN; // expected-warning{{never executed}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testReductionToConstant (int a) {
|
2010-07-28 07:30:21 +08:00
|
|
|
if ((a*0) != 0) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a&0) != 0) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((a|(~0)) != (~0)) WARN; // expected-warning{{never executed}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testSymmetricIntSymOperations (int a) {
|
2010-07-28 07:30:21 +08:00
|
|
|
if ((2+a) != (a+2)) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((2*a) != (a*2)) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((2&a) != (a&2)) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((2^a) != (a^2)) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((2|a) != (a|2)) WARN; // expected-warning{{never executed}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testAsymmetricIntSymOperations (int a) {
|
2010-07-28 07:30:21 +08:00
|
|
|
if (((~0) >> a) != (~0)) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((0 >> a) != 0) WARN; // expected-warning{{never executed}}
|
|
|
|
if ((0 << a) != 0) WARN; // expected-warning{{never executed}}
|
2010-06-20 12:56:29 +08:00
|
|
|
|
|
|
|
// Unsigned right shift shifts in zeroes.
|
|
|
|
if ((((unsigned)(~0)) >> ((unsigned) a)) != ((unsigned)(~0)))
|
|
|
|
WARN; // expected-warning{{}}
|
|
|
|
}
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
void testLocations (char *a) {
|
|
|
|
char *b = a;
|
2010-07-28 07:30:21 +08:00
|
|
|
if (!(b==a)) WARN; // expected-warning{{never executed}}
|
|
|
|
if (!(b>=a)) WARN; // expected-warning{{never executed}}
|
|
|
|
if (!(b<=a)) WARN; // expected-warning{{never executed}}
|
|
|
|
if (b!=a) WARN; // expected-warning{{never executed}}
|
|
|
|
if (b>a) WARN; // expected-warning{{never executed}}
|
|
|
|
if (b<a) WARN; // expected-warning{{never executed}}
|
2010-08-19 05:17:24 +08:00
|
|
|
if (b-a) WARN; // expected-warning{{never executed}}
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
2012-05-07 07:40:02 +08:00
|
|
|
|
|
|
|
void testMixedTypeComparisons (char a, unsigned long b) {
|
|
|
|
if (a != 0) return;
|
|
|
|
if (b != 0x100) return;
|
|
|
|
|
|
|
|
if (a > b) WARN; // expected-warning{{never executed}}
|
|
|
|
if (b < a) WARN; // expected-warning{{never executed}}
|
|
|
|
if (a == b) WARN; // expected-warning{{never executed}}
|
|
|
|
}
|