John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -fblocks %s -Wno-unreachable-code |
| 2 | |
| 3 | namespace test0 { |
| 4 | struct D { ~D(); }; |
| 5 | |
| 6 | int f(bool b) { |
| 7 | if (b) { |
| 8 | D d; |
| 9 | goto end; |
| 10 | } |
| 11 | |
| 12 | end: |
| 13 | return 1; |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | namespace test1 { |
| 18 | struct C { C(); }; |
| 19 | |
| 20 | int f(bool b) { |
| 21 | if (b) |
| 22 | goto foo; // expected-error {{illegal goto into protected scope}} |
| 23 | C c; // expected-note {{jump bypasses variable initialization}} |
| 24 | foo: |
| 25 | return 1; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | namespace test2 { |
| 30 | struct C { C(); }; |
| 31 | |
| 32 | int f(void **ip) { |
| 33 | static void *ips[] = { &&lbl1, &&lbl2 }; |
| 34 | |
| 35 | C c; |
| 36 | goto *ip; |
| 37 | lbl1: |
| 38 | return 0; |
| 39 | lbl2: |
| 40 | return 1; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | namespace test3 { |
| 45 | struct C { C(); }; |
| 46 | |
| 47 | int f(void **ip) { |
| 48 | static void *ips[] = { &&lbl1, &&lbl2 }; |
| 49 | |
| 50 | goto *ip; |
| 51 | lbl1: { |
| 52 | C c; |
| 53 | return 0; |
| 54 | } |
| 55 | lbl2: |
| 56 | return 1; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | namespace test4 { |
| 61 | struct C { C(); }; |
| 62 | struct D { ~D(); }; |
| 63 | |
| 64 | int f(void **ip) { |
| 65 | static void *ips[] = { &&lbl1, &&lbl2 }; |
| 66 | |
| 67 | C c0; |
| 68 | |
John McCall | 95c225d | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 69 | goto *ip; // expected-error {{indirect goto might cross protected scopes}} |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 70 | C c1; // expected-note {{jump bypasses variable initialization}} |
| 71 | lbl1: // expected-note {{possible target of indirect goto}} |
| 72 | return 0; |
| 73 | lbl2: |
| 74 | return 1; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | namespace test5 { |
| 79 | struct C { C(); }; |
| 80 | struct D { ~D(); }; |
| 81 | |
| 82 | int f(void **ip) { |
| 83 | static void *ips[] = { &&lbl1, &&lbl2 }; |
| 84 | C c0; |
| 85 | |
| 86 | goto *ip; |
| 87 | lbl1: // expected-note {{possible target of indirect goto}} |
| 88 | return 0; |
| 89 | lbl2: |
| 90 | if (ip[1]) { |
| 91 | D d; // expected-note {{jump exits scope of variable with non-trivial destructor}} |
| 92 | ip += 2; |
John McCall | 95c225d | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 93 | goto *ip; // expected-error {{indirect goto might cross protected scopes}} |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 94 | } |
| 95 | return 1; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | namespace test6 { |
| 100 | struct C { C(); }; |
| 101 | |
| 102 | unsigned f(unsigned s0, unsigned s1, void **ip) { |
| 103 | static void *ips[] = { &&lbl1, &&lbl2, &&lbl3, &&lbl4 }; |
| 104 | C c0; |
| 105 | |
| 106 | goto *ip; |
| 107 | lbl1: |
| 108 | s0++; |
| 109 | goto *++ip; |
| 110 | lbl2: |
| 111 | s0 -= s1; |
| 112 | goto *++ip; |
| 113 | lbl3: { |
| 114 | unsigned tmp = s0; |
| 115 | s0 = s1; |
| 116 | s1 = tmp; |
| 117 | goto *++ip; |
| 118 | } |
| 119 | lbl4: |
| 120 | return s0; |
| 121 | } |
| 122 | } |
| 123 | |
John McCall | e46f62c | 2010-08-01 01:24:59 +0000 | [diff] [blame] | 124 | // C++0x says it's okay to skip non-trivial initializers on static |
| 125 | // locals, and we implement that in '03 as well. |
| 126 | namespace test7 { |
| 127 | struct C { C(); }; |
| 128 | |
| 129 | void test() { |
| 130 | goto foo; |
| 131 | static C c; |
| 132 | foo: |
| 133 | return; |
| 134 | } |
| 135 | } |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 136 | |
| 137 | // PR7789 |
| 138 | namespace test8 { |
| 139 | void test1(int c) { |
| 140 | switch (c) { |
| 141 | case 0: |
| 142 | int x = 56; // expected-note {{jump bypasses variable initialization}} |
| 143 | case 1: // expected-error {{switch case is in protected scope}} |
| 144 | x = 10; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void test2() { |
| 149 | goto l2; // expected-error {{goto into protected scope}} |
| 150 | l1: int x = 5; // expected-note {{jump bypasses variable initialization}} |
| 151 | l2: x++; |
| 152 | } |
| 153 | } |