scope-check.c revision 02db31c62ae6c68b5cbbecd09acbbc09bc03d110
1// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -std=gnu99 %s -Wno-unreachable-code
2
3int test1(int x) {
4  goto L;    // expected-error{{illegal goto into protected scope}}
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}}
7  L:
8  return sizeof a;
9}
10
11int test2(int x) {
12  goto L;            // expected-error{{illegal goto into protected scope}}
13  typedef int a[x];  // expected-note {{jump bypasses initialization of VLA typedef}}
14  L:
15  return sizeof(a);
16}
17
18void test3clean(int*);
19
20int test3() {
21  goto L;            // expected-error{{illegal goto into protected scope}}
22int a __attribute((cleanup(test3clean))); // expected-note {{jump bypasses initialization of declaration with __attribute__((cleanup))}}
23L:
24  return a;
25}
26
27int test4(int x) {
28  goto L;       // expected-error{{illegal goto into protected scope}}
29int a[x];       // expected-note {{jump bypasses initialization of variable length array}}
30  test4(x);
31L:
32  return sizeof a;
33}
34
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
44int test6() {
45  // just plain invalid.
46  goto x;  // expected-error {{use of undeclared label 'x'}}
47}
48
49void test7(int x) {
50  switch (x) {
51  case 1: ;
52    int a[x];       // expected-note {{jump bypasses initialization of variable length array}}
53  case 2:           // expected-error {{illegal switch case into protected scope}}
54    a[1] = 2;
55    break;
56  }
57}
58
59int test8(int x) {
60  // For statement.
61  goto L2;     // expected-error {{illegal goto into protected scope}}
62  for (int arr[x];   // expected-note {{jump bypasses initialization of variable length array}}
63       ; ++x)
64    L2:;
65
66  // Statement expressions.
67  goto L3;   // expected-error {{illegal goto into protected scope}}
68  int Y = ({  int a[x];   // expected-note {{jump bypasses initialization of variable length array}}
69           L3: 4; });
70
71  goto L4; // expected-error {{illegal goto into protected scope}}
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.
86    if (x) goto L6; // ok
87  }
88
89  {
90  L7: ;// ok
91    int A[x], B = ({ if (x)
92                       goto L7;
93                     else
94                       goto L8;  // expected-error {{illegal goto into protected scope}}
95                     4; }),
96        C[x];   // expected-note {{jump bypasses initialization of variable length array}}
97  L8:; // bad
98  }
99
100  {
101  L9: ;// ok
102    int A[({ if (x)
103               goto L9;
104             else
105               // FIXME:
106               goto L10;  // fixme-error {{illegal goto into protected scope}}
107           4; })];
108  L10:; // bad
109  }
110
111  {
112    // FIXME: Crashes goto checker.
113    //goto L11;// ok
114    //int A[({   L11: 4; })];
115  }
116
117  {
118    goto L12;
119
120    int y = 4;   // fixme-warn: skips initializer.
121  L12:
122    ;
123  }
124
125  // Statement expressions 2.
126  goto L1;     // expected-error {{illegal goto into protected scope}}
127  return x == ({
128                 int a[x];   // expected-note {{jump bypasses initialization of variable length array}}
129               L1:
130                 42; });
131}
132
133void test9(int n, void *P) {
134  int Y;
135  int Z = 4;
136  goto *P;  // ok.
137
138L2: ;
139  int a[n]; // expected-note 2 {{jump bypasses initialization of variable length array}}
140
141L3:
142L4:
143  goto *P;  // expected-warning {{illegal indirect goto in protected scope, unknown effect on scopes}}
144  goto L3;  // ok
145  goto L4;  // ok
146
147  void *Ptrs[] = {
148    &&L2,   // Ok.
149    &&L3   // expected-warning {{address taken of label in protected scope, jump to it would have unknown effect on scope}}
150  };
151}
152
153void test10(int n, void *P) {
154  goto L0;     // expected-error {{illegal goto into protected scope}}
155  typedef int A[n];  // expected-note {{jump bypasses initialization of VLA typedef}}
156L0:
157
158  goto L1;      // expected-error {{illegal goto into protected scope}}
159  A b, c[10];        // expected-note 2 {{jump bypasses initialization of variable length array}}
160L1:
161  goto L2;     // expected-error {{illegal goto into protected scope}}
162  A d[n];      // expected-note {{jump bypasses initialization of variable length array}}
163L2:
164  return;
165}
166
167void 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}}
174    case 4:       // expected-error {{illegal switch case into protected scope}}
175      return;
176    }
177  };
178}
179
180
181// TODO: When and if gotos are allowed in blocks, this should work.
182void test12(int n) {
183  void *P = ^{
184    goto L1;
185  L1:
186    goto L2;
187  L2:
188    goto L3;    // expected-error {{illegal goto into protected scope}}
189    int Arr[n]; // expected-note {{jump bypasses initialization of variable length array}}
190  L3:
191    goto L4;
192  L4: return;
193  };
194}
195
196