flexible-array-init.c 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
  2. struct one {
  3. int a;
  4. int values[]; // expected-note 4{{initialized flexible array member 'values' is here}}
  5. } x = {5, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}}
  6. struct one x2 = { 5, 1, 2, 3 }; // expected-warning{{flexible array initialization is a GNU extension}}
  7. void test() {
  8. struct one x3 = {5, {1, 2, 3}}; // expected-error{{initialization of flexible array member is not allowed}}
  9. struct one x3a = { 5 };
  10. struct one x3b = { .a = 5 };
  11. struct one x3c = { 5, {} }; // expected-warning{{use of GNU empty initializer extension}} \
  12. // expected-warning{{flexible array initialization is a GNU extension}} \
  13. // expected-warning{{zero size arrays are an extension}}
  14. }
  15. struct foo {
  16. int x;
  17. int y[]; // expected-note 8 {{initialized flexible array member 'y' is here}}
  18. };
  19. struct bar { struct foo z; }; // expected-warning {{'z' may not be nested in a struct due to flexible array member}}
  20. struct foo a = { 1, { 2, 3, 4 } }; // expected-warning{{flexible array initialization is a GNU extension}}
  21. struct bar b = { { 1, { 2, 3, 4 } } }; // expected-error{{initialization of flexible array member is not allowed}}
  22. struct bar c = { { 1, { } } }; // // expected-warning{{flexible array initialization is a GNU extension}} \
  23. // expected-warning{{use of GNU empty initializer extension}} \
  24. // expected-warning{{zero size arrays are an extension}}
  25. struct foo d[1] = { { 1, { 2, 3, 4 } } }; // expected-warning{{'struct foo' may not be used as an array element due to flexible array member}} \
  26. // expected-error{{initialization of flexible array member is not allowed}}
  27. struct foo desig_foo = { .y = {2, 3, 4} }; // expected-warning{{flexible array initialization is a GNU extension}}
  28. struct bar desig_bar = { .z.y = { } }; // expected-warning{{use of GNU empty initializer extension}} \
  29. // expected-warning{{zero size arrays are an extension}} \
  30. // expected-warning{{flexible array initialization is a GNU extension}}
  31. struct bar desig_bar2 = { .z.y = { 2, 3, 4} }; // expected-error{{initialization of flexible array member is not allowed}}
  32. struct foo design_foo2 = { .y = 2 }; // expected-error{{flexible array requires brace-enclosed initializer}}
  33. struct point {
  34. int x, y;
  35. };
  36. struct polygon {
  37. int numpoints;
  38. struct point points[]; // expected-note{{initialized flexible array member 'points' is here}}
  39. };
  40. struct polygon poly = {
  41. .points[2] = { 1, 2} }; // expected-error{{designator into flexible array member subobject}}
  42. // PR3540
  43. struct X {
  44. int a;
  45. int b;
  46. char data[];
  47. };
  48. struct Y {
  49. int a:4;
  50. int b:4;
  51. int c;
  52. int d;
  53. int e;
  54. struct X xs[]; // expected-warning{{'struct X' may not be used as an array element due to flexible array member}}
  55. };
  56. // PR8217
  57. struct PR8217a {
  58. int i;
  59. char v[]; // expected-note 2 {{initialized flexible array member 'v' is here}}
  60. };
  61. void PR8217() {
  62. struct PR8217a foo1 = { .i = 0, .v = "foo" }; // expected-error {{initialization of flexible array member is not allowed}}
  63. struct PR8217a foo2 = { .i = 0 };
  64. struct PR8217a foo3 = { .i = 0, .v = { 'b', 'a', 'r', '\0' } }; // expected-error {{initialization of flexible array member is not allowed}}
  65. struct PR8217a bar;
  66. }
  67. typedef struct PR10648 {
  68. unsigned long n;
  69. int v[]; // expected-note {{initialized flexible array member 'v' is here}}
  70. } PR10648;
  71. int f10648() {
  72. return (PR10648){2, {3, 4}}.v[1]; // expected-error {{initialization of flexible array member is not allowed}}
  73. }
  74. struct FlexWithUnnamedBitfield { int : 10; int x; int y[]; }; // expected-note {{initialized flexible array member 'y' is here}}
  75. void TestFlexWithUnnamedBitfield() {
  76. struct FlexWithUnnamedBitfield x = {10, {3}}; // expected-error {{initialization of flexible array member is not allowed}}
  77. }