2010-08-07 06:23:07 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-constraints=range -fblocks -verify -analyzer-opt-analyze-nested-blocks -analyzer-check-objc-mem -analyzer-check-idempotent-operations -verify %s
|
2010-07-07 05:43:29 +08:00
|
|
|
|
|
|
|
// Basic tests
|
|
|
|
|
|
|
|
extern void test(int i);
|
2010-07-17 08:40:32 +08:00
|
|
|
extern void test_f(float f);
|
2010-07-07 05:43:29 +08:00
|
|
|
|
2010-08-19 05:17:24 +08:00
|
|
|
unsigned basic() {
|
2010-07-07 05:43:29 +08:00
|
|
|
int x = 10, zero = 0, one = 1;
|
|
|
|
|
|
|
|
// x op x
|
2010-07-28 02:49:08 +08:00
|
|
|
x = x; // expected-warning {{Assigned value is always the same as the existing value}}
|
|
|
|
test(x - x); // expected-warning {{Both operands to '-' always have the same value}}
|
|
|
|
x -= x; // expected-warning {{Both operands to '-=' always have the same value}}
|
2010-07-07 05:43:29 +08:00
|
|
|
x = 10; // no-warning
|
2010-07-28 02:49:08 +08:00
|
|
|
test(x / x); // expected-warning {{Both operands to '/' always have the same value}}
|
|
|
|
x /= x; // expected-warning {{Both operands to '/=' always have the same value}}
|
2010-07-07 05:43:29 +08:00
|
|
|
x = 10; // no-warning
|
2010-07-28 02:49:08 +08:00
|
|
|
test(x & x); // expected-warning {{Both operands to '&' always have the same value}}
|
|
|
|
x &= x; // expected-warning {{Both operands to '&=' always have the same value}}
|
|
|
|
test(x | x); // expected-warning {{Both operands to '|' always have the same value}}
|
|
|
|
x |= x; // expected-warning {{Both operands to '|=' always have the same value}}
|
2010-07-07 05:43:29 +08:00
|
|
|
|
|
|
|
// x op 1
|
2010-07-28 02:49:08 +08:00
|
|
|
test(x * one); // expected-warning {{The right operand to '*' is always 1}}
|
|
|
|
x *= one; // expected-warning {{The right operand to '*=' is always 1}}
|
|
|
|
test(x / one); // expected-warning {{The right operand to '/' is always 1}}
|
|
|
|
x /= one; // expected-warning {{The right operand to '/=' is always 1}}
|
2010-07-07 05:43:29 +08:00
|
|
|
|
|
|
|
// 1 op x
|
2010-07-28 02:49:08 +08:00
|
|
|
test(one * x); // expected-warning {{The left operand to '*' is always 1}}
|
2010-07-07 05:43:29 +08:00
|
|
|
|
|
|
|
// x op 0
|
2010-07-28 02:49:08 +08:00
|
|
|
test(x + zero); // expected-warning {{The right operand to '+' is always 0}}
|
|
|
|
test(x - zero); // expected-warning {{The right operand to '-' is always 0}}
|
|
|
|
test(x * zero); // expected-warning {{The right operand to '*' is always 0}}
|
|
|
|
test(x & zero); // expected-warning {{The right operand to '&' is always 0}}
|
|
|
|
test(x | zero); // expected-warning {{The right operand to '|' is always 0}}
|
|
|
|
test(x ^ zero); // expected-warning {{The right operand to '^' is always 0}}
|
|
|
|
test(x << zero); // expected-warning {{The right operand to '<<' is always 0}}
|
|
|
|
test(x >> zero); // expected-warning {{The right operand to '>>' is always 0}}
|
2010-07-07 05:43:29 +08:00
|
|
|
|
|
|
|
// 0 op x
|
2010-07-28 02:49:08 +08:00
|
|
|
test(zero + x); // expected-warning {{The left operand to '+' is always 0}}
|
|
|
|
test(zero - x); // expected-warning {{The left operand to '-' is always 0}}
|
|
|
|
test(zero / x); // expected-warning {{The left operand to '/' is always 0}}
|
|
|
|
test(zero * x); // expected-warning {{The left operand to '*' is always 0}}
|
|
|
|
test(zero & x); // expected-warning {{The left operand to '&' is always 0}}
|
|
|
|
test(zero | x); // expected-warning {{The left operand to '|' is always 0}}
|
|
|
|
test(zero ^ x); // expected-warning {{The left operand to '^' is always 0}}
|
|
|
|
test(zero << x); // expected-warning {{The left operand to '<<' is always 0}}
|
|
|
|
test(zero >> x); // expected-warning {{The left operand to '>>' is always 0}}
|
2010-08-19 05:17:24 +08:00
|
|
|
|
|
|
|
// Overwrite the values so these aren't marked as psuedoconstants
|
|
|
|
x = 1;
|
|
|
|
zero = 2;
|
|
|
|
one = 3;
|
|
|
|
|
|
|
|
return x + zero + one;
|
2010-07-07 05:43:29 +08:00
|
|
|
}
|
2010-07-17 08:40:32 +08:00
|
|
|
|
|
|
|
void floats(float x) {
|
2010-08-13 06:45:47 +08:00
|
|
|
test_f(x * 1.0); // no-warning
|
2010-07-17 08:40:32 +08:00
|
|
|
test_f(x * 1.0F); // no-warning
|
|
|
|
}
|
2010-07-31 05:42:31 +08:00
|
|
|
|
2010-08-13 06:45:47 +08:00
|
|
|
// Ensure that we don't report false poitives in complex loops
|
2010-07-31 05:42:31 +08:00
|
|
|
void bailout() {
|
2010-08-13 06:45:47 +08:00
|
|
|
int unused = 0, result = 4;
|
|
|
|
result = result; // expected-warning {{Assigned value is always the same as the existing value}}
|
2010-07-31 05:42:31 +08:00
|
|
|
|
2010-08-13 06:45:47 +08:00
|
|
|
for (unsigned bg = 0; bg < 1024; bg ++) {
|
|
|
|
result = bg * result; // no-warning
|
2010-07-31 05:42:31 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++) {
|
2010-08-13 06:45:47 +08:00
|
|
|
unused *= i; // no-warning
|
2010-07-31 05:42:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-13 06:45:47 +08:00
|
|
|
|
|
|
|
// False positive tests
|
|
|
|
|
|
|
|
unsigned false1() {
|
2010-08-17 05:43:52 +08:00
|
|
|
int a = 10;
|
|
|
|
return a * (5 - 2 - 3); // no-warning
|
2010-08-13 06:45:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum testenum { enum1 = 0, enum2 };
|
|
|
|
unsigned false2() {
|
2010-08-17 05:43:52 +08:00
|
|
|
int a = 1234;
|
|
|
|
return enum1 + a; // no-warning
|
2010-08-13 06:45:47 +08:00
|
|
|
}
|
|
|
|
|
2010-08-19 05:17:24 +08:00
|
|
|
// Self assignments of parameters are common false positives
|
|
|
|
unsigned false3(int param) {
|
|
|
|
param = param; // no-warning
|
|
|
|
|
|
|
|
unsigned nonparam = 5;
|
|
|
|
|
|
|
|
nonparam = nonparam; // expected-warning{{Assigned value is always the same as the existing value}}
|
|
|
|
|
|
|
|
return nonparam;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Psuedo-constants (vars only read) and constants should not be reported
|
|
|
|
unsigned false4() {
|
|
|
|
// Trivial constant
|
|
|
|
const int height = 1; // no-warning
|
|
|
|
// Psuedo-constant (never changes after decl)
|
|
|
|
int width = height; // no-warning
|
|
|
|
|
|
|
|
return width * 10; // no-warning
|
|
|
|
}
|