Mike Stump | 4617191 | 2010-01-23 20:12:18 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -fblocks -std=gnu99 %s -Wno-unreachable-code |
Eli Friedman | 8f17b66 | 2009-02-28 05:41:13 +0000 | [diff] [blame] | 2 | |
| 3 | int test1(int x) { |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 4 | goto L; // expected-error{{goto into protected scope}} |
Chris Lattner | 5ce71c9 | 2009-04-18 18:42:55 +0000 | [diff] [blame] | 5 | int a[x]; // expected-note {{jump bypasses initialization of variable length array}} |
| 6 | int b[x]; // expected-note {{jump bypasses initialization of variable length array}} |
Eli Friedman | 8f17b66 | 2009-02-28 05:41:13 +0000 | [diff] [blame] | 7 | L: |
| 8 | return sizeof a; |
| 9 | } |
Eli Friedman | 709fa15 | 2009-02-28 06:22:14 +0000 | [diff] [blame] | 10 | |
| 11 | int test2(int x) { |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 12 | goto L; // expected-error{{goto into protected scope}} |
Chris Lattner | 5ce71c9 | 2009-04-18 18:42:55 +0000 | [diff] [blame] | 13 | typedef int a[x]; // expected-note {{jump bypasses initialization of VLA typedef}} |
Eli Friedman | 709fa15 | 2009-02-28 06:22:14 +0000 | [diff] [blame] | 14 | L: |
| 15 | return sizeof(a); |
| 16 | } |
| 17 | |
| 18 | void test3clean(int*); |
| 19 | |
| 20 | int test3() { |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 21 | goto L; // expected-error{{goto into protected scope}} |
John McCall | ebc2fb2 | 2010-05-12 01:15:36 +0000 | [diff] [blame] | 22 | int a __attribute((cleanup(test3clean))); // expected-note {{jump bypasses initialization of variable with __attribute__((cleanup))}} |
Chris Lattner | fd0c0cf | 2009-04-18 07:54:11 +0000 | [diff] [blame] | 23 | L: |
Eli Friedman | 709fa15 | 2009-02-28 06:22:14 +0000 | [diff] [blame] | 24 | return a; |
| 25 | } |
Steve Naroff | 1b6823d | 2009-04-15 16:58:41 +0000 | [diff] [blame] | 26 | |
| 27 | int test4(int x) { |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 28 | goto L; // expected-error{{goto into protected scope}} |
Chris Lattner | 5ce71c9 | 2009-04-18 18:42:55 +0000 | [diff] [blame] | 29 | int a[x]; // expected-note {{jump bypasses initialization of variable length array}} |
Chris Lattner | fd0c0cf | 2009-04-18 07:54:11 +0000 | [diff] [blame] | 30 | test4(x); |
| 31 | L: |
| 32 | return sizeof a; |
Steve Naroff | 1b6823d | 2009-04-15 16:58:41 +0000 | [diff] [blame] | 33 | } |
Chris Lattner | fd0c0cf | 2009-04-18 07:54:11 +0000 | [diff] [blame] | 34 | |
| 35 | int test5(int x) { |
| 36 | int a[x]; |
| 37 | test5(x); |
| 38 | goto L; // Ok. |
| 39 | L: |
| 40 | goto L; // Ok. |
| 41 | return sizeof a; |
| 42 | } |
| 43 | |
Chris Lattner | a5251fc | 2009-04-18 09:36:27 +0000 | [diff] [blame] | 44 | int test6() { |
| 45 | // just plain invalid. |
| 46 | goto x; // expected-error {{use of undeclared label 'x'}} |
| 47 | } |
| 48 | |
Chris Lattner | 366920a | 2009-04-18 19:42:37 +0000 | [diff] [blame] | 49 | void test7(int x) { |
Chris Lattner | a9768b7 | 2009-04-18 19:50:02 +0000 | [diff] [blame] | 50 | switch (x) { |
Chris Lattner | 366920a | 2009-04-18 19:42:37 +0000 | [diff] [blame] | 51 | case 1: ; |
| 52 | int a[x]; // expected-note {{jump bypasses initialization of variable length array}} |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 53 | case 2: // expected-error {{switch case is in protected scope}} |
Chris Lattner | 366920a | 2009-04-18 19:42:37 +0000 | [diff] [blame] | 54 | a[1] = 2; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | |
Chris Lattner | 35562d1 | 2009-04-18 22:42:18 +0000 | [diff] [blame] | 59 | int test8(int x) { |
Chris Lattner | 5b40e0c | 2009-04-18 22:56:52 +0000 | [diff] [blame] | 60 | // For statement. |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 61 | goto L2; // expected-error {{goto into protected scope}} |
Chris Lattner | 35562d1 | 2009-04-18 22:42:18 +0000 | [diff] [blame] | 62 | for (int arr[x]; // expected-note {{jump bypasses initialization of variable length array}} |
Chris Lattner | 5b40e0c | 2009-04-18 22:56:52 +0000 | [diff] [blame] | 63 | ; ++x) |
Chris Lattner | c78476b | 2009-04-18 23:07:55 +0000 | [diff] [blame] | 64 | L2:; |
Chris Lattner | 5b40e0c | 2009-04-18 22:56:52 +0000 | [diff] [blame] | 65 | |
| 66 | // Statement expressions. |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 67 | goto L3; // expected-error {{goto into protected scope}} |
Chris Lattner | 5b40e0c | 2009-04-18 22:56:52 +0000 | [diff] [blame] | 68 | int Y = ({ int a[x]; // expected-note {{jump bypasses initialization of variable length array}} |
| 69 | L3: 4; }); |
Chris Lattner | 35562d1 | 2009-04-18 22:42:18 +0000 | [diff] [blame] | 70 | |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 71 | goto L4; // expected-error {{goto into protected scope}} |
Chris Lattner | 2b7b2ca | 2009-04-18 23:01:20 +0000 | [diff] [blame] | 72 | { |
| 73 | int A[x], // expected-note {{jump bypasses initialization of variable length array}} |
| 74 | B[x]; // expected-note {{jump bypasses initialization of variable length array}} |
| 75 | L4: ; |
| 76 | } |
| 77 | |
| 78 | { |
| 79 | L5: ;// ok |
| 80 | int A[x], B = ({ if (x) |
| 81 | goto L5; |
| 82 | else |
| 83 | goto L6; |
| 84 | 4; }); |
| 85 | L6:; // ok. |
Chris Lattner | 04ea2b6 | 2009-04-19 01:05:26 +0000 | [diff] [blame] | 86 | if (x) goto L6; // ok |
Chris Lattner | 2b7b2ca | 2009-04-18 23:01:20 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | { |
| 90 | L7: ;// ok |
| 91 | int A[x], B = ({ if (x) |
| 92 | goto L7; |
| 93 | else |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 94 | goto L8; // expected-error {{goto into protected scope}} |
Chris Lattner | 2b7b2ca | 2009-04-18 23:01:20 +0000 | [diff] [blame] | 95 | 4; }), |
| 96 | C[x]; // expected-note {{jump bypasses initialization of variable length array}} |
| 97 | L8:; // bad |
| 98 | } |
| 99 | |
Chris Lattner | c78476b | 2009-04-18 23:07:55 +0000 | [diff] [blame] | 100 | { |
| 101 | L9: ;// ok |
| 102 | int A[({ if (x) |
| 103 | goto L9; |
| 104 | else |
| 105 | // FIXME: |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 106 | goto L10; // fixme-error {{goto into protected scope}} |
Chris Lattner | c78476b | 2009-04-18 23:07:55 +0000 | [diff] [blame] | 107 | 4; })]; |
| 108 | L10:; // bad |
| 109 | } |
| 110 | |
| 111 | { |
| 112 | // FIXME: Crashes goto checker. |
| 113 | //goto L11;// ok |
| 114 | //int A[({ L11: 4; })]; |
| 115 | } |
| 116 | |
Chris Lattner | 04ea2b6 | 2009-04-19 01:05:26 +0000 | [diff] [blame] | 117 | { |
| 118 | goto L12; |
| 119 | |
| 120 | int y = 4; // fixme-warn: skips initializer. |
| 121 | L12: |
| 122 | ; |
| 123 | } |
Chris Lattner | 2b7b2ca | 2009-04-18 23:01:20 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 5b40e0c | 2009-04-18 22:56:52 +0000 | [diff] [blame] | 125 | // Statement expressions 2. |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 126 | goto L1; // expected-error {{goto into protected scope}} |
Chris Lattner | 35562d1 | 2009-04-18 22:42:18 +0000 | [diff] [blame] | 127 | return x == ({ |
| 128 | int a[x]; // expected-note {{jump bypasses initialization of variable length array}} |
Chris Lattner | 5b40e0c | 2009-04-18 22:56:52 +0000 | [diff] [blame] | 129 | L1: |
Chris Lattner | 35562d1 | 2009-04-18 22:42:18 +0000 | [diff] [blame] | 130 | 42; }); |
| 131 | } |
Chris Lattner | 04ea2b6 | 2009-04-19 01:05:26 +0000 | [diff] [blame] | 132 | |
| 133 | void test9(int n, void *P) { |
| 134 | int Y; |
| 135 | int Z = 4; |
John McCall | 95c225d | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 136 | goto *P; // expected-error {{indirect goto might cross protected scopes}} |
Chris Lattner | 04ea2b6 | 2009-04-19 01:05:26 +0000 | [diff] [blame] | 137 | |
| 138 | L2: ; |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 139 | int a[n]; // expected-note {{jump bypasses initialization of variable length array}} |
Chris Lattner | 04ea2b6 | 2009-04-19 01:05:26 +0000 | [diff] [blame] | 140 | |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 141 | L3: // expected-note {{possible target of indirect goto}} |
Chris Lattner | b5cf1ea | 2009-04-19 01:16:06 +0000 | [diff] [blame] | 142 | L4: |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 143 | goto *P; |
Chris Lattner | b5cf1ea | 2009-04-19 01:16:06 +0000 | [diff] [blame] | 144 | goto L3; // ok |
| 145 | goto L4; // ok |
Chris Lattner | 04ea2b6 | 2009-04-19 01:05:26 +0000 | [diff] [blame] | 146 | |
| 147 | void *Ptrs[] = { |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 148 | &&L2, |
| 149 | &&L3 |
Chris Lattner | 04ea2b6 | 2009-04-19 01:05:26 +0000 | [diff] [blame] | 150 | }; |
| 151 | } |
| 152 | |
Chris Lattner | 3037a82 | 2009-04-19 04:48:07 +0000 | [diff] [blame] | 153 | void test10(int n, void *P) { |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 154 | goto L0; // expected-error {{goto into protected scope}} |
Chris Lattner | 3037a82 | 2009-04-19 04:48:07 +0000 | [diff] [blame] | 155 | typedef int A[n]; // expected-note {{jump bypasses initialization of VLA typedef}} |
| 156 | L0: |
| 157 | |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 158 | goto L1; // expected-error {{goto into protected scope}} |
Chris Lattner | 3037a82 | 2009-04-19 04:48:07 +0000 | [diff] [blame] | 159 | A b, c[10]; // expected-note 2 {{jump bypasses initialization of variable length array}} |
| 160 | L1: |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 161 | goto L2; // expected-error {{goto into protected scope}} |
Chris Lattner | 3037a82 | 2009-04-19 04:48:07 +0000 | [diff] [blame] | 162 | A d[n]; // expected-note {{jump bypasses initialization of variable length array}} |
| 163 | L2: |
| 164 | return; |
| 165 | } |
| 166 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 167 | void test11(int n) { |
| 168 | void *P = ^{ |
| 169 | switch (n) { |
| 170 | case 1:; |
| 171 | case 2: |
| 172 | case 3:; |
| 173 | int Arr[n]; // expected-note {{jump bypasses initialization of variable length array}} |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 174 | case 4: // expected-error {{switch case is in protected scope}} |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | }; |
| 178 | } |
| 179 | |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 180 | |
Chris Lattner | deae3a7 | 2009-04-19 04:51:27 +0000 | [diff] [blame] | 181 | // TODO: When and if gotos are allowed in blocks, this should work. |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 182 | void test12(int n) { |
Chris Lattner | deae3a7 | 2009-04-19 04:51:27 +0000 | [diff] [blame] | 183 | void *P = ^{ |
Mike Stump | a3899eb | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 184 | goto L1; |
Chris Lattner | deae3a7 | 2009-04-19 04:51:27 +0000 | [diff] [blame] | 185 | L1: |
Mike Stump | a3899eb | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 186 | goto L2; |
Chris Lattner | deae3a7 | 2009-04-19 04:51:27 +0000 | [diff] [blame] | 187 | L2: |
John McCall | 97ba481 | 2010-08-02 23:33:14 +0000 | [diff] [blame] | 188 | goto L3; // expected-error {{goto into protected scope}} |
Mike Stump | a3899eb | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 189 | int Arr[n]; // expected-note {{jump bypasses initialization of variable length array}} |
Chris Lattner | deae3a7 | 2009-04-19 04:51:27 +0000 | [diff] [blame] | 190 | L3: |
Mike Stump | a3899eb | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 191 | goto L4; |
Chris Lattner | deae3a7 | 2009-04-19 04:51:27 +0000 | [diff] [blame] | 192 | L4: return; |
| 193 | }; |
| 194 | } |
| 195 | |
John McCall | ddb0b4d | 2010-05-12 00:58:13 +0000 | [diff] [blame] | 196 | void test13(int n, void *p) { |
| 197 | int vla[n]; |
| 198 | goto *p; |
| 199 | a0: ; |
| 200 | static void *ps[] = { &&a0 }; |
| 201 | } |
John McCall | 95c225d | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 202 | |
| 203 | int test14(int n) { |
| 204 | static void *ps[] = { &&a0, &&a1 }; |
| 205 | if (n < 0) |
| 206 | goto *&&a0; |
| 207 | |
| 208 | if (n > 0) { |
| 209 | int vla[n]; |
| 210 | a1: |
| 211 | vla[n-1] = 0; |
| 212 | } |
| 213 | a0: |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | // PR8473: IR gen can't deal with indirect gotos past VLA |
| 219 | // initialization, so that really needs to be a hard error. |
| 220 | void test15(int n, void *pc) { |
| 221 | static const void *addrs[] = { &&L1, &&L2 }; |
| 222 | |
| 223 | goto *pc; // expected-error {{indirect goto might cross protected scope}} |
| 224 | |
| 225 | L1: |
| 226 | { |
| 227 | char vla[n]; // expected-note {{jump bypasses initialization}} |
| 228 | L2: // expected-note {{possible target}} |
| 229 | vla[0] = 'a'; |
| 230 | } |
| 231 | } |