llvm-project/clang/test/Analysis/idempotent-operations.c

114 lines
4.3 KiB
C
Raw Normal View History

// 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
// Basic tests
extern void test(int i);
extern void test_f(float f);
unsigned basic() {
int x = 10, zero = 0, one = 1;
// x op x
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}}
x = 10; // no-warning
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}}
x = 10; // no-warning
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}}
// x op 1
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}}
// 1 op x
test(one * x); // expected-warning {{The left operand to '*' is always 1}}
// x op 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}}
test(x >> zero); // expected-warning {{The right operand to '>>' is always 0}}
// 0 op x
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}}
// Overwrite the values so these aren't marked as psuedoconstants
x = 1;
zero = 2;
one = 3;
return x + zero + one;
}
void floats(float x) {
test_f(x * 1.0); // no-warning
test_f(x * 1.0F); // no-warning
}
2010-07-31 05:42:31 +08:00
// Ensure that we don't report false poitives in complex loops
2010-07-31 05:42:31 +08:00
void bailout() {
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
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++) {
unused *= i; // no-warning
2010-07-31 05:42:31 +08:00
}
}
}
// False positive tests
unsigned false1() {
int a = 10;
return a * (5 - 2 - 3); // no-warning
}
enum testenum { enum1 = 0, enum2 };
unsigned false2() {
int a = 1234;
return enum1 + a; // no-warning
}
// 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
}