array-init.c revision d35005ece906cca5238d75b031b8db7eac0ac6e1
1f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff// RUN: clang -parse-ast-check -pedantic %s
2f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
36f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroffstatic int x, y, z;
46f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroff
56f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroffstatic int ary[] = { x, y, z }; // expected-error{{initializer element is not constant}}
66f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroffint ary2[] = { x, y, z }; // expected-error{{initializer element is not constant}}
76f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroff
86f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroffextern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
96f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroff
10d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroffstatic int ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible types assigning 'char *' to 'int'}}
1138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
12f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroffvoid func() {
13f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  int x = 1;
14f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
15d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff  int xComputeSize[] = { 1, 3, 5 };
16f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
1738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff  int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
18f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
19d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff  int x4 = { 1, 2 }; // // expected-warning{{excess elements in array initializer}}
20f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
21f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  int y[4][3] = {
22f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff    { 1, 3, 5 },
23f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff    { 2, 4, 6 },
24f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff    { 3, 5, 7 },
25f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  };
26f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
27f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  int y2[4][3] = {
28f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff    1, 3, 5, 2, 4, 6, 3, 5, 7
29f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  };
30f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
31d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff  int y3[4][3] = {
32d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff    { 1, 3, 5 },
33d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff    { 2, 4, 6 },
34d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff    { 3, 5, 7 },
35d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff    { 4, 6, 8 },
36d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff    { 5 }, // expected-warning{{excess elements in array initializer}}
37d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff  };
38d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff
39f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  struct threeElements {
40f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff    int a,b,c;
41f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  } z = { 1 };
42f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
43f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  struct threeElements *p = 7; // expected-warning{{incompatible types assigning 'int' to 'struct threeElements *'}}
446f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroff
456f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroff  extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
4638374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
4738374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff  static int x2[3] = { 1.0, "abc" , 5.8 }; // expected-warning{{incompatible types assigning 'char *' to 'int'}}
48f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff}
49