blob: 6794d481d68b2cf89488110b60aa51bf8db4c453 [file] [log] [blame]
Ted Kremenek565e4652010-02-05 02:06:54 +00001// RUN: %clang_cc1 -triple i386-apple-darwin9 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -verify -fblocks -analyzer-opt-analyze-nested-blocks %s
2// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -verify -fblocks -analyzer-opt-analyze-nested-blocks %s
Ted Kremenekd87682e2009-12-17 01:44:13 +00003
Ted Kremenek53287512009-12-18 20:13:39 +00004// Test basic handling of references.
Ted Kremenekd87682e2009-12-17 01:44:13 +00005char &test1_aux();
6char *test1() {
7 return &test1_aux();
8}
Ted Kremenek53287512009-12-18 20:13:39 +00009
Zhongxing Xu910e4082009-12-19 03:17:55 +000010// Test test1_aux() evaluates to char &.
Ted Kremenek53287512009-12-18 20:13:39 +000011char test1_as_rvalue() {
12 return test1_aux();
13}
14
Ted Kremenek949bdb42009-12-23 00:26:16 +000015// Test passing a value as a reference. The 'const' in test2_aux() adds
16// an ImplicitCastExpr, which is evaluated as an lvalue.
17int test2_aux(const int &n);
18int test2(int n) {
19 return test2_aux(n);
20}
21
22int test2_b_aux(const short &n);
23int test2_b(int n) {
24 return test2_b_aux(n);
25}
Ted Kremenek077a40d2009-12-23 01:19:20 +000026
27// Test getting the lvalue of a derived and converting it to a base. This
28// previously crashed.
29class Test3_Base {};
30class Test3_Derived : public Test3_Base {};
31
32int test3_aux(Test3_Base &x);
33int test3(Test3_Derived x) {
34 return test3_aux(x);
35}
36
Ted Kremenekde0d2632010-01-05 02:18:06 +000037//===---------------------------------------------------------------------===//
38// Test CFG support for C++ condition variables.
39//===---------------------------------------------------------------------===//
40
Ted Kremenek61dfbec2009-12-23 04:49:01 +000041int test_init_in_condition_aux();
42int test_init_in_condition() {
43 if (int x = test_init_in_condition_aux()) { // no-warning
44 return 1;
45 }
46 return 0;
47}
Ted Kremenekfcfb5032009-12-24 00:40:03 +000048
49int test_init_in_condition_switch() {
50 switch (int x = test_init_in_condition_aux()) { // no-warning
51 case 1:
52 return 0;
53 case 2:
54 if (x == 2)
55 return 0;
56 else {
57 // Unreachable.
58 int *p = 0;
59 *p = 0xDEADBEEF; // no-warning
60 }
61 default:
62 break;
63 }
64 return 0;
65}
Ted Kremenek4c508a12009-12-24 00:54:56 +000066
67int test_init_in_condition_while() {
Ted Kremenek4ec010a2009-12-24 01:34:10 +000068 int z = 0;
69 while (int x = ++z) { // no-warning
70 if (x == 2)
Ted Kremenek4c508a12009-12-24 00:54:56 +000071 break;
Ted Kremenek4c508a12009-12-24 00:54:56 +000072 }
Ted Kremenek4ec010a2009-12-24 01:34:10 +000073
74 if (z == 2)
75 return 0;
76
77 int *p = 0;
78 *p = 0xDEADBEEF; // no-warning
Ted Kremenek4c508a12009-12-24 00:54:56 +000079 return 0;
80}
Ted Kremenek4ec010a2009-12-24 01:34:10 +000081
Ted Kremenekdd8b4412009-12-24 02:41:19 +000082
83int test_init_in_condition_for() {
84 int z = 0;
85 for (int x = 0; int y = ++z; ++x) {
86 if (x == y) // no-warning
87 break;
88 }
89 if (z == 1)
90 return 0;
91
92 int *p = 0;
93 *p = 0xDEADBEEF; // no-warning
94 return 0;
95}
Ted Kremenekde0d2632010-01-05 02:18:06 +000096
97//===---------------------------------------------------------------------===//
98// Test handling of 'this' pointer.
99//===---------------------------------------------------------------------===//
100
101class TestHandleThis {
102 int x;
103
104 TestHandleThis();
105 int foo();
106 int null_deref_negative();
107 int null_deref_positive();
108};
109
110int TestHandleThis::foo() {
111 // Assume that 'x' is initialized.
112 return x + 1; // no-warning
113}
114
115int TestHandleThis::null_deref_negative() {
116 x = 10;
117 if (x == 10) {
118 return 1;
119 }
120 int *p = 0;
121 *p = 0xDEADBEEF; // no-warning
122 return 0;
123}
124
125int TestHandleThis::null_deref_positive() {
126 x = 10;
127 if (x == 9) {
128 return 1;
129 }
130 int *p = 0;
131 *p = 0xDEADBEEF; // expected-warning{{null pointer}}
132 return 0;
133}
134