blob: fa37d10d070b91cd79462d76b0d1fc251825f0a9 [file] [log] [blame]
Mike Stump0978af82010-01-23 20:12:18 +00001// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -std=gnu99 %s -Wno-unreachable-code
Eli Friedmanf69d09b2009-02-28 05:41:13 +00002
3int test1(int x) {
Richard Smith091405d2014-09-06 00:24:58 +00004 goto L; // expected-error{{cannot jump from this goto statement to its label}}
Chris Lattner07f62f12009-04-18 18:42:55 +00005 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 Friedmanf69d09b2009-02-28 05:41:13 +00007 L:
8 return sizeof a;
9}
Eli Friedmancba899f2009-02-28 06:22:14 +000010
11int test2(int x) {
Richard Smith091405d2014-09-06 00:24:58 +000012 goto L; // expected-error{{cannot jump from this goto statement to its label}}
Chris Lattner07f62f12009-04-18 18:42:55 +000013 typedef int a[x]; // expected-note {{jump bypasses initialization of VLA typedef}}
Eli Friedmancba899f2009-02-28 06:22:14 +000014 L:
15 return sizeof(a);
16}
17
18void test3clean(int*);
19
20int test3() {
Richard Smith091405d2014-09-06 00:24:58 +000021 goto L; // expected-error{{cannot jump from this goto statement to its label}}
John McCallef8b9b32010-05-12 01:15:36 +000022int a __attribute((cleanup(test3clean))); // expected-note {{jump bypasses initialization of variable with __attribute__((cleanup))}}
Chris Lattner9f3e7112009-04-18 07:54:11 +000023L:
Eli Friedmancba899f2009-02-28 06:22:14 +000024 return a;
25}
Steve Naroff6f842662009-04-15 16:58:41 +000026
27int test4(int x) {
Richard Smith091405d2014-09-06 00:24:58 +000028 goto L; // expected-error{{cannot jump from this goto statement to its label}}
Chris Lattner07f62f12009-04-18 18:42:55 +000029int a[x]; // expected-note {{jump bypasses initialization of variable length array}}
Chris Lattner9f3e7112009-04-18 07:54:11 +000030 test4(x);
31L:
32 return sizeof a;
Steve Naroff6f842662009-04-15 16:58:41 +000033}
Chris Lattner9f3e7112009-04-18 07:54:11 +000034
35int test5(int x) {
36 int a[x];
37 test5(x);
38 goto L; // Ok.
39L:
40 goto L; // Ok.
41 return sizeof a;
42}
43
Chris Lattner960cc522009-04-18 09:36:27 +000044int test6() {
45 // just plain invalid.
46 goto x; // expected-error {{use of undeclared label 'x'}}
47}
48
Chris Lattner7535f412009-04-18 19:42:37 +000049void test7(int x) {
Chris Lattner36dec992009-04-18 19:50:02 +000050 switch (x) {
Chris Lattner7535f412009-04-18 19:42:37 +000051 case 1: ;
52 int a[x]; // expected-note {{jump bypasses initialization of variable length array}}
Richard Smith091405d2014-09-06 00:24:58 +000053 case 2: // expected-error {{cannot jump from switch statement to this case label}}
Chris Lattner7535f412009-04-18 19:42:37 +000054 a[1] = 2;
55 break;
56 }
57}
58
Chris Lattnerfb5ef702009-04-18 22:42:18 +000059int test8(int x) {
Chris Lattnera0cfd6b2009-04-18 22:56:52 +000060 // For statement.
Richard Smith091405d2014-09-06 00:24:58 +000061 goto L2; // expected-error {{cannot jump from this goto statement to its label}}
Chris Lattnerfb5ef702009-04-18 22:42:18 +000062 for (int arr[x]; // expected-note {{jump bypasses initialization of variable length array}}
Chris Lattnera0cfd6b2009-04-18 22:56:52 +000063 ; ++x)
Chris Lattner19bd27f2009-04-18 23:07:55 +000064 L2:;
Chris Lattnera0cfd6b2009-04-18 22:56:52 +000065
66 // Statement expressions.
Richard Smith091405d2014-09-06 00:24:58 +000067 goto L3; // expected-error {{cannot jump from this goto statement to its label}}
Chris Lattnera0cfd6b2009-04-18 22:56:52 +000068 int Y = ({ int a[x]; // expected-note {{jump bypasses initialization of variable length array}}
69 L3: 4; });
Chris Lattnerfb5ef702009-04-18 22:42:18 +000070
Richard Smith091405d2014-09-06 00:24:58 +000071 goto L4; // expected-error {{cannot jump from this goto statement to its label}}
Chris Lattnerf7fcb512009-04-18 23:01:20 +000072 {
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 Lattner0bf2dd22009-04-19 01:05:26 +000086 if (x) goto L6; // ok
Chris Lattnerf7fcb512009-04-18 23:01:20 +000087 }
88
89 {
90 L7: ;// ok
91 int A[x], B = ({ if (x)
92 goto L7;
93 else
Richard Smith091405d2014-09-06 00:24:58 +000094 goto L8; // expected-error {{cannot jump from this goto statement to its label}}
Chris Lattnerf7fcb512009-04-18 23:01:20 +000095 4; }),
96 C[x]; // expected-note {{jump bypasses initialization of variable length array}}
97 L8:; // bad
98 }
99
Chris Lattner19bd27f2009-04-18 23:07:55 +0000100 {
101 L9: ;// ok
102 int A[({ if (x)
103 goto L9;
104 else
105 // FIXME:
Richard Smith091405d2014-09-06 00:24:58 +0000106 goto L10; // fixme-error {{cannot jump from this goto statement to its label}}
Chris Lattner19bd27f2009-04-18 23:07:55 +0000107 4; })];
108 L10:; // bad
109 }
110
111 {
112 // FIXME: Crashes goto checker.
113 //goto L11;// ok
114 //int A[({ L11: 4; })];
115 }
116
Chris Lattner0bf2dd22009-04-19 01:05:26 +0000117 {
118 goto L12;
119
120 int y = 4; // fixme-warn: skips initializer.
121 L12:
122 ;
123 }
Chris Lattnerf7fcb512009-04-18 23:01:20 +0000124
Chris Lattnera0cfd6b2009-04-18 22:56:52 +0000125 // Statement expressions 2.
Richard Smith091405d2014-09-06 00:24:58 +0000126 goto L1; // expected-error {{cannot jump from this goto statement to its label}}
Chris Lattnerfb5ef702009-04-18 22:42:18 +0000127 return x == ({
128 int a[x]; // expected-note {{jump bypasses initialization of variable length array}}
Chris Lattnera0cfd6b2009-04-18 22:56:52 +0000129 L1:
Chris Lattnerfb5ef702009-04-18 22:42:18 +0000130 42; });
131}
Chris Lattner0bf2dd22009-04-19 01:05:26 +0000132
133void test9(int n, void *P) {
134 int Y;
135 int Z = 4;
Richard Smith091405d2014-09-06 00:24:58 +0000136 goto *P; // expected-error {{cannot jump from this indirect goto statement to one of its possible targets}}
Chris Lattner0bf2dd22009-04-19 01:05:26 +0000137
138L2: ;
John McCallcf819ab2010-05-12 00:58:13 +0000139 int a[n]; // expected-note {{jump bypasses initialization of variable length array}}
Chris Lattner0bf2dd22009-04-19 01:05:26 +0000140
John McCallcf819ab2010-05-12 00:58:13 +0000141L3: // expected-note {{possible target of indirect goto}}
Chris Lattnerc6754052009-04-19 01:16:06 +0000142L4:
John McCallcf819ab2010-05-12 00:58:13 +0000143 goto *P;
Chris Lattnerc6754052009-04-19 01:16:06 +0000144 goto L3; // ok
145 goto L4; // ok
Chris Lattner0bf2dd22009-04-19 01:05:26 +0000146
147 void *Ptrs[] = {
John McCallcf819ab2010-05-12 00:58:13 +0000148 &&L2,
149 &&L3
Chris Lattner0bf2dd22009-04-19 01:05:26 +0000150 };
151}
152
Chris Lattner4be550e2009-04-19 04:48:07 +0000153void test10(int n, void *P) {
Richard Smith091405d2014-09-06 00:24:58 +0000154 goto L0; // expected-error {{cannot jump from this goto statement to its label}}
Chris Lattner4be550e2009-04-19 04:48:07 +0000155 typedef int A[n]; // expected-note {{jump bypasses initialization of VLA typedef}}
156L0:
157
Richard Smith091405d2014-09-06 00:24:58 +0000158 goto L1; // expected-error {{cannot jump from this goto statement to its label}}
Chris Lattner4be550e2009-04-19 04:48:07 +0000159 A b, c[10]; // expected-note 2 {{jump bypasses initialization of variable length array}}
160L1:
Richard Smith091405d2014-09-06 00:24:58 +0000161 goto L2; // expected-error {{cannot jump from this goto statement to its label}}
Chris Lattner4be550e2009-04-19 04:48:07 +0000162 A d[n]; // expected-note {{jump bypasses initialization of variable length array}}
163L2:
164 return;
165}
166
Chris Lattner45542ea2009-04-19 05:28:12 +0000167void 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}}
Richard Smith091405d2014-09-06 00:24:58 +0000174 case 4: // expected-error {{cannot jump from switch statement to this case label}}
Chris Lattner45542ea2009-04-19 05:28:12 +0000175 return;
176 }
177 };
178}
179
Chris Lattner9fecd742009-04-19 05:21:20 +0000180
Chris Lattnerde6240c2009-04-19 04:51:27 +0000181// TODO: When and if gotos are allowed in blocks, this should work.
Chris Lattner45542ea2009-04-19 05:28:12 +0000182void test12(int n) {
Chris Lattnerde6240c2009-04-19 04:51:27 +0000183 void *P = ^{
Mike Stump314825b2010-01-19 23:08:01 +0000184 goto L1;
Chris Lattnerde6240c2009-04-19 04:51:27 +0000185 L1:
Mike Stump314825b2010-01-19 23:08:01 +0000186 goto L2;
Chris Lattnerde6240c2009-04-19 04:51:27 +0000187 L2:
Richard Smith091405d2014-09-06 00:24:58 +0000188 goto L3; // expected-error {{cannot jump from this goto statement to its label}}
Mike Stump314825b2010-01-19 23:08:01 +0000189 int Arr[n]; // expected-note {{jump bypasses initialization of variable length array}}
Chris Lattnerde6240c2009-04-19 04:51:27 +0000190 L3:
Mike Stump314825b2010-01-19 23:08:01 +0000191 goto L4;
Chris Lattnerde6240c2009-04-19 04:51:27 +0000192 L4: return;
193 };
194}
195
John McCallcf819ab2010-05-12 00:58:13 +0000196void test13(int n, void *p) {
197 int vla[n];
198 goto *p;
199 a0: ;
200 static void *ps[] = { &&a0 };
201}
John McCall9de91602010-10-28 08:53:48 +0000202
203int 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.
220void test15(int n, void *pc) {
221 static const void *addrs[] = { &&L1, &&L2 };
222
Richard Smith091405d2014-09-06 00:24:58 +0000223 goto *pc; // expected-error {{cannot jump from this indirect goto statement to one of its possible targets}}
John McCall9de91602010-10-28 08:53:48 +0000224
225 L1:
226 {
227 char vla[n]; // expected-note {{jump bypasses initialization}}
228 L2: // expected-note {{possible target}}
229 vla[0] = 'a';
230 }
231}
Chris Lattner9ba479b2011-02-18 21:16:39 +0000232
233// rdar://9024687
234int test16(int [sizeof &&z]); // expected-error {{use of address-of-label extension outside of a function body}}