PR middle-end/84095 - false-positive -Wrestrict warnings for memcpy within array
[official-gcc.git] / gcc / testsuite / gcc.dg / Wrestrict-9.c
blob5ad87401ed9969292279abd471f59ffd12699c0c
1 /* PR tree-optimization/84095 - false-positive -Wrestrict warnings for
2 strcpy within array
3 { dg-do compile }
4 { dg-options "-O2 -Wrestrict -ftrack-macro-expansion=0" } */
6 typedef __SIZE_TYPE__ size_t;
8 extern void* memcpy (void* restrict, const void* restrict, size_t);
9 extern char* strcpy (char* restrict, const char* restrict);
11 #define T(d, s) strcpy (d, s)
13 struct MemArrays {
14 char pad[8];
15 char a2[3][9];
16 char a3[3][4][9];
17 } a[2];
19 struct NestedMemArrays {
20 struct MemArrays ma;
21 struct MemArrays ma1[2];
22 } nma[2];
24 /* Use a variable as the source of a copy to verify that VAR_DECL
25 is handled correctly when it's the source of GIMPLE assignment. */
26 const char *str = "1234567";
28 void test_obj_2_dim_const (void)
30 const char *s = strcpy (a[0].a2[0], "1234567");
32 T (a[0].a2[1], s);
33 T (a[0].a2[2], s);
35 /* Ideally, the offsets in the warning below would be relative to
36 the beginning of the accessed array member. Unfortunately, when
37 the offset is represented as
38 MEM_REF (char[9], A, offsetof (struct MemArrays, A[0].A2[0]) + 1)
39 as it is in this instance it's difficult to determine the member
40 that is being accessed and tease out from the MEM_REF the offset
41 as it appears in the source. As a result, the warning mentions
42 the offset from the beginning of A instead. This is suboptimal
43 and should be fixed, either by printing the correct offsets or
44 by mentioning the base object that the offset is relative to. */
45 T (a[0].a2[0] + 1, s); /* { dg-warning "accessing 8 bytes at offsets \(1|9\) and \(0|8\) overlaps 7 bytes at offset \(1|9\)." } */
46 T (a[0].a2[1] + 2, s);
47 T (a[0].a2[2] + 3, s);
49 T (a[1].a2[0], s);
50 T (a[1].a2[1], s);
51 T (a[1].a2[2], s);
53 T (a[1].a2[0] + 1, s);
54 T (a[1].a2[1] + 2, s);
55 T (a[1].a2[2] + 3, s);
58 void test_obj_nested_2_dim_const (void)
60 const char *s = strcpy (nma[0].ma.a2[0], str);
62 T (nma[0].ma.a2[1], s);
63 T (nma[0].ma.a2[2], s);
65 T (nma[0].ma.a2[0] + 1, s); /* { dg-warning "accessing 8 bytes at offsets 1 and 0 overlaps 7 bytes at offset 1" "bug " { xfail *-*-* } } */
66 T (nma[0].ma.a2[1] + 2, s);
67 T (nma[0].ma.a2[2] + 3, s);
69 T (nma[1].ma.a2[1], s);
70 T (nma[1].ma.a2[2], s);
72 T (nma[1].ma.a2[0] + 1, s);
73 T (nma[1].ma.a2[1] + 2, s);
74 T (nma[1].ma.a2[2] + 3, s);
77 T (nma[0].ma1[0].a2[1], s);
78 T (nma[0].ma1[0].a2[2], s);
80 T (nma[0].ma1[0].a2[0] + 1, s);
81 T (nma[0].ma1[0].a2[1] + 2, s);
82 T (nma[0].ma1[0].a2[2] + 3, s);
84 T (nma[1].ma1[0].a2[1], s);
85 T (nma[1].ma1[0].a2[2], s);
87 T (nma[1].ma1[0].a2[0] + 1, s);
88 T (nma[1].ma1[0].a2[1] + 2, s);
89 T (nma[1].ma1[0].a2[2] + 3, s);
92 void test_obj_2_dim_var (int i, int j)
94 const char *s = memcpy (a[0].a2[0], "1234567", 8);
96 T (a[i].a2[0], s); /* { dg-bogus "\\\[-Wrestrict]" } */
97 T (a[i].a2[1], s);
98 T (a[i].a2[2], s);
100 T (a[i].a2[0] + 1, s);
101 T (a[i].a2[1] + 1, s);
102 T (a[i].a2[2] + 1, s);
104 T (a[0].a2[i], s); /* { dg-bogus "\\\[-Wrestrict]" } */
105 T (a[1].a2[i], s);
107 T (a[i].a2[0] + j, s);
108 T (a[i].a2[1] + j, s);
109 T (a[i].a2[2] + j, s);
111 T (a[0].a2[i] + 1, s);
112 T (a[1].a2[i] + 1, s);
114 T (a[0].a2[i] + j, s);
115 T (a[1].a2[i] + j, s);
117 if (i < 0 || 1 < i)
118 i = 1;
120 T (a[i].a2[0], s);
121 T (a[i].a2[1], s);
122 T (a[i].a2[2], s);
124 T (a[i].a2[0] + 1, s);
125 T (a[i].a2[1] + 1, s);
126 T (a[i].a2[2] + 1, s);
128 T (a[0].a2[i], s);
129 T (a[1].a2[i], s);
131 T (a[i].a2[0] + j, s);
132 T (a[i].a2[1] + j, s);
133 T (a[i].a2[2] + j, s);
135 T (a[0].a2[i] + 1, s);
136 T (a[1].a2[i] + 1, s);
138 T (a[0].a2[i] + j, s);
139 T (a[1].a2[i] + j, s);
142 void test_obj_nested_2_dim_var (int i, int j)
144 const char *s = strcpy (nma[0].ma.a2[0], "1234567");
146 T (nma[i].ma.a2[0], s); /* { dg-bogus "\\\[-Wrestrict]" } */
147 T (nma[i].ma.a2[1], s);
148 T (nma[i].ma.a2[2], s);
150 T (nma[i].ma.a2[0] + 1, s);
151 T (nma[i].ma.a2[1] + 1, s);
152 T (nma[i].ma.a2[2] + 1, s);
154 T (nma[0].ma.a2[i], s); /* { dg-bogus "\\\[-Wrestrict]" } */
155 T (nma[1].ma.a2[i], s);
157 T (nma[i].ma.a2[0] + j, s);
158 T (nma[i].ma.a2[1] + j, s);
159 T (nma[i].ma.a2[2] + j, s);
161 T (nma[0].ma.a2[i] + 1, s);
162 T (nma[1].ma.a2[i] + 1, s);
164 T (nma[0].ma.a2[i] + j, s);
165 T (nma[1].ma.a2[i] + j, s);
168 void test_ref_2_dim_const (struct MemArrays *p)
170 strcpy (p[0].a2[0], "1234567");
171 const char *s = p[0].a2[0];
173 T (p[0].a2[1], s);
174 T (p[0].a2[2], s);
176 T (p[1].a2[0], s);
177 T (p[1].a2[1], s);
178 T (p[1].a2[2], s);
181 void test_ref_2_dim_var (struct MemArrays *p, int i, int j)
183 strcpy (p[0].a2[0], "1234567");
184 const char *s = p[0].a2[0];
186 T (p[i].a2[0], s); /* { dg-bogus "\\\[-Wrestrict]" } */
187 T (p[i].a2[1], s);
188 T (p[i].a2[2], s);
190 T (p[0].a2[i], s);
191 T (p[1].a2[i], s);
193 T (p[i].a2[0] + j, s);
194 T (p[i].a2[1] + j, s);
195 T (p[i].a2[2] + j, s);
197 T (p[0].a2[i] + j, s);
198 T (p[1].a2[i] + j, s);
201 void test_obj_3_dim_var (int i, int j)
203 strcpy (a[0].a3[0][0], "1234567");
204 const char *s = a[0].a3[0][0];
206 T (a[0].a3[0][i], s);
207 T (a[0].a3[1][i], s);
208 T (a[0].a3[2][i], s);
210 T (a[1].a3[0][i], s);
211 T (a[1].a3[1][i], s);
212 T (a[1].a3[2][i], s);
214 T (a[0].a3[i][0], s); /* { dg-bogus "\\\[-Wrestrict\]" } */
215 T (a[0].a3[i][1], s);
216 T (a[0].a3[i][2], s);
218 T (a[1].a3[i][0], s);
219 T (a[1].a3[i][1], s);
220 T (a[1].a3[i][2], s);
222 T (a[i].a3[0][0], s); /* { dg-bogus "\\\[-Wrestrict\]" } */
223 T (a[i].a3[0][1], s);
224 T (a[i].a3[0][2], s);
226 T (a[i].a3[1][0], s);
227 T (a[i].a3[1][1], s);
228 T (a[i].a3[1][2], s);
230 T (a[i].a3[2][0], s);
231 T (a[i].a3[2][1], s);
232 T (a[i].a3[2][2], s);
235 T (a[0].a3[0][i] + 1, s);
236 T (a[0].a3[1][i] + 1, s);
237 T (a[0].a3[2][i] + 1, s);
239 T (a[1].a3[0][i] + 1, s);
240 T (a[1].a3[1][i] + 1, s);
241 T (a[1].a3[2][i] + 1, s);
244 T (a[0].a3[0][i] + j, s);
245 T (a[0].a3[1][i] + j, s);
246 T (a[0].a3[2][i] + j, s);
248 T (a[1].a3[0][i] + j, s);
249 T (a[1].a3[1][i] + j, s);
250 T (a[1].a3[2][i] + j, s);
252 T (a[0].a3[i][0] + j, s);
253 T (a[0].a3[i][1] + j, s);
254 T (a[0].a3[i][2] + j, s);
256 T (a[1].a3[i][0] + j, s);
257 T (a[1].a3[i][1] + j, s);
258 T (a[1].a3[i][2] + j, s);
260 T (a[i].a3[0][0] + j, s);
261 T (a[i].a3[0][1] + j, s);
262 T (a[i].a3[0][2] + j, s);
264 T (a[i].a3[1][0] + j, s);
265 T (a[i].a3[1][1] + j, s);
266 T (a[i].a3[1][2] + j, s);
268 T (a[i].a3[2][0] + j, s);
269 T (a[i].a3[2][1] + j, s);
270 T (a[i].a3[2][2] + j, s);
273 void test_obj_3_dim_const (struct MemArrays *p)
275 strcpy (p[0].a3[0][0], "1234567");
276 const char *s = p[0].a3[0][0];
278 T (p[0].a3[0][1], s);
279 T (p[0].a3[0][2], s);
280 T (p[0].a3[0][3], s);
282 T (p[0].a3[0][1] + 1, s);
283 T (p[0].a3[0][2] + 1, s);
284 T (p[0].a3[0][3] + 1, s);
286 T (p[0].a3[1][0], s);
287 T (p[0].a3[1][1], s);
288 T (p[0].a3[1][2], s);
289 T (p[0].a3[1][3], s);
291 T (p[0].a3[1][0] + 1, s);
292 T (p[0].a3[1][1] + 1, s);
293 T (p[0].a3[1][2] + 1, s);
294 T (p[0].a3[1][3] + 1, s);
296 T (p[0].a3[2][0], s);
297 T (p[0].a3[2][1], s);
298 T (p[0].a3[2][2], s);
299 T (p[0].a3[2][3], s);
301 T (p[1].a3[0][0], s);
302 T (p[1].a3[0][1], s);
303 T (p[1].a3[0][2], s);
304 T (p[1].a3[0][3], s);
306 T (p[1].a3[1][0], s);
307 T (p[1].a3[1][1], s);
308 T (p[1].a3[1][2], s);
309 T (p[1].a3[1][3], s);
311 T (p[1].a3[2][0], s);
312 T (p[1].a3[2][1], s);
313 T (p[1].a3[2][2], s);
314 T (p[1].a3[2][3], s);