diff options
Diffstat (limited to 'test/Analysis/idempotent-operations.c')
-rw-r--r-- | test/Analysis/idempotent-operations.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/test/Analysis/idempotent-operations.c b/test/Analysis/idempotent-operations.c new file mode 100644 index 000000000000..9cef08edc43f --- /dev/null +++ b/test/Analysis/idempotent-operations.c @@ -0,0 +1,52 @@ +// RUN: %clang_cc1 -analyze -analyzer-idempotent-operation -analyzer-store=region -analyzer-constraints=range -fblocks -verify -analyzer-opt-analyze-nested-blocks -analyzer-check-objc-mem -verify %s + +// Basic tests + +extern void test(int i); + +void basic() { + int x = 10, zero = 0, one = 1; + + // x op x + x = x; // expected-warning {{idempotent operation; both operands are always equal in value}} + test(x - x); // expected-warning {{idempotent operation; both operands are always equal in value}} + x -= x; // expected-warning {{idempotent operation; both operands are always equal in value}} + x = 10; // no-warning + test(x / x); // expected-warning {{idempotent operation; both operands are always equal in value}} + x /= x; // expected-warning {{idempotent operation; both operands are always equal in value}} + x = 10; // no-warning + test(x & x); // expected-warning {{idempotent operation; both operands are always equal in value}} + x &= x; // expected-warning {{idempotent operation; both operands are always equal in value}} + test(x | x); // expected-warning {{idempotent operation; both operands are always equal in value}} + x |= x; // expected-warning {{idempotent operation; both operands are always equal in value}} + + // x op 1 + test(x * one); // expected-warning {{idempotent operation; the right operand is always 1}} + x *= one; // expected-warning {{idempotent operation; the right operand is always 1}} + test(x / one); // expected-warning {{idempotent operation; the right operand is always 1}} + x /= one; // expected-warning {{idempotent operation; the right operand is always 1}} + + // 1 op x + test(one * x); // expected-warning {{idempotent operation; the left operand is always 1}} + + // x op 0 + test(x + zero); // expected-warning {{idempotent operation; the right operand is always 0}} + test(x - zero); // expected-warning {{idempotent operation; the right operand is always 0}} + test(x * zero); // expected-warning {{idempotent operation; the right operand is always 0}} + test(x & zero); // expected-warning {{idempotent operation; the right operand is always 0}} + test(x | zero); // expected-warning {{idempotent operation; the right operand is always 0}} + test(x ^ zero); // expected-warning {{idempotent operation; the right operand is always 0}} + test(x << zero); // expected-warning {{idempotent operation; the right operand is always 0}} + test(x >> zero); // expected-warning {{idempotent operation; the right operand is always 0}} + + // 0 op x + test(zero + x); // expected-warning {{idempotent operation; the left operand is always 0}} + test(zero - x); // expected-warning {{idempotent operation; the left operand is always 0}} + test(zero / x); // expected-warning {{idempotent operation; the left operand is always 0}} + test(zero * x); // expected-warning {{idempotent operation; the left operand is always 0}} + test(zero & x); // expected-warning {{idempotent operation; the left operand is always 0}} + test(zero | x); // expected-warning {{idempotent operation; the left operand is always 0}} + test(zero ^ x); // expected-warning {{idempotent operation; the left operand is always 0}} + test(zero << x); // expected-warning {{idempotent operation; the left operand is always 0}} + test(zero >> x); // expected-warning {{idempotent operation; the left operand is always 0}} +} |