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