scope-check.c 6.1 KB

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