blob: 74d31e72396c5aef321c55d473ad29ac85e2fe4b [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
Ted Kremenek3e5637f2010-07-27 18:49:08 +000012 x = x; // expected-warning {{Assigned value is always the same as the existing value}}
13 test(x - x); // expected-warning {{Both operands to '-' always have the same value}}
14 x -= x; // expected-warning {{Both operands to '-=' always have the same value}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000015 x = 10; // no-warning
Ted Kremenek3e5637f2010-07-27 18:49:08 +000016 test(x / x); // expected-warning {{Both operands to '/' always have the same value}}
17 x /= x; // expected-warning {{Both operands to '/=' always have the same value}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000018 x = 10; // no-warning
Ted Kremenek3e5637f2010-07-27 18:49:08 +000019 test(x & x); // expected-warning {{Both operands to '&' always have the same value}}
20 x &= x; // expected-warning {{Both operands to '&=' always have the same value}}
21 test(x | x); // expected-warning {{Both operands to '|' always have the same value}}
22 x |= x; // expected-warning {{Both operands to '|=' always have the same value}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000023
24 // x op 1
Ted Kremenek3e5637f2010-07-27 18:49:08 +000025 test(x * one); // expected-warning {{The right operand to '*' is always 1}}
26 x *= one; // expected-warning {{The right operand to '*=' is always 1}}
27 test(x / one); // expected-warning {{The right operand to '/' is always 1}}
28 x /= one; // expected-warning {{The right operand to '/=' is always 1}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000029
30 // 1 op x
Ted Kremenek3e5637f2010-07-27 18:49:08 +000031 test(one * x); // expected-warning {{The left operand to '*' is always 1}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000032
33 // x op 0
Ted Kremenek3e5637f2010-07-27 18:49:08 +000034 test(x + zero); // expected-warning {{The right operand to '+' is always 0}}
35 test(x - zero); // expected-warning {{The right operand to '-' is always 0}}
36 test(x * zero); // expected-warning {{The right operand to '*' is always 0}}
37 test(x & zero); // expected-warning {{The right operand to '&' is always 0}}
38 test(x | zero); // expected-warning {{The right operand to '|' is always 0}}
39 test(x ^ zero); // expected-warning {{The right operand to '^' is always 0}}
40 test(x << zero); // expected-warning {{The right operand to '<<' is always 0}}
41 test(x >> zero); // expected-warning {{The right operand to '>>' is always 0}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000042
43 // 0 op x
Ted Kremenek3e5637f2010-07-27 18:49:08 +000044 test(zero + x); // expected-warning {{The left operand to '+' is always 0}}
45 test(zero - x); // expected-warning {{The left operand to '-' is always 0}}
46 test(zero / x); // expected-warning {{The left operand to '/' is always 0}}
47 test(zero * x); // expected-warning {{The left operand to '*' is always 0}}
48 test(zero & x); // expected-warning {{The left operand to '&' is always 0}}
49 test(zero | x); // expected-warning {{The left operand to '|' is always 0}}
50 test(zero ^ x); // expected-warning {{The left operand to '^' is always 0}}
51 test(zero << x); // expected-warning {{The left operand to '<<' is always 0}}
52 test(zero >> x); // expected-warning {{The left operand to '>>' is always 0}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000053}
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}