idempotent-operations.c revision df4ca423ec7d9b62842e112d1b824faa08b64810
1// RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-constraints=range -fblocks -verify -analyzer-opt-analyze-nested-blocks -analyzer-check-objc-mem -verify %s
2
3// Basic tests
4
5extern void test(int i);
6
7void basic() {
8  int x = 10, zero = 0, one = 1;
9
10  // x op x
11  x = x;        // expected-warning {{idempotent operation; both operands are always equal in value}}
12  test(x - x);  // expected-warning {{idempotent operation; both operands are always equal in value}}
13  x -= x;       // expected-warning {{idempotent operation; both operands are always equal in value}}
14  x = 10;       // no-warning
15  test(x / x);  // expected-warning {{idempotent operation; both operands are always equal in value}}
16  x /= x;       // expected-warning {{idempotent operation; both operands are always equal in value}}
17  x = 10;       // no-warning
18  test(x & x);  // expected-warning {{idempotent operation; both operands are always equal in value}}
19  x &= x;       // expected-warning {{idempotent operation; both operands are always equal in value}}
20  test(x | x);  // expected-warning {{idempotent operation; both operands are always equal in value}}
21  x |= x;       // expected-warning {{idempotent operation; both operands are always equal in value}}
22
23  // x op 1
24  test(x * one);  // expected-warning {{idempotent operation; the right operand is always 1}}
25  x *= one;       // expected-warning {{idempotent operation; the right operand is always 1}}
26  test(x / one);  // expected-warning {{idempotent operation; the right operand is always 1}}
27  x /= one;       // expected-warning {{idempotent operation; the right operand is always 1}}
28
29  // 1 op x
30  test(one * x);   // expected-warning {{idempotent operation; the left operand is always 1}}
31
32  // x op 0
33  test(x + zero);  // expected-warning {{idempotent operation; the right operand is always 0}}
34  test(x - zero);  // expected-warning {{idempotent operation; the right operand is always 0}}
35  test(x * zero);  // expected-warning {{idempotent operation; the right operand is always 0}}
36  test(x & zero);  // expected-warning {{idempotent operation; the right operand is always 0}}
37  test(x | zero);  // expected-warning {{idempotent operation; the right operand is always 0}}
38  test(x ^ zero);  // expected-warning {{idempotent operation; the right operand is always 0}}
39  test(x << zero); // expected-warning {{idempotent operation; the right operand is always 0}}
40  test(x >> zero); // expected-warning {{idempotent operation; the right operand is always 0}}
41
42  // 0 op x
43  test(zero + x);  // expected-warning {{idempotent operation; the left operand is always 0}}
44  test(zero - x);  // expected-warning {{idempotent operation; the left operand is always 0}}
45  test(zero / x);  // expected-warning {{idempotent operation; the left operand is always 0}}
46  test(zero * x);  // expected-warning {{idempotent operation; the left operand is always 0}}
47  test(zero & x);  // expected-warning {{idempotent operation; the left operand is always 0}}
48  test(zero | x);  // expected-warning {{idempotent operation; the left operand is always 0}}
49  test(zero ^ x);  // expected-warning {{idempotent operation; the left operand is always 0}}
50  test(zero << x); // expected-warning {{idempotent operation; the left operand is always 0}}
51  test(zero >> x); // expected-warning {{idempotent operation; the left operand is always 0}}
52}
53