libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / Warray-bounds-61.c
blob5b66cdc0aab1a475cf5c0dde281a204655b332c3
1 /* PR middle-end/94940 - spurious -Warray-bounds for a zero length array
2 member of union
3 { dg-do compile }
4 { dg-options "-O2 -Wall" } */
6 extern int n;
8 extern union Ua3_a0 {
9 int a3[3];
10 int a2[2]; // can only alias a3[0 - 2]
11 int a1[1]; // can alias all of the union
12 int a0[0]; // ditto
13 } ua3_a0;
15 void test_ua3_ua0_a0 (int i)
17 ua3_a0.a0[0] = 0; // { dg-bogus "\\\[-Warray-bounds" }
18 ua3_a0.a0[1] = 0; // { dg-bogus "\\\[-Warray-bounds" }
19 ua3_a0.a0[2] = 0; // { dg-bogus "\\\[-Warray-bounds" }
20 ua3_a0.a0[3] = 0; // { dg-warning "\\\[-Warray-bounds" }
21 ua3_a0.a0[4] = 0; // { dg-warning "\\\[-Warray-bounds" }
22 ua3_a0.a0[i] = 0; // { dg-bogus "\\\[-Warray-bounds" }
24 if (i < __LINE__)
25 i = 5;
26 ua3_a0.a0[i] = 0; // { dg-warning "\\\[-Warray-bounds" }
28 if (i > -1)
29 i = -1;
30 ua3_a0.a0[i] = 0; // { dg-warning "\\\[-Warray-bounds" }
33 void test_ua3_ua0_a1 (int i)
35 /* Abusing one-element array members the same way as those of
36 length zero is discouraged but so far acceted without warnings.
37 This should change at some point. */
39 ua3_a0.a1[0] = 0;
40 ua3_a0.a1[1] = 0;
41 ua3_a0.a1[2] = 0;
42 ua3_a0.a1[3] = 0; // { dg-warning "\\\[-Warray-bounds" }
43 ua3_a0.a1[i] = 0;
45 if (i > -1)
46 i = -1;
47 ua3_a0.a1[i] = 0; // { dg-warning "\\\[-Warray-bounds" }
49 if (i < 7)
50 i = 7;
51 ua3_a0.a1[i] = 0; // { dg-warning "\\\[-Warray-bounds" }
54 void test_ua3_ua0_a2 (int i)
56 ua3_a0.a2[0] = 0;
57 ua3_a0.a2[1] = 0;
58 ua3_a0.a2[2] = 0; // { dg-warning "\\\[-Warray-bounds" }
59 ua3_a0.a2[i] = 0;
61 if (i < __LINE__)
62 i = __LINE__;
63 ua3_a0.a2[i] = 0; // { dg-warning "\\\[-Warray-bounds" }
65 if (i > -1)
66 i = -1;
67 ua3_a0.a2[i] = 0; // { dg-warning "\\\[-Warray-bounds" }
71 extern union Ua2_a3 {
72 int a2[2]; // can only alias a3[0 - 1]
73 int a3[3];
74 } ua2_a3;
76 void test_ua2_ua3 (int i)
78 ua2_a3.a2[0] = 0; // { dg-bogus "\\\[-Warray-bounds" }
79 ua2_a3.a2[1] = 0; // { dg-bogus "\\\[-Warray-bounds" }
80 ua2_a3.a2[2] = 0; // { dg-warning "\\\[-Warray-bounds" }
81 ua2_a3.a2[i] = 0;
83 if (i < __LINE__)
84 i = __LINE__;
85 ua2_a3.a2[i] = 0; // { dg-warning "\\\[-Warray-bounds" }
89 extern struct SUa2_a0 {
90 union Ua2_a0 {
91 int a2[2];
92 int a0[0];
93 } u;
94 } sua2_a0;
96 void test_sua2_sua0 (int i)
98 n += sua2_a0.u.a0[0];
99 n += sua2_a0.u.a0[1];
100 n += sua2_a0.u.a0[2]; // { dg-warning "\\\[-Warray-bounds" }
101 n += sua2_a0.u.a0[i];
103 if (i < __LINE__)
104 i = __LINE__;
105 n += sua2_a0.u.a0[i]; // { dg-warning "\\\[-Warray-bounds" }
108 void test_sua2_sua0_ptr (int i)
110 union Ua2_a0 *p = &sua2_a0.u;
112 n += p->a0[0];
113 n += p->a0[1];
114 n += p->a0[2]; // { dg-warning "\\\[-Warray-bounds" }
115 n += p->a0[i];
119 extern struct SUSa3_a0 {
120 union USa3_a0 {
121 struct {
122 int a3[3];
123 } s;
124 int a2[2]; // can alias s.a3[0 - 2]
125 int a1[1]; // can alias s.a3[0 - 2]
126 int a0[0]; // can alias s.a3[0]
127 } u;
128 } susa3_ua0;
130 void test_susa3_sua0 (int i, int j)
132 n += susa3_ua0.u.a0[0];
133 n += susa3_ua0.u.a0[1];
134 n += susa3_ua0.u.a0[2];
135 n += susa3_ua0.u.a0[3]; // { dg-warning "\\\[-Warray-bounds" }
138 void test_susa3_sua0_ptr (int i, int j)
140 union USa3_a0 *p = &susa3_ua0.u;
141 n += p->a0[0];
142 n += p->a0[1];
143 n += p->a0[2];
144 n += p->a0[3]; // { dg-warning "\\\[-Warray-bounds" }
147 void test_susa3_sua1 (int i)
149 n += susa3_ua0.u.a1[0];
150 n += susa3_ua0.u.a1[1];
151 n += susa3_ua0.u.a1[2];
152 n += susa3_ua0.u.a1[3]; // { dg-warning "\\\[-Warray-bounds" }
154 if (i < __LINE__)
155 i = __LINE__;
156 n += susa3_ua0.u.a1[i]; // { dg-warning "\\\[-Warray-bounds" }
159 void test_susa3_sua2 (void)
161 n += susa3_ua0.u.a2[0];
162 n += susa3_ua0.u.a2[1];
163 n += susa3_ua0.u.a2[2]; // { dg-warning "\\\[-Warray-bounds" }
164 n += susa3_ua0.u.a2[3]; // { dg-warning "\\\[-Warray-bounds" }
168 extern struct {
169 union {
170 struct {
171 int a3[3];
172 } s1;
173 struct {
174 int a0[0];
175 } s2;
176 } u;
177 } susa3_usa0;
179 void test_susi3_susi0 (int i)
181 n += susa3_usa0.u.s2.a0[0];
182 n += susa3_usa0.u.s2.a0[1];
183 n += susa3_usa0.u.s2.a0[2];
184 n += susa3_usa0.u.s2.a0[3]; // { dg-warning "\\\[-Warray-bounds" }
185 n += susa3_usa0.u.s2.a0[i];
187 if (i < __LINE__)
188 i = __LINE__;
189 n += susa3_usa0.u.s2.a0[i]; // { dg-warning "\\\[-Warray-bounds" }