1 // RUN: %clang_cc1 %s -verify=stock,c -fsyntax-only
2 // RUN: %clang_cc1 %s -verify=stock,cpp -fsyntax-only -x c++
3 // RUN: %clang_cc1 %s -verify=stock,cpp -fsyntax-only -fms-compatibility -x c++
4 // RUN: %clang_cc1 %s -verify=stock,c,gnu -fsyntax-only -Wgnu-flexible-array-union-member -Wgnu-empty-struct
5 // RUN: %clang_cc1 %s -verify=stock,c,microsoft -fsyntax-only -fms-compatibility -Wmicrosoft
7 // The test checks that an attempt to initialize union with flexible array
8 // member with an initializer list doesn't crash clang.
11 union { char x
[]; } r
= {0}; /* gnu-warning {{flexible array member 'x' in a union is a GNU extension}}
12 microsoft-warning {{flexible array member 'x' in a union is a Microsoft extension}}
18 char x
[]; /* gnu-warning {{flexible array member 'x' in a union is a GNU extension}}
19 microsoft-warning {{flexible array member 'x' in a union is a Microsoft extension}}
24 42, /* initializes "b" */
31 char x
[]; /* gnu-warning {{flexible array member 'x' in a union is a GNU extension}}
32 microsoft-warning {{flexible array member 'x' in a union is a Microsoft extension}}
40 /* Initialization of flexible array in a union is never allowed. */
45 char x
[]; /* gnu-warning {{flexible array member 'x' in a union is a GNU extension}}
46 microsoft-warning {{flexible array member 'x' in a union is a Microsoft extension}}
52 { 'c' }, /* c-warning {{excess elements in struct initializer}}
53 cpp-error {{excess elements in struct initializer}}
57 /* Initialization of flexible array in a union is never allowed. */
62 char x
[]; /* gnu-warning {{flexible array member 'x' in a union is a GNU extension}}
63 microsoft-warning {{flexible array member 'x' in a union is a Microsoft extension}}
64 stock-note {{initialized flexible array member 'x' is here}}
69 .b
= 13, /* stock-note {{previous initialization is here}} */
70 .x
= { 'c' }, /* stock-error {{initialization of flexible array member is not allowed}}
71 c-warning {{initializer overrides prior initialization of this subobject}}
72 cpp-error {{initializer partially overrides prior initialization of this subobject}}
76 /* Flexible array initialization always allowed when not in a union,
77 and when struct has another member.
92 .x
= { 'x', 'y', 'z' },
100 struct _flex_extension
{
101 char x
[]; /* gnu-warning {{flexible array member 'x' in otherwise empty struct is a GNU extension}}
102 microsoft-warning {{flexible array member 'x' in otherwise empty struct is a Microsoft extension}}
108 struct _flex_extensioni
{
109 char x
[]; /* gnu-warning {{flexible array member 'x' in otherwise empty struct is a GNU extension}}
110 microsoft-warning {{flexible array member 'x' in otherwise empty struct is a Microsoft extension}}
112 } flex_extensioni
= {
113 .x
= { 'x', 'y', 'z' },
116 struct already_hidden
{
121 struct { } __empty
; // gnu-warning {{empty struct is a GNU extension}}
127 struct still_zero_sized
{
128 struct { } __unused
; // gnu-warning {{empty struct is a GNU extension}}
136 char x
[]; /* gnu-warning {{flexible array member 'x' in a union is a GNU extension}}
137 microsoft-warning {{flexible array member 'x' in a union is a Microsoft extension}}
143 int x
[]; /* gnu-warning {{flexible array member 'x' in otherwise empty struct is a GNU extension}}
144 microsoft-warning {{flexible array member 'x' in otherwise empty struct is a Microsoft extension}}
149 short x
[]; /* gnu-warning {{flexible array member 'x' in a union is a GNU extension}}
150 microsoft-warning {{flexible array member 'x' in a union is a Microsoft extension}}
160 union { short x
[]; }; /* stock-warning-re {{field '' with variable sized type '{{.*}}' not at the end of a struct or class is a GNU extension}}
161 gnu-warning {{flexible array member 'x' in a union is a GNU extension}}
162 microsoft-warning {{flexible array member 'x' in a union is a Microsoft extension}}
165 } not_at_end
= {{}, 3};
167 struct _not_at_end_s
{
168 struct { int a
; short x
[]; }; /* stock-warning-re {{field '' with variable sized type '{{.*}}' not at the end of a struct or class is a GNU extension}} */
170 } not_at_end_s
= {{}, 3};
174 union { /* stock-warning-re {{field '' with variable sized type '{{.*}}' not at the end of a struct or class is a GNU extension}} */
175 short x
[]; /* stock-note {{initialized flexible array member 'x' is here}}
176 gnu-warning {{flexible array member 'x' in a union is a GNU extension}}
177 microsoft-warning {{flexible array member 'x' in a union is a Microsoft extension}}
184 {5}, /* stock-error {{initialization of flexible array member is not allowed}} */
188 // expected-no-diagnostics