array-init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // RUN: %clang_cc1 -std=gnu99 -fsyntax-only -pedantic -verify %s
  2. // RUN: %clang_cc1 -std=gnu99 -fsyntax-only -Wgnu -Wc11-extensions -verify %s
  3. // REQUIRES: LP64
  4. extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
  5. static int x, y, z;
  6. static int ary[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
  7. int ary2[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
  8. extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
  9. static long ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
  10. void func() {
  11. int x = 1;
  12. typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
  13. int xComputeSize[] = { 1, 3, 5 };
  14. int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
  15. int x4 = { 1, 2 }; // expected-warning{{excess elements in scalar initializer}}
  16. int y[4][3] = {
  17. { 1, 3, 5 },
  18. { 2, 4, 6 },
  19. { 3, 5, 7 },
  20. };
  21. int y2[4][3] = {
  22. 1, 3, 5, 2, 4, 6, 3, 5, 7
  23. };
  24. int y3[4][3] = {
  25. { 1, 3, 5 },
  26. { 2, 4, 6 },
  27. { 3, 5, 7 },
  28. { 4, 6, 8 },
  29. { 5 }, // expected-warning{{excess elements in array initializer}}
  30. };
  31. struct threeElements {
  32. int a,b,c;
  33. } z = { 1 };
  34. struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}}
  35. extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
  36. static long x2[3] = { 1.0,
  37. "abc", // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
  38. 5.8 }; // expected-warning {{implicit conversion from 'double' to 'long' changes value from 5.8 to 5}}
  39. }
  40. void test() {
  41. int y1[3] = {
  42. { 1, 2, 3 } // expected-warning{{excess elements in scalar initializer}}
  43. };
  44. int y3[4][3] = {
  45. { 1, 3, 5 },
  46. { 2, 4, 6 },
  47. { 3, 5, 7 },
  48. { 4, 6, 8 },
  49. { }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
  50. };
  51. int y4[4][3] = {
  52. { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
  53. { 4, 6 },
  54. { 3, 5, 7 },
  55. { 4, 6, 8 },
  56. };
  57. }
  58. void allLegalAndSynonymous() {
  59. short q[4][3][2] = {
  60. { 1 },
  61. { 2, 3 },
  62. { 4, 5, 6 }
  63. };
  64. short q2[4][3][2] = {
  65. { 1, 0, 0, 0, 0, 0 },
  66. { 2, 3, 0, 0, 0, 0 },
  67. { 4, 5, 6 }
  68. };
  69. short q3[4][3][2] = {
  70. {
  71. { 1 },
  72. },
  73. {
  74. { 2, 3 },
  75. },
  76. {
  77. { 4, 5 },
  78. { 6 },
  79. },
  80. };
  81. }
  82. void legal() {
  83. short q[][3][2] = {
  84. { 1 },
  85. { 2, 3 },
  86. { 4, 5, 6 }
  87. };
  88. int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1];
  89. }
  90. unsigned char asso_values[] = { 34 };
  91. int legal2() {
  92. return asso_values[0];
  93. }
  94. void illegal() {
  95. short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}}
  96. { 1, 0, 0, 0, 0, 0 },
  97. { 2, 3, 0, 0, 0, 0 },
  98. { 4, 5, 6 }
  99. };
  100. short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}}
  101. {
  102. { 1 },
  103. },
  104. {
  105. { 2, 3 },
  106. },
  107. {
  108. { 4, 5 },
  109. { 6 },
  110. },
  111. };
  112. int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}}
  113. }
  114. typedef int AryT[];
  115. void testTypedef()
  116. {
  117. AryT a = { 1, 2 }, b = { 3, 4, 5 };
  118. int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1];
  119. int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1];
  120. }
  121. static char const xx[] = "test";
  122. int xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1];
  123. static char const yy[5] = "test";
  124. static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
  125. void charArrays() {
  126. static char const test[] = "test";
  127. int test_sizecheck[(sizeof(test) / sizeof(char)) == 5? 1 : -1];
  128. static char const test2[] = { "weird stuff" };
  129. static char const test3[] = { "test", "excess stuff" }; // expected-warning{{excess elements in char array initializer}}
  130. char* cp[] = { "Hello" };
  131. char c[] = { "Hello" };
  132. int l[sizeof(c) == 6 ? 1 : -1];
  133. int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [7]'}}
  134. char c2[] = { "Hello", "Good bye" }; //expected-warning{{excess elements in char array initializer}}
  135. int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [6]'}}
  136. char c3[5] = { "Hello" };
  137. char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
  138. int i3[] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
  139. }
  140. void variableArrayInit() {
  141. int a = 4;
  142. char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}}
  143. int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
  144. }
  145. // Pure array tests
  146. float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
  147. float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
  148. char r3[][5] = {1,2,3,4,5,6};
  149. int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];
  150. char r3_2[sizeof r3 == 10 ? 1 : -1];
  151. float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
  152. char r5[][5] = {"aa", "bbb", "ccccc"};
  153. char r6[sizeof r5 == 15 ? 1 : -1];
  154. const char r7[] = "zxcv";
  155. char r8[5] = "5char";
  156. char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
  157. unsigned char r10[] = __extension__ (_Generic(0, int: (__extension__ "foo" )));
  158. int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
  159. // Some struct tests
  160. void autoStructTest() {
  161. struct s1 {char a; char b;} t1;
  162. struct s2 {struct s1 c;} t2 = { t1 };
  163. // The following is a less than great diagnostic (though it's on par with EDG).
  164. struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}}
  165. int t4[sizeof t3 == 6 ? 1 : -1];
  166. }
  167. struct foo { int z; } w;
  168. int bar (void) {
  169. struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
  170. return z.z;
  171. }
  172. struct s3 {void (*a)(void);} t5 = {autoStructTest};
  173. struct {int a; int b[];} t6 = {1, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}} \
  174. // expected-note{{initialized flexible array member 'b' is here}}
  175. union {char a; int b;} t7[] = {1, 2, 3};
  176. int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
  177. struct bittest{int : 31, a, :21, :12, b;};
  178. struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
  179. // Not completely sure what should happen here...
  180. int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
  181. int u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}}
  182. // PR2362
  183. void varArray() {
  184. int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
  185. }
  186. // PR2151
  187. void emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct is a GNU extension}} \
  188. // expected-error{{initializer for aggregate with no elements}}
  189. void noNamedInit() {
  190. struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}} \
  191. // expected-warning {{struct without named members is a GNU extension}}
  192. }
  193. struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
  194. int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
  195. // ptrs are constant
  196. struct soft_segment_descriptor {
  197. long ssd_base;
  198. };
  199. static int dblfault_tss;
  200. union uniao { int ola; } xpto[1];
  201. struct soft_segment_descriptor gdt_segs[] = {
  202. {(long) &dblfault_tss},
  203. { (long)xpto},
  204. };
  205. static void sppp_ipv6cp_up();
  206. const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \
  207. // expected-warning{{excess elements in struct initializer}}
  208. struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}
  209. typedef struct _Matrix Matrix;
  210. void test_matrix() {
  211. const Matrix mat1 = {
  212. { { 1.0f, 2.0f, 3.0f, 4.0f,
  213. 5.0f, 6.0f, 7.0f, 8.0f,
  214. 9.0f, 10.0f, 11.0f, 12.0f,
  215. 13.0f, 14.0f, 15.0f, 16.0f } }
  216. };
  217. const Matrix mat2 = {
  218. 1.0f, 2.0f, 3.0f, 4.0f,
  219. 5.0f, 6.0f, 7.0f, 8.0f,
  220. 9.0f, 10.0f, 11.0f, 12.0f,
  221. 13.0f, 14.0f, 15.0f, 16.0f
  222. };
  223. }
  224. char badchararray[1] = { badchararray[0], "asdf" }; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}}
  225. // Test the GNU extension for initializing an array from an array
  226. // compound literal. PR9261.
  227. typedef int int5[5];
  228. int a1[5] = (int[]){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int [5]' from a compound literal of type 'int [5]' is a GNU extension}}
  229. int a2[5] = (int[5]){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int [5]' from a compound literal of type 'int [5]' is a GNU extension}}
  230. int a3[] = ((int[]){1, 2, 3, 4, 5}); // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int [5]' is a GNU extension}}
  231. int a4[] = (int[5]){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int [5]' is a GNU extension}}
  232. int a5[] = (int5){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int5' (aka 'int [5]') is a GNU extension}}
  233. int a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}}
  234. int nonconst_value();
  235. int a7[5] = (int[5]){ 1,
  236. 2,
  237. 3,
  238. 4,
  239. nonconst_value() // expected-error{{initializer element is not a compile-time constant}}
  240. };
  241. // <rdar://problem/10636946>
  242. __attribute__((weak)) const unsigned int test10_bound = 10;
  243. char test10_global[test10_bound]; // expected-error {{variable length array declaration not allowed at file scope}}
  244. void test10() {
  245. char test10_local[test10_bound] = "help"; // expected-error {{variable-sized object may not be initialized}}
  246. }