aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/ext/flexary18.C
blob: 4ab864d991d5d168a0c872285093c497c3e082db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// PR c++/71912 - [6/7 regression] flexible array in struct in union rejected
// { dg-do compile }
// { dg-additional-options "-Wpedantic -Wno-error=pedantic" }

#if __cplusplus

namespace pr71912 {

#endif

struct foo {
  int a;
  char s[];                             // { dg-message "array member .char pr71912::foo::s \\\[\\\]. declared here" }
};

struct bar {
  double d;
  char t[];
};

struct baz {
  union {
    struct foo f;
    struct bar b;
  }
  // The definition of struct foo is fine but the use of struct foo
  // in the definition of u below is what's invalid and must be clearly
  // diagnosed.
    u;                                  // { dg-warning "invalid use of .struct pr71912::foo. with a flexible array member in .struct pr71912::baz." }
};

struct xyyzy {
  union {
    struct {
      int a;
      char s[];                         // { dg-message "declared here" }
    } f;
    struct {
      double d;
      char t[];
    } b;
  } u;                                  // { dg-warning "invalid use" }
};

struct baz b;
struct xyyzy x;

#if __cplusplus

}

#endif

// The following definitions aren't strictly valid but, like those above,
// are accepted for compatibility with GCC (in C mode).  They are benign
// in that the flexible array member is at the highest offset within
// the outermost type and doesn't overlap with other members except for
// those of the union.
union UnionStruct1 {
  struct { int n1, a[]; } s;
  int n2;
};

union UnionStruct2 {
  struct { int n1, a1[]; } s1;
  struct { int n2, a2[]; } s2;
  int n3;
};

union UnionStruct3 {
  struct { int n1, a1[]; } s1;
  struct { double n2, a2[]; } s2;
  char n3;
};

union UnionStruct4 {
  struct { int n1, a1[]; } s1;
  struct { struct { int n2, a2[]; } s2; } s3;
  char n3;
};

union UnionStruct5 {
  struct { struct { int n1, a1[]; } s1; } s2;   // { dg-warning "invalid use" }
  struct { double n2, a2[]; } s3;
  char n3;
};

union UnionStruct6 {
  struct { struct { int n1, a1[]; } s1; } s2;   // { dg-warning "invalid use" }
  struct { struct { int n2, a2[]; } s3; } s4;
  char n3;
};

union UnionStruct7 {
  struct { int n1, a1[]; } s1;
  struct { double n2, a2[]; } s2;
  struct { struct { int n3, a3[]; } s3; } s4;
};

union UnionStruct8 {
  struct { int n1, a1[]; } s1;
  struct { struct { int n2, a2[]; } s2; } s3;
  struct { struct { int n3, a3[]; } s4; } s5;
};

union UnionStruct9 {
  struct { struct { int n1, a1[]; } s1; } s2;   // { dg-warning "invalid use" }
  struct { struct { int n2, a2[]; } s3; } s4;
  struct { struct { int n3, a3[]; } s5; } s6;
};

struct StructUnion1 {
  union {
    struct { int n1, a1[]; } s1;        // { dg-message "declared here" }
    struct { double n2, a2[]; } s2;
    char n3;
  } u;                                  // { dg-warning "invalid use" }
};

// The following are invalid and rejected.
struct StructUnion2 {
  union {
    struct { int n1, a1[]; } s1;        // { dg-error "not at end" }
  } u;
  char n3;                              // { dg-message "next member" }
};

struct StructUnion3 {
  union {
    struct { int n1, a1[]; } s1;        // { dg-error "not at end" }
    struct { double n2, a2[]; } s2;
  } u;
  char n3;                              // { dg-message "next member" }
};

struct StructUnion4 {
  union {
    struct { int n1, a1[]; } s1;        // { dg-error "not at end" }
  } u1;
  union {
    struct { double n2, a2[]; } s2;
  } u2;                                 // { dg-message "next member" }
};

struct StructUnion5 {
  union {
    union {
      struct { int n1, a1[]; } s1;      // { dg-message "declared here" }
    } u1;
    union { struct { int n2, a2[]; } s2; } u2;
  } u;                                  // { dg-warning "invalid use" }
};

struct StructUnion6 {
  union {
    struct { int n1, a1[]; } s1;        // { dg-message "declared here" }
    union { struct { int n2, a2[]; } s2; } u2;
  } u;                                  // { dg-warning "invalid use" }
};

struct StructUnion7 {
  union {
    union {
      struct { double n2, a2[]; } s2;   // { dg-message "declared here" }
    } u2;
    struct { int n1, a1[]; } s1;
  } u;                                  // { dg-warning "invalid use" }
};

struct StructUnion8 {
  struct {
    union {
      union {
	struct { int n1, a1[]; } s1;    // { dg-error "not at end" }
      } u1;
      union {
	struct { double n2, a2[]; } s2;
      } u2;
    } u;
  } s1;

  struct {
    union {
      union {
	struct { int n1, a1[]; } s1;
      } u1;
      union {
	struct { double n2, a2[]; } s2;
      } u2;
    } u; } s2;                              // { dg-message "next member" }
};

struct StructUnion9 {                       // { dg-message "in the definition" }
  struct A1 {
    union B1 {
      union C1 {
	struct Sx1 { int n1, a1[]; } sx1;   // { dg-error "not at end" }
      } c1;
      union D1 {
	struct Sx2 { double n2, a2[]; } sx2;
      } d1;
    } b1;                                   // { dg-warning "invalid use" }
  } a1;

  struct A2 {
    union B2 {
      union C2 {
	struct Sx3 { int n3, a3[]; } sx3;   // { dg-message "declared here" }
      } c2;
      union D2 { struct Sx4 { double n4, a4[]; } sx4; } d2;
    } b2;                                   // { dg-warning "invalid use" }
  } a2;                                     // { dg-message "next member" }
};

// { dg-prune-output "forbids flexible array member" }