blob: 94e972c5519c80d7f7d18138cfb4ef2adddbf855 [file] [log] [blame]
Tom Caredf4ca422010-07-16 20:41:41 +00001// RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-constraints=range -fblocks -verify -analyzer-opt-analyze-nested-blocks -analyzer-check-objc-mem -verify %s
Tom Caredb2fa8a2010-07-06 21:43:29 +00002
3// Basic tests
4
5extern void test(int i);
Ted Kremenek45329312010-07-17 00:40:32 +00006extern void test_f(float f);
Tom Caredb2fa8a2010-07-06 21:43:29 +00007
8void basic() {
9 int x = 10, zero = 0, one = 1;
10
11 // x op x
12 x = x; // expected-warning {{idempotent operation; both operands are always equal in value}}
13 test(x - x); // expected-warning {{idempotent operation; both operands are always equal in value}}
14 x -= x; // expected-warning {{idempotent operation; both operands are always equal in value}}
15 x = 10; // no-warning
16 test(x / x); // expected-warning {{idempotent operation; both operands are always equal in value}}
17 x /= x; // expected-warning {{idempotent operation; both operands are always equal in value}}
18 x = 10; // no-warning
19 test(x & x); // expected-warning {{idempotent operation; both operands are always equal in value}}
20 x &= x; // expected-warning {{idempotent operation; both operands are always equal in value}}
21 test(x | x); // expected-warning {{idempotent operation; both operands are always equal in value}}
22 x |= x; // expected-warning {{idempotent operation; both operands are always equal in value}}
23
24 // x op 1
25 test(x * one); // expected-warning {{idempotent operation; the right operand is always 1}}
26 x *= one; // expected-warning {{idempotent operation; the right operand is always 1}}
27 test(x / one); // expected-warning {{idempotent operation; the right operand is always 1}}
28 x /= one; // expected-warning {{idempotent operation; the right operand is always 1}}
29
30 // 1 op x
31 test(one * x); // expected-warning {{idempotent operation; the left operand is always 1}}
32
33 // x op 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 test(x >> zero); // expected-warning {{idempotent operation; the right operand is always 0}}
42
43 // 0 op x
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 test(zero >> x); // expected-warning {{idempotent operation; the left operand is always 0}}
53}
Ted Kremenek45329312010-07-17 00:40:32 +000054
55void floats(float x) {
56 test_f(x * 1.0); // no-warning
57 test_f(x * 1.0F); // no-warning
58}