Ted Kremenek | d87682e | 2009-12-17 01:44:13 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i386-apple-darwin9 -analyze -analyzer-experimental-internal-checks -checker-cfref -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 -checker-cfref -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 | 61dfbec | 2009-12-23 04:49:01 +0000 | [diff] [blame] | 37 | int test_init_in_condition_aux(); |
| 38 | int test_init_in_condition() { |
| 39 | if (int x = test_init_in_condition_aux()) { // no-warning |
| 40 | return 1; |
| 41 | } |
| 42 | return 0; |
| 43 | } |
Ted Kremenek | fcfb503 | 2009-12-24 00:40:03 +0000 | [diff] [blame] | 44 | |
| 45 | int test_init_in_condition_switch() { |
| 46 | switch (int x = test_init_in_condition_aux()) { // no-warning |
| 47 | case 1: |
| 48 | return 0; |
| 49 | case 2: |
| 50 | if (x == 2) |
| 51 | return 0; |
| 52 | else { |
| 53 | // Unreachable. |
| 54 | int *p = 0; |
| 55 | *p = 0xDEADBEEF; // no-warning |
| 56 | } |
| 57 | default: |
| 58 | break; |
| 59 | } |
| 60 | return 0; |
| 61 | } |
Ted Kremenek | 4c508a1 | 2009-12-24 00:54:56 +0000 | [diff] [blame] | 62 | |
| 63 | int test_init_in_condition_while() { |
Ted Kremenek | 4ec010a | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 64 | int z = 0; |
| 65 | while (int x = ++z) { // no-warning |
| 66 | if (x == 2) |
Ted Kremenek | 4c508a1 | 2009-12-24 00:54:56 +0000 | [diff] [blame] | 67 | break; |
Ted Kremenek | 4c508a1 | 2009-12-24 00:54:56 +0000 | [diff] [blame] | 68 | } |
Ted Kremenek | 4ec010a | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 69 | |
| 70 | if (z == 2) |
| 71 | return 0; |
| 72 | |
| 73 | int *p = 0; |
| 74 | *p = 0xDEADBEEF; // no-warning |
Ted Kremenek | 4c508a1 | 2009-12-24 00:54:56 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
Ted Kremenek | 4ec010a | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | dd8b441 | 2009-12-24 02:41:19 +0000 | [diff] [blame^] | 78 | |
| 79 | int test_init_in_condition_for() { |
| 80 | int z = 0; |
| 81 | for (int x = 0; int y = ++z; ++x) { |
| 82 | if (x == y) // no-warning |
| 83 | break; |
| 84 | } |
| 85 | if (z == 1) |
| 86 | return 0; |
| 87 | |
| 88 | int *p = 0; |
| 89 | *p = 0xDEADBEEF; // no-warning |
| 90 | return 0; |
| 91 | } |