2018-08-30 04:29:17 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify -analyzer-config eagerly-assume=false %s
|
2010-06-20 12:56:29 +08:00
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
void clang_analyzer_eval(int);
|
2010-06-20 12:56:29 +08:00
|
|
|
|
|
|
|
// 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;
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(b == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(b >= a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(b <= a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(b != a); // expected-warning{{FALSE}}
|
|
|
|
clang_analyzer_eval(b > a); // expected-warning{{FALSE}}
|
|
|
|
clang_analyzer_eval(b < a); // expected-warning{{FALSE}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testSelfOperations (int a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval((a|a) == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a&a) == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a^a) == 0); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a-a) == 0); // expected-warning{{TRUE}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testIdempotent (int a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval((a*1) == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a/1) == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a+0) == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a-0) == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a<<0) == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a>>0) == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a^0) == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a&(~0)) == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a|0) == a); // expected-warning{{TRUE}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testReductionToConstant (int a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval((a*0) == 0); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a&0) == 0); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a|(~0)) == (~0)); // expected-warning{{TRUE}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testSymmetricIntSymOperations (int a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval((2+a) == (a+2)); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((2*a) == (a*2)); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((2&a) == (a&2)); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((2^a) == (a^2)); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((2|a) == (a|2)); // expected-warning{{TRUE}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testAsymmetricIntSymOperations (int a) {
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(((~0) >> a) == (~0)); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((0 >> a) == 0); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((0 << a) == 0); // expected-warning{{TRUE}}
|
2010-06-20 12:56:29 +08:00
|
|
|
|
|
|
|
// Unsigned right shift shifts in zeroes.
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(((~0U) >> a) != (~0U)); // expected-warning{{UNKNOWN}}
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
void testLocations (char *a) {
|
|
|
|
char *b = a;
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(b == a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(b >= a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(b <= a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(b != a); // expected-warning{{FALSE}}
|
|
|
|
clang_analyzer_eval(b > a); // expected-warning{{FALSE}}
|
|
|
|
clang_analyzer_eval(b < a); // expected-warning{{FALSE}}
|
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;
|
|
|
|
|
2012-05-17 00:01:10 +08:00
|
|
|
clang_analyzer_eval(a <= b); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(b >= a); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(a != b); // expected-warning{{TRUE}}
|
2012-05-07 07:40:02 +08:00
|
|
|
}
|
2017-11-10 03:06:22 +08:00
|
|
|
|
2020-05-05 00:44:43 +08:00
|
|
|
void testBitwiseRules(unsigned int a, int b, int c) {
|
2020-05-06 00:42:33 +08:00
|
|
|
clang_analyzer_eval((a | 1) >= 1); // expected-warning{{TRUE}}
|
2017-11-10 03:06:22 +08:00
|
|
|
clang_analyzer_eval((a | -1) >= -1); // expected-warning{{TRUE}}
|
2020-05-06 00:42:33 +08:00
|
|
|
clang_analyzer_eval((a | 2) >= 2); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a | 5) >= 5); // expected-warning{{TRUE}}
|
2017-11-10 03:06:22 +08:00
|
|
|
clang_analyzer_eval((a | 10) >= 10); // expected-warning{{TRUE}}
|
|
|
|
|
|
|
|
// Argument order should not influence this
|
|
|
|
clang_analyzer_eval((1 | a) >= 1); // expected-warning{{TRUE}}
|
|
|
|
|
2020-05-06 00:42:33 +08:00
|
|
|
clang_analyzer_eval((a & 1) <= 1); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a & 1) >= 0); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a & 2) <= 2); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a & 5) <= 5); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((a & 10) <= 10); // expected-warning{{TRUE}}
|
2017-11-10 03:06:22 +08:00
|
|
|
clang_analyzer_eval((a & -10) <= 10); // expected-warning{{UNKNOWN}}
|
|
|
|
|
|
|
|
// Again, check for different argument order.
|
|
|
|
clang_analyzer_eval((1 & a) <= 1); // expected-warning{{TRUE}}
|
|
|
|
|
2020-05-05 00:44:43 +08:00
|
|
|
unsigned int d = a;
|
|
|
|
d |= 1;
|
|
|
|
clang_analyzer_eval((d | 0) == 0); // expected-warning{{FALSE}}
|
2017-11-10 03:06:22 +08:00
|
|
|
|
|
|
|
// Rules don't apply to signed typed, as the values might be negative.
|
|
|
|
clang_analyzer_eval((b | 1) > 0); // expected-warning{{UNKNOWN}}
|
|
|
|
|
|
|
|
// Even for signed values, bitwise OR with a non-zero is always non-zero.
|
2020-05-06 00:42:33 +08:00
|
|
|
clang_analyzer_eval((b | 1) == 0); // expected-warning{{FALSE}}
|
2017-11-10 03:06:22 +08:00
|
|
|
clang_analyzer_eval((b | -2) == 0); // expected-warning{{FALSE}}
|
|
|
|
clang_analyzer_eval((b | 10) == 0); // expected-warning{{FALSE}}
|
2020-05-06 00:42:33 +08:00
|
|
|
clang_analyzer_eval((b | 0) == 0); // expected-warning{{UNKNOWN}}
|
2018-06-06 14:25:51 +08:00
|
|
|
clang_analyzer_eval((b | -2) >= 0); // expected-warning{{FALSE}}
|
2020-05-05 00:44:43 +08:00
|
|
|
|
|
|
|
// Check that we can operate with negative ranges
|
|
|
|
if (b < 0) {
|
|
|
|
clang_analyzer_eval((b | -1) == -1); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((b | -10) >= -10); // expected-warning{{TRUE}}
|
2020-05-06 00:42:33 +08:00
|
|
|
clang_analyzer_eval((b & 0) == 0); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((b & -10) <= -10); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((b & 5) >= 0); // expected-warning{{TRUE}}
|
2020-05-05 00:44:43 +08:00
|
|
|
|
|
|
|
int e = (b | -5);
|
|
|
|
clang_analyzer_eval(e >= -5 && e <= -1); // expected-warning{{TRUE}}
|
|
|
|
|
|
|
|
if (b < -20) {
|
2020-05-06 00:42:33 +08:00
|
|
|
clang_analyzer_eval((b | e) >= -5); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((b & -10) < -20); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((b & e) < -20); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval((b & -30) <= -30); // expected-warning{{TRUE}}
|
|
|
|
|
|
|
|
if (c >= -30 && c <= -10) {
|
|
|
|
clang_analyzer_eval((b & c) <= -20); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a <= 40) {
|
|
|
|
int g = (int)a & b;
|
|
|
|
clang_analyzer_eval(g <= 40 && g >= 0); // expected-warning{{TRUE}}
|
2020-05-05 00:44:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we can reason about the result even if know nothing
|
|
|
|
// about one of the operands.
|
|
|
|
clang_analyzer_eval((b | c) != 0); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a <= 30 && b >= 10 && c >= 20) {
|
|
|
|
// Check that we can reason about non-constant operands.
|
|
|
|
clang_analyzer_eval((b | c) >= 20); // expected-warning{{TRUE}}
|
|
|
|
|
|
|
|
// Check that we can reason about the resulting range even if
|
|
|
|
// the types are not the same, but we still can convert operand
|
|
|
|
// ranges.
|
|
|
|
clang_analyzer_eval((a | b) >= 10); // expected-warning{{TRUE}}
|
2020-05-06 00:42:33 +08:00
|
|
|
clang_analyzer_eval((a & b) <= 30); // expected-warning{{TRUE}}
|
|
|
|
|
|
|
|
if (b <= 20) {
|
|
|
|
clang_analyzer_eval((a & b) <= 20); // expected-warning{{TRUE}}
|
|
|
|
}
|
2020-05-05 00:44:43 +08:00
|
|
|
}
|
2017-11-10 03:06:22 +08:00
|
|
|
|
|
|
|
// Check that dynamically computed constants also work.
|
2020-05-01 16:49:23 +08:00
|
|
|
unsigned int constant = 1 << 3;
|
2020-05-05 00:44:43 +08:00
|
|
|
unsigned int f = a | constant;
|
|
|
|
clang_analyzer_eval(f >= constant); // expected-warning{{TRUE}}
|
2020-05-01 16:49:23 +08:00
|
|
|
|
|
|
|
// Check that nested expressions also work.
|
|
|
|
clang_analyzer_eval(((a | 10) | 5) >= 10); // expected-warning{{TRUE}}
|
|
|
|
|
2020-05-05 00:44:43 +08:00
|
|
|
if (a < 10) {
|
|
|
|
clang_analyzer_eval((a | 20) >= 20); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
2020-05-01 16:49:23 +08:00
|
|
|
if (a > 10) {
|
2020-05-06 00:42:33 +08:00
|
|
|
clang_analyzer_eval((a & 1) <= 1); // expected-warning{{TRUE}}
|
2020-05-01 16:49:23 +08:00
|
|
|
}
|
2017-11-10 03:06:22 +08:00
|
|
|
}
|