Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 1 | // 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 Kremenek | d87682e | 2009-12-17 01:44:13 +0000 | [diff] [blame] | 3 | |
Ted Kremenek | 5328751 | 2009-12-18 20:13:39 +0000 | [diff] [blame] | 4 | // Test basic handling of references. |
Ted Kremenek | d87682e | 2009-12-17 01:44:13 +0000 | [diff] [blame] | 5 | char &test1_aux(); |
| 6 | char *test1() { |
| 7 | return &test1_aux(); |
| 8 | } |
Ted Kremenek | 5328751 | 2009-12-18 20:13:39 +0000 | [diff] [blame] | 9 | |
Zhongxing Xu | 910e408 | 2009-12-19 03:17:55 +0000 | [diff] [blame] | 10 | // Test test1_aux() evaluates to char &. |
Ted Kremenek | 5328751 | 2009-12-18 20:13:39 +0000 | [diff] [blame] | 11 | char test1_as_rvalue() { |
| 12 | return test1_aux(); |
| 13 | } |
| 14 | |
Ted Kremenek | 949bdb4 | 2009-12-23 00:26:16 +0000 | [diff] [blame] | 15 | // Test passing a value as a reference. The 'const' in test2_aux() adds |
| 16 | // an ImplicitCastExpr, which is evaluated as an lvalue. |
| 17 | int test2_aux(const int &n); |
| 18 | int test2(int n) { |
| 19 | return test2_aux(n); |
| 20 | } |
| 21 | |
| 22 | int test2_b_aux(const short &n); |
| 23 | int test2_b(int n) { |
| 24 | return test2_b_aux(n); |
| 25 | } |
Ted Kremenek | 077a40d | 2009-12-23 01:19:20 +0000 | [diff] [blame] | 26 | |
| 27 | // Test getting the lvalue of a derived and converting it to a base. This |
| 28 | // previously crashed. |
| 29 | class Test3_Base {}; |
| 30 | class Test3_Derived : public Test3_Base {}; |
| 31 | |
| 32 | int test3_aux(Test3_Base &x); |
| 33 | int test3(Test3_Derived x) { |
| 34 | return test3_aux(x); |
| 35 | } |
| 36 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 37 | //===---------------------------------------------------------------------===// |
| 38 | // Test CFG support for C++ condition variables. |
| 39 | //===---------------------------------------------------------------------===// |
| 40 | |
Ted Kremenek | 61dfbec | 2009-12-23 04:49:01 +0000 | [diff] [blame] | 41 | int test_init_in_condition_aux(); |
| 42 | int test_init_in_condition() { |
| 43 | if (int x = test_init_in_condition_aux()) { // no-warning |
| 44 | return 1; |
| 45 | } |
| 46 | return 0; |
| 47 | } |
Ted Kremenek | fcfb503 | 2009-12-24 00:40:03 +0000 | [diff] [blame] | 48 | |
| 49 | int 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 Kremenek | 4c508a1 | 2009-12-24 00:54:56 +0000 | [diff] [blame] | 66 | |
| 67 | int test_init_in_condition_while() { |
Ted Kremenek | 4ec010a | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 68 | int z = 0; |
| 69 | while (int x = ++z) { // no-warning |
| 70 | if (x == 2) |
Ted Kremenek | 4c508a1 | 2009-12-24 00:54:56 +0000 | [diff] [blame] | 71 | break; |
Ted Kremenek | 4c508a1 | 2009-12-24 00:54:56 +0000 | [diff] [blame] | 72 | } |
Ted Kremenek | 4ec010a | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 73 | |
| 74 | if (z == 2) |
| 75 | return 0; |
| 76 | |
| 77 | int *p = 0; |
| 78 | *p = 0xDEADBEEF; // no-warning |
Ted Kremenek | 4c508a1 | 2009-12-24 00:54:56 +0000 | [diff] [blame] | 79 | return 0; |
| 80 | } |
Ted Kremenek | 4ec010a | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | dd8b441 | 2009-12-24 02:41:19 +0000 | [diff] [blame] | 82 | |
| 83 | int 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 Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 96 | |
| 97 | //===---------------------------------------------------------------------===// |
| 98 | // Test handling of 'this' pointer. |
| 99 | //===---------------------------------------------------------------------===// |
| 100 | |
| 101 | class TestHandleThis { |
| 102 | int x; |
| 103 | |
| 104 | TestHandleThis(); |
| 105 | int foo(); |
| 106 | int null_deref_negative(); |
| 107 | int null_deref_positive(); |
| 108 | }; |
| 109 | |
| 110 | int TestHandleThis::foo() { |
| 111 | // Assume that 'x' is initialized. |
| 112 | return x + 1; // no-warning |
| 113 | } |
| 114 | |
| 115 | int 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 | |
| 125 | int 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 | |
Ted Kremenek | 741b9be | 2010-07-29 01:31:59 +0000 | [diff] [blame] | 135 | // PR 7675 - passing literals by-reference |
| 136 | void pr7675(const double &a); |
| 137 | void pr7675(const int &a); |
| 138 | void pr7675(const char &a); |
| 139 | void pr7675_i(const _Complex double &a); |
| 140 | |
| 141 | void pr7675_test() { |
| 142 | pr7675(10.0); |
| 143 | pr7675(10); |
| 144 | pr7675('c'); |
| 145 | pr7675_i(4.0i); |
| 146 | // Add null deref to ensure we are analyzing the code up to this point. |
| 147 | int *p = 0; |
| 148 | *p = 0xDEADBEEF; // expected-warning{{null pointer}} |
| 149 | } |
| 150 | |
Ted Kremenek | a427f1d | 2010-08-31 18:47:34 +0000 | [diff] [blame] | 151 | // <rdar://problem/8375510> - CFGBuilder should handle temporaries. |
| 152 | struct R8375510 { |
| 153 | R8375510(); |
| 154 | ~R8375510(); |
| 155 | R8375510 operator++(int); |
| 156 | }; |
| 157 | |
| 158 | int r8375510(R8375510 x, R8375510 y) { |
| 159 | for (; ; x++) { } |
| 160 | } |
| 161 | |
Zhanyong Wan | 739830d | 2010-10-31 04:22:34 +0000 | [diff] [blame^] | 162 | // PR8426 -- this used to crash. |
| 163 | |
| 164 | void Use(void* to); |
| 165 | |
| 166 | template <class T> class Foo { |
| 167 | ~Foo(); |
| 168 | struct Bar; |
| 169 | Bar* bar_; |
| 170 | }; |
| 171 | |
| 172 | template <class T> Foo<T>::~Foo() { |
| 173 | Use(bar_); |
| 174 | T::DoSomething(); |
| 175 | bar_->Work(); |
| 176 | } |
| 177 | |
| 178 | // PR8427 -- this used to crash. |
| 179 | |
| 180 | class Dummy {}; |
| 181 | |
| 182 | bool operator==(Dummy, int); |
| 183 | |
| 184 | template <typename T> |
| 185 | class Foo2 { |
| 186 | bool Bar(); |
| 187 | }; |
| 188 | |
| 189 | template <typename T> |
| 190 | bool Foo2<T>::Bar() { |
| 191 | return 0 == T(); |
| 192 | } |
| 193 | |
| 194 | // PR8433 -- this used to crash. |
| 195 | |
| 196 | template <typename T> |
| 197 | class Foo3 { |
| 198 | public: |
| 199 | void Bar(); |
| 200 | void Baz(); |
| 201 | T value_; |
| 202 | }; |
| 203 | |
| 204 | template <typename T> |
| 205 | void Foo3<T>::Bar() { |
| 206 | Baz(); |
| 207 | value_(); |
| 208 | } |