aboutsummaryrefslogtreecommitdiff
path: root/test/Analysis/idempotent-operations.c
blob: 94e972c5519c80d7f7d18138cfb4ef2adddbf855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// RUN: %clang_cc1 -analyze -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);
extern void test_f(float f);

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}}
}

void floats(float x) {
  test_f(x * 1.0); // no-warning
  test_f(x * 1.0F); // no-warning
}