Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wclass-memaccess.C

raw
1 /* PR c++/80560 - warn on undefined memory operations involving non-trivial
2 types
3 { dg-do compile }
4 { dg-options "-Wclass-memaccess -ftrack-macro-expansion=0" } */
5
6 typedef __SIZE_TYPE__ size_t;
7
8 extern "C"
9 {
10 void* memcpy (void*, const void*, size_t);
11 void* memmove (void*, const void*, size_t);
12 void* mempcpy (void*, const void*, size_t);
13 void* memset (void*, int, size_t);
14 void* realloc (void*, size_t);
15 }
16
17 namespace std {
18
19 #if __cplusplus >= 201103L
20 enum class byte: unsigned char { };
21 #else
22 typedef unsigned char byte;
23 #endif
24 }
25
26 #if __cplusplus < 201103L
27 typedef unsigned short char16_t;
28 #endif
29
30 /* Ordinary bzcopy and bzero aren't recognized as special. */
31 #define bcopy __builtin_bcopy
32 #define bzero __builtin_bzero
33
34 void sink (void*);
35
36 #define T(fn, arglist) ((fn arglist), sink (p))
37
38 #if !defined TEST || TEST == TEST_TRIVIAL
39
40 /* Trivial can be manipulated by raw memory functions. */
41 struct Trivial
42 {
43 int i; unsigned bf: 1; char *s; char a[4];
44
45 // Non-copy assignment doesn't make the class non-trivial or not
46 // trivially assignable.
47 Trivial& operator= (int);
48
49 // Likewise, template assignment doesn't make the class non-trivial
50 // or not trivially assignable.
51 template <class U>
52 Trivial& operator= (U);
53 };
54
55 void test (Trivial *p, void *q, int x)
56 {
57 const size_t n = x;
58
59 T (bzero, (p, 1));
60 T (bzero, (p, n));
61 T (bzero, (p, sizeof *p));
62 T (bzero, (q, 1));
63 T (bzero, (q, n));
64 T (bzero, (q, sizeof *p));
65
66 T (bcopy, (p, q, 1));
67 T (bcopy, (p, q, n));
68 T (bcopy, (p, q, sizeof *p));
69 T (bcopy, (q, p, 1));
70 T (bcopy, (q, p, n));
71 T (bcopy, (q, p, sizeof *p));
72
73 T (memcpy, (p, q, 1));
74 T (memcpy, (p, q, n));
75 T (memcpy, (p, q, sizeof *p));
76 T (memcpy, (q, p, 1));
77 T (memcpy, (q, p, n));
78 T (memcpy, (q, p, sizeof *p));
79
80 T (memset, (p, 0, 1));
81 T (memset, (p, 0, n));
82 T (memset, (p, 0, sizeof *p));
83 T (memset, (q, 0, 1));
84 T (memset, (q, 0, n));
85 T (memset, (q, 0, sizeof *p));
86
87 T (memset, (p, 1, 1));
88 T (memset, (p, 1, n));
89 T (memset, (p, 1, sizeof *p));
90 T (memset, (q, 1, 1));
91 T (memset, (q, 1, n));
92 T (memset, (q, 1, sizeof *p));
93
94 T (memset, (p, x, 1));
95 T (memset, (p, x, n));
96 T (memset, (p, x, sizeof *p));
97 T (memset, (q, x, 1));
98 T (memset, (q, x, n));
99 T (memset, (q, x, sizeof *p));
100
101 T (memmove, (p, q, 1));
102 T (memmove, (p, q, n));
103 T (memmove, (p, q, sizeof *p));
104 T (memmove, (q, p, 1));
105 T (memmove, (q, p, n));
106 T (memmove, (q, p, sizeof *p));
107
108 T (q = realloc, (p, 1));
109 T (q = realloc, (p, n));
110 T (q = realloc, (p, sizeof *p));
111
112 T (q = realloc, (q, 1));
113 T (q = realloc, (q, n));
114 T (q = realloc, (q, sizeof *p));
115 }
116
117 #endif
118
119 #if !defined TEST || TEST == TEST_TRIVIAL_ACCESS
120
121 /* TrivialAccess can be manipulated by raw memory functions in contexts
122 that have access to the trivial specia functions. */
123 struct TrivialAccess
124 {
125 int i; unsigned bf: 1; char *s; char a[4];
126
127 private:
128 TrivialAccess () = default;
129 TrivialAccess (const TrivialAccess&) = default;
130 protected:
131 TrivialAccess& operator= (const TrivialAccess&) = default;
132
133 void test_member (const TrivialAccess*, int);
134
135 friend void test_friend (TrivialAccess*, const TrivialAccess*, int);
136 };
137
138 void test (TrivialAccess *p, const TrivialAccess *q, int i)
139 {
140 void *pv;
141 (void)&pv;
142
143 /* Verify that a warning is issued when the copy ctor and copy
144 assignment are inaccessible. */
145 T (bzero, (p, sizeof *p)); // { dg-warning "bzero" }
146 T (bcopy, (q, p, sizeof *p)); // { dg-warning "bcopy" }
147 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
148 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
149 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
150 T (pv = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
151 }
152
153 void test_friend (TrivialAccess *p, const TrivialAccess *q, int i)
154 {
155 void *pv;
156 (void)&pv;
157
158 /* Verify that no warning is issued when the otherwise inaccessible
159 copy ctor and copy assignment can be accessed within the current
160 context. */
161 T (bzero, (p, sizeof *p));
162 T (bcopy, (q, p, sizeof *p));
163 T (memcpy, (p, q, sizeof *p));
164 T (memset, (p, i, sizeof *p));
165 T (memmove, (p, q, sizeof *p));
166 T (pv = realloc, (p, sizeof *p));
167 }
168
169 void TrivialAccess::test_member (const TrivialAccess *q, int i)
170 {
171 void *pv;
172 (void)&pv;
173
174 TrivialAccess *p = this;
175
176 /* Verify that no warning is issued when the otherwise inaccessible
177 copy ctor and copy assignment can be accessed within the current
178 context. */
179 T (bzero, (p, sizeof *p));
180 T (bcopy, (q, p, sizeof *p));
181 T (memcpy, (p, q, sizeof *p));
182 T (memset, (p, i, sizeof *p));
183 T (memmove, (p, q, sizeof *p));
184 T (pv = realloc, (p, sizeof *p));
185 }
186
187 #endif
188
189 #if !defined TEST || TEST == TEST_HAS_DEFAULT
190
191 /* HasDefault is trivially copyable but should be initialized by
192 the ctor, not bzero or memset. */
193 struct HasDefault { char a[4]; HasDefault (); };
194
195 void test (HasDefault *p, const HasDefault &x,
196 void *q, const unsigned char *s, const std::byte *b,
197 const signed char *ss, const char16_t *ws,
198 const int ia[])
199 {
200 const int i = *ia;
201 const size_t n = *ia;
202
203 // HasDefault is neither trivial nor standard-layout. The warning
204 // should mention the former since it's more permissive than the latter
205 // and so more informative.
206 T (bzero, (p, sizeof *p)); // { dg-warning "bzero(\[^\n\r\]*). clearing an object of non-trivial type .struct HasDefault.; use assignment or value-initialization instead" }
207
208 T (memset, (p, 0, sizeof *p)); // { dg-warning ".void\\* memset(\[^\n\r\]*). clearing an object of non-trivial type .struct HasDefault.; use assignment or value-initialization instead" }
209
210 T (memset, (p, 1, sizeof *p)); // { dg-warning ".void\\* memset(\[^\n\r\]*). writing to an object of non-trivial type .struct HasDefault.; use assignment instead" }
211
212 T (memset, (p, i, sizeof *p)); // { dg-warning ".void\\* memset(\[^\n\r\]*). writing to an object of non-trivial type .struct HasDefault.; use assignment instead" }
213
214 // Copying from another object of the same type is fine.
215 T (bcopy, (&x, p, sizeof *p));
216 T (bcopy, (&x, p, n));
217
218 T (memcpy, (p, &x, sizeof *p));
219 T (memcpy, (p, &x, n));
220
221 // Copying from a void* or character buffer is also fine.
222 T (bcopy, (q, p, sizeof *p));
223 T (bcopy, (q, p, n));
224 T (bcopy, (s, p, sizeof *p));
225 T (bcopy, (s, p, n));
226 T (bcopy, (b, p, n));
227
228 T (memcpy, (p, q, sizeof *p));
229 T (memcpy, (p, q, n));
230 T (memcpy, (p, s, sizeof *p));
231 T (memcpy, (p, s, n));
232 T (memcpy, (p, b, n));
233
234 T (memmove, (p, q, sizeof *p));
235 T (memmove, (p, q, n));
236 T (memmove, (p, s, sizeof *p));
237 T (memmove, (p, s, n));
238 T (memmove, (p, b, n));
239
240 T (mempcpy, (p, q, sizeof *p));
241 T (mempcpy, (p, q, n));
242 T (mempcpy, (p, s, sizeof *p));
243 T (mempcpy, (p, s, n));
244 T (mempcpy, (p, b, n));
245
246 // ...but partial copies are diagnosed.
247 T (memcpy, (p, &x, 1)); // { dg-warning "writing to an object of a non-trivial type .struct HasDefault. leaves 3 bytes unchanged" } */
248 T (memmove, (p, q, 2)); // { dg-warning "writing to an object of a non-trivial type .struct HasDefault. leaves 2 bytes unchanged" } */
249 T (mempcpy, (p, q, 3)); // { dg-warning "writing to an object of a non-trivial type .struct HasDefault. leaves 1 byte unchanged" } */
250
251 // Otherwise, copying from an object of an unrelated type is diagnosed.
252 T (memcpy, (p, ia, sizeof *p)); // { dg-warning ".void\\* memcpy(\[^\n\r\]*). copying an object of non-trivial type .struct HasDefault. from an array of .const int." }
253 extern long *ip;
254 T (memcpy, (p, ip, sizeof *p)); // { dg-warning ".void\\* memcpy(\[^\n\r\]*). copying an object of non-trivial type .struct HasDefault. from an array of .long." }
255 T (memcpy, (p, ss, sizeof *p)); // { dg-warning ".void\\* memcpy(\[^\n\r\]*). copying an object of non-trivial type .struct HasDefault. from an array of .const signed char." }
256 T (memcpy, (p, ws, sizeof *p)); // { dg-warning ".void\\* memcpy(\[^\n\r\]*). copying an object of non-trivial type .struct HasDefault. from an array of .const \(char16_t\|unsigned short\)." }
257
258 T (memmove, (p, ia, sizeof *p)); // { dg-warning ".void\\* memmove(\[^\n\r\]*). copying an object of non-trivial type .struct HasDefault. from an array of .const int." }
259
260 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning ".void\\* mempcpy(\[^\n\r\]*). copying an object of non-trivial type .struct HasDefault. from an array of .const int." }
261
262 // Reallocating is the same as calling memcpy except that only
263 // shrinking reallocation is diagnosed.
264 T (q = realloc, (p, 1)); // { dg-warning "moving an object of non-trivial type .struct HasDefault. and size 4 into a region of size 1" }
265 T (q = realloc, (p, n));
266 T (q = realloc, (p, sizeof *p));
267 T (q = realloc, (p, sizeof *p + 1));
268 }
269
270 #endif
271
272 #if !defined TEST || TEST == TEST_HAS_TEMPLATE_DEFAULT
273
274 /* HasTemplateDefault should be initialized by means of the ctor,
275 not zeroed out by bzero/memset. */
276 struct HasTemplateDefault
277 {
278 template <class U>
279 HasTemplateDefault (U);
280 };
281
282 void test (HasTemplateDefault *p, const HasTemplateDefault &x,
283 const void *q, const unsigned char *s, const std::byte *b,
284 const signed char *ss, const char16_t *ws,
285 const int ia[])
286 {
287 const int i = *ia;
288 const size_t n = *ia;
289
290 // Zeroing out is diagnosed because value initialization is
291 // invalid (the template ctor makes default ctor unavailable).
292 T (bzero, (p, sizeof *p)); // { dg-warning "bzero" }
293 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
294 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
295 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
296
297 // Copying from an object of any type is okay.
298 T (bcopy, (&x, p, sizeof *p));
299 T (bcopy, (q, p, sizeof *p));
300 T (bcopy, (s, p, sizeof *p));
301 T (bcopy, (b, p, sizeof *p));
302 T (bcopy, (ss, p, sizeof *p)); // { dg-warning "bcopy" }
303 T (bcopy, (ws, p, sizeof *p)); // { dg-warning "bcopy" }
304 T (bcopy, (ia, p, sizeof *p)); // { dg-warning "bcopy" }
305
306 T (memcpy, (p, &x, sizeof *p));
307 T (memcpy, (p, q, sizeof *p));
308 T (memcpy, (p, s, sizeof *p));
309 T (memcpy, (p, b, sizeof *p));
310 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
311 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
312 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
313
314 T (memmove, (p, &x, sizeof *p));
315 T (memmove, (p, q, sizeof *p));
316 T (memmove, (p, s, sizeof *p));
317 T (memmove, (p, b, sizeof *p));
318 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
319 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
320 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
321
322 T (mempcpy, (p, &x, sizeof *p));
323 T (mempcpy, (p, q, sizeof *p));
324 T (mempcpy, (p, s, sizeof *p));
325 T (mempcpy, (p, b, sizeof *p));
326 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
327 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
328 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
329
330 // Reallocating is the same as calling memcpy.
331 T (q = realloc, (p, 1));
332 T (q = realloc, (p, n));
333 T (q = realloc, (p, sizeof *p));
334 }
335
336 #endif
337
338 #if !defined TEST || TEST == TEST_HAS_COPY
339
340 /* HasCopy should be copied using the copy ctor or assignment, not
341 by memcpy or memmove. Since it's non-trivial, it should not be zeroed
342 out by bzero/memset either and should instead use assignment and/or
343 value initialization. */
344 struct HasCopy { int i; HasCopy (const HasCopy&); };
345
346 void test (HasCopy *p, const HasCopy &x,
347 const void *q, const unsigned char *s, const std::byte *b,
348 const signed char *ss, const char16_t *ws,
349 const int ia[])
350 {
351 const int i = *ia;
352 const size_t n = *ia;
353
354 // Zeroing out is diagnosed because value initialization is invalid
355 // (the copy ctor makes no default ctor unavailable). Since the type
356 // has no default ctor verify that the suggested alternative does not
357 // include value-initialization.
358 T (bzero, (p, sizeof *p)); // { dg-warning "clearing an object of non-trivial type .struct HasCopy.; use assignment instead" }
359 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
360 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
361 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
362
363 // Copying from an object of any type is diagnosed.
364 T (bcopy, (&x, p, sizeof *p)); // { dg-warning "bcopy" }
365 T (bcopy, (q, p, sizeof *p)); // { dg-warning "bcopy" }
366 T (bcopy, (s, p, sizeof *p)); // { dg-warning "bcopy" }
367 T (bcopy, (b, p, sizeof *p)); // { dg-warning "bcopy" }
368 T (bcopy, (ss, p, sizeof *p)); // { dg-warning "bcopy" }
369 T (bcopy, (ws, p, sizeof *p)); // { dg-warning "bcopy" }
370 T (bcopy, (ia, p, sizeof *p)); // { dg-warning "bcopy" }
371
372 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "memcpy" }
373 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
374 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
375 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
376 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
377 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
378 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
379
380 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
381 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
382 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
383 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
384 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
385 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
386 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
387
388 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
389 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
390 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
391 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
392 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
393 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
394 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
395
396 // Reallocating is the same as calling memcpy.
397 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
398 T (q = realloc, (p, n)); // { dg-warning "realloc" }
399 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
400 }
401
402 #endif
403
404 #if !defined TEST || TEST == TEST_HAS_DEFAULT_AND_COPY
405
406 /* HasDefaultAndCopy is like HasCopy above but its default ctor takes
407 a default argument to verify that the suggested alternative offered
408 by the warning includes the default ctor (i.e., the test verifies
409 that the default ctor is recognized as such despite taking an argument. */
410
411 struct HasDefaultAndCopy
412 {
413 HasDefaultAndCopy (int = 0); // default ctor
414 HasDefaultAndCopy (const HasDefaultAndCopy&);
415 };
416
417 void test (HasDefaultAndCopy *p, const HasDefaultAndCopy &x)
418 {
419 T (bzero, (p, sizeof *p)); // { dg-warning "clearing an object of non-trivial type .struct HasDefaultAndCopy.; use assignment or value-initialization instead" }
420 T (memset, (p, 0, sizeof *p)); // { dg-warning "clearing an object of non-trivial type .struct HasDefaultAndCopy.; use assignment or value-initialization instead" }
421 }
422
423 #endif
424
425 #if !defined TEST || TEST == TEST_HAS_PRIVATE_COPY
426
427 /* HasPrivateCopy cannot be copied using memcpy or memmove. Since it's
428 non-trivial, it it should not be zeroed out by bzero/memset either
429 and should instead use assignment and/or value initialization. */
430 struct HasPrivateCopy {
431 int i;
432 private:
433 HasPrivateCopy (const HasPrivateCopy&);
434 };
435
436 void test (HasPrivateCopy *p, const HasPrivateCopy &x,
437 const void *q, const unsigned char *s, const std::byte *b,
438 const signed char *ss, const char16_t *ws,
439 const int ia[])
440 {
441 const int i = *ia;
442 const size_t n = *ia;
443
444 // Zeroing out is diagnosed because value initialization is
445 // invalid (the copy ctor makes no default ctor unavailable).
446 // Verify also that the suggestion offers assignment but not
447 // value initialization (since the lattare is not available).
448 T (bzero, (p, sizeof *p)); // { dg-warning "clearing an object of non-trivial type .struct HasPrivateCopy.; use assignment instead" }
449 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
450 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
451 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
452
453 // Copying from an object of any type is diagnosed.
454 T (memcpy, (p, &x, sizeof *p)); // { dg-warning ".void\\* memcpy(\[^\n\r\]*). writing to an object of non-trivially copyable type .struct HasPrivateCopy.; use copy-assignment instead" }
455 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
456 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
457 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
458 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
459 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
460 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
461
462 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
463 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
464 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
465 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
466 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
467 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
468 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
469
470 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
471 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
472 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
473 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
474 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
475 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
476 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
477
478 // Reallocating is the same as calling memcpy.
479 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
480 T (q = realloc, (p, n)); // { dg-warning "realloc" }
481 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
482 }
483
484 #endif
485
486 #if !defined TEST || TEST == TEST_HAS_DTOR
487
488 /* HasDtor should be initialized using aggregate or memberwise intialization,
489 not bzero or memset. */
490 struct HasDtor { int i; ~HasDtor (); };
491
492 void test (HasDtor *p, const HasDtor &x,
493 const void *q, const unsigned char *s, const std::byte *b,
494 const signed char *ss, const char16_t *ws,
495 const int ia[])
496 {
497 const int i = *ia;
498 const size_t n = *ia;
499
500 // Zeroing out is diagnosed only because it's difficult not to.
501 // Otherwise, a class that's non-trivial only because it has
502 // a non-trivial dtor can be safely zeroed out (that's what
503 // value-initializing it does).
504 T (bzero, (p, sizeof *p)); // { dg-warning "bzero" }
505 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
506 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
507 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
508
509 // Copying from an object of any type is diagnosed simply because
510 // a class with a user-defined dtor is not trivially copyable.
511 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "memcpy" }
512 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
513 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
514 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
515 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
516 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
517 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
518
519 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
520 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
521 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
522 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
523 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
524 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
525 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
526
527 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
528 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
529 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
530 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
531 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
532 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
533 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
534
535 // Reallocating is the same as calling memcpy.
536 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
537 T (q = realloc, (p, n)); // { dg-warning "realloc" }
538 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
539 }
540
541 #endif
542
543 #if !defined TEST || TEST == TEST_HAS_DELETED_DTOR
544
545 // HasDeletedDtor is trivial so clearing and copying it is okay.
546 // Relocation would bypass the deleted dtor and so it's diagnosed.
547
548 struct HasDeletedDtor
549 {
550 int i;
551 ~HasDeletedDtor () = delete;
552 };
553
554 void test (HasDeletedDtor *p, const HasDeletedDtor &x,
555 const void *q, const unsigned char *s, const std::byte *b,
556 const signed char *ss, const char16_t *ws,
557 const int ia[])
558 {
559 const int i = *ia;
560 const size_t n = *ia;
561
562 T (bzero, (p, sizeof *p));
563 T (memset, (p, 0, sizeof *p));
564 T (memset, (p, 1, sizeof *p));
565 T (memset, (p, i, sizeof *p));
566
567 T (memcpy, (p, &x, sizeof *p));
568 T (memcpy, (p, q, sizeof *p));
569 T (memcpy, (p, s, sizeof *p));
570 T (memcpy, (p, b, sizeof *p));
571 T (memcpy, (p, ss, sizeof *p));
572 T (memcpy, (p, ws, sizeof *p));
573 T (memcpy, (p, ia, sizeof *p));
574
575 T (memmove, (p, &x, sizeof *p));
576 T (memmove, (p, q, sizeof *p));
577 T (memmove, (p, s, sizeof *p));
578 T (memmove, (p, b, sizeof *p));
579 T (memmove, (p, ss, sizeof *p));
580 T (memmove, (p, ws, sizeof *p));
581 T (memmove, (p, ia, sizeof *p));
582
583 T (mempcpy, (p, &x, sizeof *p));
584 T (mempcpy, (p, q, sizeof *p));
585 T (mempcpy, (p, s, sizeof *p));
586 T (mempcpy, (p, b, sizeof *p));
587 T (mempcpy, (p, ss, sizeof *p));
588 T (mempcpy, (p, ws, sizeof *p));
589 T (mempcpy, (p, ia, sizeof *p));
590
591 // Reallocating is diagnosed.
592 T (q = realloc, (p, 1)); // { dg-warning "moving an object of type .struct HasDeletedDtor. with deleted destructor" }
593 T (q = realloc, (p, n)); // { dg-warning "realloc" }
594 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
595 }
596
597 #endif
598
599 #if !defined TEST || TEST == TEST_HAS_PRIVATE_DTOR
600
601 // Unlike HasDeletedDtor, HasPrivateDtor is okay to zero-out and copy
602 // but not relocate because doing so would bypass the deleted dtor..
603
604 struct HasPrivateDtor
605 {
606 int i;
607 private:
608 ~HasPrivateDtor ();
609 };
610
611 void test (HasPrivateDtor *p, const HasPrivateDtor &x,
612 const void *q, const unsigned char *s, const std::byte *b,
613 const signed char *ss, const char16_t *ws,
614 const int ia[])
615 {
616 const int i = *ia;
617 const size_t n = *ia;
618
619 T (bzero, (p, sizeof *p)); // { dg-warning "clearing an object of non-trivial type .struct HasPrivateDtor.; use assignment or value-initialization instead" }
620 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
621 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
622 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
623
624 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "writing to an object of non-trivially copyable type .struct HasPrivateDtor.; use copy-assignment or copy-initialization instead" }
625 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
626 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
627 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
628 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
629 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
630 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
631
632 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
633 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
634 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
635 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
636 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
637 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
638 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
639
640 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
641 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
642 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
643 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
644 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
645 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
646 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
647
648 // Reallocating is diagnosed.
649 T (q = realloc, (p, 1)); // { dg-warning "moving an object of non-trivially copyable type .struct HasPrivateDtor." }
650 T (q = realloc, (p, n)); // { dg-warning "realloc" }
651 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
652 }
653
654 #endif
655
656 #if !defined TEST || TEST == TEST_HAS_COPY_ASSIGN
657
658 /* HasCopyAssign should be copied using the copy ctor or assignment, not
659 by memcpy or memmove. */
660 struct HasCopyAssign { void operator= (HasCopyAssign&); };
661
662 void test (HasCopyAssign *p, const HasCopyAssign &x,
663 const void *q, const unsigned char *s, const std::byte *b,
664 const signed char *ss, const char16_t *ws,
665 const int ia[])
666 {
667 const int i = *ia;
668 const size_t n = *ia;
669
670 // Zeroing out is diagnosed because it when used with an existing
671 // (already constructed) object in lieu of assigning a new value
672 // to it would bypass the user-defined assignment.
673 T (bzero, (p, sizeof *p)); // { dg-warning "bzero" }
674 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
675 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
676 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
677
678 // Copying from an object of any type is diagnosed.
679 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "memcpy" }
680 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
681 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
682 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
683 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
684 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
685 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
686
687 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
688 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
689 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
690 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
691 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
692 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
693 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
694
695 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
696 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
697 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
698 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
699 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
700 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
701 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
702
703 // Reallocating is the same as calling memcpy.
704 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
705 T (q = realloc, (p, n)); // { dg-warning "realloc" }
706 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
707 }
708
709 #endif
710
711 #if !defined TEST || TEST == TEST_HAS_MOVE_ASSIGN
712
713 /* Like HasCopyAssign, HasMoveAssign should be copied using the copy
714 ctor or assignment, not by memcpy or memmove. */
715 struct HasMoveAssign
716 {
717 #if __cplusplus > 199711L
718 void operator= (HasMoveAssign&&);
719 #else
720 // C++ 98 has no reference references. Simply repeat the HasCopyAssign
721 // test to avoid having to add a conditional to every dg-warning directive.
722 void operator= (const HasMoveAssign&);
723 #endif
724 };
725
726 void test (HasMoveAssign *p, const HasMoveAssign &x,
727 const void *q, const unsigned char *s, const std::byte *b,
728 const signed char *ss, const char16_t *ws,
729 const int ia[])
730 {
731 const int i = *ia;
732 const size_t n = *ia;
733
734 // Zeroing out is diagnosed because it when used with an existing
735 // (already constructed) object in lieu of assigning a new value
736 // to it would bypass the user-defined assignment.
737 T (bzero, (p, sizeof *p)); // { dg-warning "bzero" }
738 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
739 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
740 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
741
742 // Copying from an object of any type is diagnosed.
743 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "memcpy" }
744 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
745 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
746 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
747 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
748 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
749 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
750
751 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
752 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
753 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
754 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
755 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
756 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
757 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
758
759 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
760 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
761 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
762 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
763 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
764 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
765 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
766
767 // Reallocating is the same as calling memcpy.
768 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
769 T (q = realloc, (p, n)); // { dg-warning "realloc" }
770 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
771 }
772
773 #endif
774
775 #if !defined TEST || TEST == TEST_TRIVIAL_COPY_HAS_MOVE_ASSIGN
776
777 /* TrivialCopyHasMoveAssign should be copied using the copy ctor
778 or assignment, not by memcpy or memmove. */
779 struct TrivialCopyHasMoveAssign
780 {
781 typedef TrivialCopyHasMoveAssign Self;
782
783 Self& operator= (const Self&) = default;
784
785 #if __cplusplus > 199711L
786 Self& operator= (Self&&);
787 #else
788 // C++ 98 has no reference references. Fake the test by adding
789 // a non-const overload of the assignment operator (which should
790 // have the same effect).
791 Self& operator= (Self&);
792 #endif
793 };
794
795 void test (TrivialCopyHasMoveAssign *p, const TrivialCopyHasMoveAssign &x,
796 const void *q, const unsigned char *s, const std::byte *b,
797 const signed char *ss, const char16_t *ws,
798 const int ia[])
799 {
800 const int i = *ia;
801 const size_t n = *ia;
802
803 // Zeroing out is diagnosed because it when used with an existing
804 // (already constructed) object in lieu of assigning a new value
805 // to it would bypass the user-defined assignment.
806 T (bzero, (p, sizeof *p)); // { dg-warning "bzero" }
807 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
808 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
809 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
810
811 // Copying from an object of any type is diagnosed.
812 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "memcpy" }
813 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
814 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
815 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
816 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
817 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
818 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
819
820 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
821 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
822 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
823 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
824 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
825 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
826 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
827
828 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
829 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
830 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
831 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
832 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
833 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
834 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
835
836 // Reallocating is the same as calling memcpy.
837 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
838 T (q = realloc, (p, n)); // { dg-warning "realloc" }
839 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
840 }
841
842 #endif
843
844 #if !defined TEST || TEST == TEST_TRIVIAL_MOVE_HAS_COPY_ASSIGN
845
846 /* TrivialMoveNontrivialCopyAssign should be copied using the copy ctor
847 or assignment, not by memcpy or memmove. */
848 struct TrivialMoveNontrivialCopyAssign
849 {
850 typedef TrivialMoveNontrivialCopyAssign Self;
851
852 Self& operator= (const Self&);
853 #if __cplusplus > 199711L
854 // C++ 98 has no reference references. Fake the test by simply
855 // not declaring the move assignment.
856 Self& operator= (Self&&) = default;
857 #endif
858 };
859
860 void test (TrivialMoveNontrivialCopyAssign *p,
861 const TrivialMoveNontrivialCopyAssign &x,
862 const void *q, const unsigned char *s, const std::byte *b,
863 const signed char *ss, const char16_t *ws,
864 const int ia[])
865 {
866 const int i = *ia;
867 const size_t n = *ia;
868
869 // Zeroing out is diagnosed because it when used with an existing
870 // (already constructed) object in lieu of assigning a new value
871 // to it would bypass the user-defined assignment.
872 T (bzero, (p, sizeof *p)); // { dg-warning "bzero" }
873 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
874 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
875 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
876
877 // Copying from an object of any type is diagnosed.
878 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "memcpy" }
879 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
880 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
881 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
882 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
883 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
884 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
885
886 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
887 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
888 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
889 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
890 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
891 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
892 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
893
894 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
895 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
896 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
897 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
898 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
899 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
900 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
901
902 // Reallocating is the same as calling memcpy.
903 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
904 T (q = realloc, (p, n)); // { dg-warning "realloc" }
905 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
906 }
907
908 #endif
909
910 #if !defined TEST || TEST == TEST_TRIVIAL_ASSIGN_REF_OVERLOAD
911
912 /* TrivialAssignRefOverload is a trivial type. */
913 struct TrivialAssignRefOverload {
914 int i;
915 typedef TrivialAssignRefOverload Self;
916
917 Self& operator= (Self&) = default;
918 Self& operator= (const Self&) = delete;
919 Self& operator= (volatile Self&) = delete;
920 Self& operator= (const volatile Self&) = delete;
921 };
922
923 void test (TrivialAssignRefOverload *p, const TrivialAssignRefOverload &x,
924 const void *q, const unsigned char *s, const std::byte *b,
925 const signed char *ss, const char16_t *ws,
926 const int ia[])
927 {
928 const int i = *ia;
929 const size_t n = *ia;
930
931 T (bzero, (p, sizeof *p));
932 T (memset, (p, 0, sizeof *p));
933 T (memset, (p, 1, sizeof *p));
934 T (memset, (p, i, sizeof *p));
935
936 T (memcpy, (p, &x, sizeof *p));
937 T (memcpy, (p, q, sizeof *p));
938 T (memcpy, (p, s, sizeof *p));
939 T (memcpy, (p, b, sizeof *p));
940 T (memcpy, (p, ss, sizeof *p));
941 T (memcpy, (p, ws, sizeof *p));
942 T (memcpy, (p, ia, sizeof *p));
943
944 T (memmove, (p, &x, sizeof *p));
945 T (memmove, (p, q, sizeof *p));
946 T (memmove, (p, s, sizeof *p));
947 T (memmove, (p, b, sizeof *p));
948 T (memmove, (p, ss, sizeof *p));
949 T (memmove, (p, ws, sizeof *p));
950 T (memmove, (p, ia, sizeof *p));
951
952 T (mempcpy, (p, &x, sizeof *p));
953 T (mempcpy, (p, q, sizeof *p));
954 T (mempcpy, (p, s, sizeof *p));
955 T (mempcpy, (p, b, sizeof *p));
956 T (mempcpy, (p, ss, sizeof *p));
957 T (mempcpy, (p, ws, sizeof *p));
958 T (mempcpy, (p, ia, sizeof *p));
959
960 T (q = realloc, (p, 1));
961 T (q = realloc, (p, n));
962 T (q = realloc, (p, sizeof *p));
963 }
964
965 #endif
966
967 #if !defined TEST || TEST == TEST_TRIVIAL_ASSIGN_CSTREF_OVERLOAD
968
969 /* TrivialAssignCstOverload is a trivial type. */
970 struct TrivialAssignCstRefOverload {
971 int i;
972 typedef TrivialAssignCstRefOverload Self;
973
974 Self& operator= (Self&) = delete;
975 Self& operator= (const Self&) = default;
976 Self& operator= (volatile Self&) = delete;
977 Self& operator= (const volatile Self&) = delete;
978 };
979
980 void test (TrivialAssignCstRefOverload *p,
981 const TrivialAssignCstRefOverload &x,
982 const void *q, const unsigned char *s, std::byte *b,
983 const signed char *ss, const char16_t *ws,
984 const int ia[])
985 {
986 const int i = *ia;
987 const size_t n = *ia;
988
989 T (bzero, (p, sizeof *p));
990 T (memset, (p, 0, sizeof *p));
991 T (memset, (p, 1, sizeof *p));
992 T (memset, (p, i, sizeof *p));
993
994 T (memcpy, (p, &x, sizeof *p));
995 T (memcpy, (p, q, sizeof *p));
996 T (memcpy, (p, s, sizeof *p));
997 T (memcpy, (p, b, sizeof *p));
998 T (memcpy, (p, ss, sizeof *p));
999 T (memcpy, (p, ws, sizeof *p));
1000 T (memcpy, (p, ia, sizeof *p));
1001
1002 T (memmove, (p, &x, sizeof *p));
1003 T (memmove, (p, q, sizeof *p));
1004 T (memmove, (p, s, sizeof *p));
1005 T (memmove, (p, b, sizeof *p));
1006 T (memmove, (p, ss, sizeof *p));
1007 T (memmove, (p, ws, sizeof *p));
1008 T (memmove, (p, ia, sizeof *p));
1009
1010 T (mempcpy, (p, &x, sizeof *p));
1011 T (mempcpy, (p, q, sizeof *p));
1012 T (mempcpy, (p, s, sizeof *p));
1013 T (mempcpy, (p, b, sizeof *p));
1014 T (mempcpy, (p, ss, sizeof *p));
1015 T (mempcpy, (p, ws, sizeof *p));
1016 T (mempcpy, (p, ia, sizeof *p));
1017
1018 T (q = realloc, (p, 1));
1019 T (q = realloc, (p, n));
1020 T (q = realloc, (p, sizeof *p));
1021 }
1022
1023 #endif
1024
1025 #if !defined TEST || TEST == TEST_TRIVIAL_REF_HAS_VOLREF_ASSIGN
1026
1027 struct TrivialRefHasVolRefAssign
1028 {
1029 typedef TrivialRefHasVolRefAssign Self;
1030
1031 Self& operator= (Self&) = default;
1032 Self& operator= (volatile Self&);
1033 };
1034
1035 void test (TrivialRefHasVolRefAssign *p,
1036 const TrivialRefHasVolRefAssign &x,
1037 const void *q, const unsigned char *s, const std::byte *b,
1038 const signed char *ss, const char16_t *ws,
1039 const int ia[])
1040 {
1041 const int i = *ia;
1042 const size_t n = *ia;
1043
1044 // Zeroing out is diagnosed because it when used with an existing
1045 // (already constructed) object in lieu of assigning a new value
1046 // to it would bypass the user-defined assignment.
1047 T (bzero, (p, sizeof *p)); // { dg-warning "bzero" }
1048 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
1049 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
1050 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
1051
1052 // Copying from an object of any type is diagnosed.
1053 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "memcpy" }
1054 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
1055 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
1056 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
1057 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
1058 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
1059 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
1060
1061 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
1062 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
1063 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
1064 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
1065 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1066 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1067 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1068
1069 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
1070 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
1071 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
1072 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
1073 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1074 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1075 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1076
1077 // Reallocating is the same as calling memcpy.
1078 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
1079 T (q = realloc, (p, n)); // { dg-warning "realloc" }
1080 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
1081 }
1082
1083 #endif
1084
1085 #if !defined TEST || TEST == TEST_HAS_VOLREF_ASSIGN
1086
1087 struct HasVolRefAssign {
1088 int i;
1089 typedef HasVolRefAssign Self;
1090
1091 Self& operator= (volatile Self&);
1092 };
1093
1094 void test (HasVolRefAssign *p, const HasVolRefAssign &x,
1095 const void *q, const unsigned char *s, const std::byte *b,
1096 const signed char *ss, const char16_t *ws,
1097 const int ia[])
1098 {
1099 const int i = *ia;
1100 const size_t n = *ia;
1101
1102 // Zeroing out is diagnosed because it when used with an existing
1103 // (already constructed) object in lieu of assigning a new value
1104 // to it would bypass the user-defined assignment.
1105 T (bzero, (p, sizeof *p)); // { dg-warning "bzero" }
1106 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
1107 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
1108 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
1109
1110 // Copying from an object of any type is diagnosed.
1111 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "memcpy" }
1112 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
1113 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
1114 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
1115 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
1116 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
1117 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
1118
1119 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
1120 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
1121 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
1122 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
1123 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1124 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1125 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1126
1127 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
1128 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
1129 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
1130 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
1131 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1132 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1133 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1134
1135 // Reallocating is the same as calling memcpy.
1136 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
1137 T (q = realloc, (p, n)); // { dg-warning "realloc" }
1138 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
1139 }
1140
1141 #endif
1142
1143 #if !defined TEST || TEST == TEST_HAS_VIRTUALS
1144
1145 /* HasVirtuals should only be manipulated by the special member functions
1146 and not by bzero, memcpy, or any other raw memory function. Doing
1147 otherwse might corrupt the vtable pointer. */
1148 struct HasVirtuals { int i; virtual void foo (); };
1149
1150 void test (HasVirtuals *p, const HasVirtuals &x,
1151 const void *q, const unsigned char *s, const std::byte *b,
1152 const signed char *ss, const char16_t *ws,
1153 const int ia[])
1154 {
1155 const int i = *ia;
1156 const size_t n = *ia;
1157
1158 // Zeroing out is diagnosed because it corrupts the vtable.
1159 T (bzero, (p, sizeof *p)); // { dg-warning "bzero" }
1160 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
1161 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
1162 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
1163
1164 // Copying is diagnosed because when used to initialize an object
1165 // could incorrectly initialize the vtable.
1166 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "memcpy" }
1167 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
1168 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
1169 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
1170 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
1171 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
1172 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
1173
1174 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
1175 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
1176 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
1177 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
1178 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1179 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1180 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1181
1182 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
1183 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
1184 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
1185 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
1186 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1187 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1188 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1189
1190 // Reallocating is the same as calling memcpy.
1191 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
1192 T (q = realloc, (p, n)); // { dg-warning "realloc" }
1193 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
1194 }
1195
1196 #endif
1197
1198 #if !defined TEST || TEST == TEST_HAS_CONST_DATA
1199
1200 /* HasConstData should only be initialized using aggregate initializatoon
1201 and not cleared by bzero, or copied into using memcpy. Since it's not
1202 assignable allowing, raw memory functions to write into it would defeat
1203 const-correctness. */
1204 struct HasConstData { const char a[4]; };
1205
1206 void test (HasConstData *p, const HasConstData &x,
1207 const void *q, const unsigned char *s, const std::byte *b,
1208 const signed char *ss, const char16_t *ws,
1209 const int ia[])
1210 {
1211 const int i = *ia;
1212 const size_t n = *ia;
1213
1214 // The following is ill-formed because HasConstData's cannot
1215 // be assigned (the assignment is implicitly deleted). For
1216 // that reason all raw memory operations are diagnosed.
1217 // *p = x;
1218
1219 // Zeroing out is diagnosed because if used with an existing
1220 // (already initialized) object could break const correctness.
1221 // Since the default ctor and copy assignment are both deleted,
1222 // verify that they're not suggested as a possible alternative.
1223 T (bzero, (p, sizeof *p)); // { dg-warning "clearing an object of type .struct HasConstData. with no trivial copy-assignment \\\[" "c++ 11 and later" { target { c++11 } } }
1224 // { dg-warning "clearing an object of type .struct HasConstData. with no trivial copy-assignment" "c++ 98" { target { c++98_only } } .-1 }
1225 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
1226 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
1227 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
1228
1229 // Copying is also diagnosed.
1230 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "writing to an object of type .struct HasConstData. with no trivial copy-assignment; use copy-initialization instead" "c++ 11 and later" { target { c++11 } } }
1231 // { dg-warning "writing to an object of type .struct HasConstData. with no trivial copy-assignment" "c++ 98" { target { c++98_only } } .-1 }
1232 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
1233 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
1234 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
1235 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
1236 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
1237 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
1238
1239 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
1240 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
1241 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
1242 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
1243 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1244 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1245 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1246
1247 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
1248 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
1249 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
1250 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
1251 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1252 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1253 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1254
1255 // Reallocating is not diagnosed except in C++ 98 due to a bug.
1256 T (q = realloc, (p, 1)); // { dg-warning "moving an object of non-trivially copyable type .struct HasConstData.; use .new. and .delete. instead" "c++98" { target { c++98_only } } }
1257 // { dg-warning "moving an object of non-trivial type" "" { target c++11 } .-1 }
1258 T (q = realloc, (p, n)); // { dg-warning "realloc" "c++98" { target { c++98_only } } }
1259 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" "c++98" { target { c++98_only } } }
1260 }
1261
1262 #endif
1263
1264 #if !defined TEST || TEST == TEST_HAS_REFERENCE
1265
1266 /* HasReference should only be initialized using aggregate initializatoon
1267 and not cleared by bzero, or copied into using memcpy. Since it's not
1268 assignable, allowing raw memory functions to write into it could
1269 corrupt the reference. */
1270 struct HasReference { int &ci; };
1271
1272 void test (HasReference *p, const HasReference &x,
1273 const void *q, const unsigned char *s, const std::byte *b,
1274 const signed char *ss, const char16_t *ws,
1275 const int ia[])
1276 {
1277 const int i = *ia;
1278 const size_t n = *ia;
1279
1280 // Similarly to HasConstData, the following is ill-formed because
1281 // Hasreference cannot be assigned (the assignment is implicitly
1282 // deleted). For that reason all raw memory operations are diagnosed.
1283 // *p = x;
1284
1285 // Zeroing out is diagnosed because if used with an existing
1286 // (already initialized) object would invalidate the reference.
1287 // Since copy-assignment is deleted verify it's not suggested
1288 // as an alternative. (C++ 11 and later only; C++ 98 is broken).
1289 T (bzero, (p, sizeof *p)); // { dg-warning "clearing an object of type .struct HasReference. with no trivial copy-assignment \\\[" "c++ 11 and later" { target { c++11 } } }
1290 // { dg-warning "clearing an object of type .struct HasReference. with no trivial copy-assignment" "c++ 98" { target { c++98_only } } .-1 }
1291 T (bzero, (p, n)); // { dg-warning "bzero" }
1292 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
1293 T (memset, (p, 0, n)); // { dg-warning "memset" }
1294 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
1295 T (memset, (p, 1, n)); // { dg-warning "memset" }
1296 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
1297 T (memset, (p, i, n)); // { dg-warning "memset" }
1298
1299 // Copying is also diagnosed.
1300 T (memcpy, (p, &x, sizeof *p)); // { dg-warning "writing to an object of type .struct HasReference. with no trivial copy-assignment; use copy-initialization instead" "c++ 11 and later" { target { c++11 } } }
1301 // { dg-warning "writing to an object of type .struct HasReference. with no trivial copy-assignment" "c++ 98" { target { c++98_only } } .-1 }
1302 T (memcpy, (p, &x, n)); // { dg-warning "memcpy" }
1303 T (memcpy, (p, q, sizeof *p)); // { dg-warning "memcpy" }
1304 T (memcpy, (p, q, n)); // { dg-warning "memcpy" }
1305 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
1306 T (memcpy, (p, s, n)); // { dg-warning "memcpy" }
1307 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
1308 T (memcpy, (p, b, n)); // { dg-warning "memcpy" }
1309 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
1310 T (memcpy, (p, ss, n)); // { dg-warning "memcpy" }
1311 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
1312 T (memcpy, (p, ws, n)); // { dg-warning "memcpy" }
1313 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
1314 T (memcpy, (p, ia, n)); // { dg-warning "memcpy" }
1315
1316 T (memmove, (p, &x, sizeof *p)); // { dg-warning "memmove" }
1317 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
1318 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
1319 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
1320 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1321 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1322 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1323
1324 T (mempcpy, (p, &x, sizeof *p)); // { dg-warning "mempcpy" }
1325 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
1326 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
1327 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
1328 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1329 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1330 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1331
1332 // Reallocating is not diagnosed because a type with a reference
1333 // is (perhaps surprisingly) trivially copyable. It is diagnosed
1334 // in C++ 98 because of a bug, but it seems like it should be
1335 // diagnosed in all modes.
1336 T (q = realloc, (p, 1)); // { dg-warning "realloc" "c++ 98" { target { c++98_only } } }
1337 // { dg-warning "moving an object of non-trivial type" "" { target c++11 } .-1 }
1338 T (q = realloc, (p, n)); // { dg-warning "realloc" "c++ 98" { target { c++98_only } } }
1339 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" "c++ 98" { target { c++98_only } } }
1340 }
1341
1342 #endif
1343
1344 #if !defined TEST || TEST == TEST_HAS_MEM_DATA_PTR
1345
1346 /* HasMemDataPtr should only be initialized using aggregate initializatoon
1347 and not cleared by bzero or written into using memset because its
1348 representation is different from ordinary scalars (a null member data
1349 pointer is all ones). It can be copied into using memcpy from an object
1350 of the same type or from a character buffer. */
1351 struct HasMemDataPtr { int HasMemDataPtr::*p; };
1352
1353 void test (HasMemDataPtr *p, const HasMemDataPtr &x,
1354 const void *q, const unsigned char *s, const std::byte *b,
1355 const signed char *ss, const char16_t *ws,
1356 const int ia[])
1357 {
1358 const int i = *ia;
1359 const size_t n = *ia;
1360
1361 // Zeroing out is diagnosed because a null member data pointer has
1362 // a representation that's all bits set.
1363 T (bzero, (p, sizeof *p)); // { dg-warning "clearing an object of type .struct HasMemDataPtr. containing a pointer-to-member" }
1364 T (bzero, (p, n)); // { dg-warning "bzero" }
1365 T (memset, (p, 0, sizeof *p)); // { dg-warning "memset" }
1366 T (memset, (p, 0, n)); // { dg-warning "memset" }
1367 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
1368 T (memset, (p, 1, n)); // { dg-warning "memset" }
1369 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
1370 T (memset, (p, i, n)); // { dg-warning "memset" }
1371
1372 // Copying is not diagnosed.
1373 T (memcpy, (p, &x, sizeof *p));
1374 T (memcpy, (p, &x, n));
1375 T (memcpy, (p, q, sizeof *p));
1376 T (memcpy, (p, q, n));
1377 T (memcpy, (p, s, sizeof *p));
1378 T (memcpy, (p, s, n));
1379 T (memcpy, (p, b, sizeof *p));
1380 T (memcpy, (p, b, n));
1381 T (memcpy, (p, ss, sizeof *p));
1382 T (memcpy, (p, ss, n));
1383 T (memcpy, (p, ws, sizeof *p));
1384 T (memcpy, (p, ws, n));
1385 T (memcpy, (p, ia, sizeof *p));
1386 T (memcpy, (p, ia, n));
1387
1388 T (memmove, (p, &x, sizeof *p));
1389 T (memmove, (p, q, sizeof *p));
1390 T (memmove, (p, s, sizeof *p));
1391 T (memmove, (p, b, sizeof *p));
1392 T (memmove, (p, ss, sizeof *p));
1393 T (memmove, (p, ws, sizeof *p));
1394 T (memmove, (p, ia, sizeof *p));
1395
1396 T (mempcpy, (p, &x, sizeof *p));
1397 T (mempcpy, (p, q, sizeof *p));
1398 T (mempcpy, (p, s, sizeof *p));
1399 T (mempcpy, (p, b, sizeof *p));
1400 T (mempcpy, (p, ss, sizeof *p));
1401 T (mempcpy, (p, ws, sizeof *p));
1402 T (mempcpy, (p, ia, sizeof *p));
1403
1404 // Reallocating is the same as calling memcpy.
1405 T (q = realloc, (p, 1));
1406 T (q = realloc, (p, n));
1407 T (q = realloc, (p, sizeof *p));
1408 T (q = realloc, (p, sizeof *p + 1));
1409 }
1410
1411 #endif
1412
1413 #if !defined TEST || TEST == TEST_HAS_SOME_PRIVATE_DATA
1414
1415 /* HasSomePrivateData can be initialized using value initialization
1416 and should not be written to using memset with a non-zero argument.
1417 Doing otherwise would break encapsulation. */
1418 struct HasSomePrivateData { char a[2]; private: char b[2]; };
1419
1420 void test (HasSomePrivateData *p, const HasSomePrivateData &x,
1421 const void *q, const unsigned char *s, const std::byte *b,
1422 const signed char *ss, const char16_t *ws,
1423 const int ia[])
1424 {
1425 const int i = *ia;
1426 const size_t n = *ia;
1427
1428 // Zeroing out is not diagnosed because it's equivalent to value
1429 // initialization.
1430 T (bzero, (p, sizeof *p));
1431 T (memset, (p, 0, sizeof *p));
1432 // Calling memset with a (possibly) non-zero argument is diagnosed
1433 // because it breaks encapsulation.
1434 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
1435 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
1436
1437 // Calling memcpy to copy from an object of the same type or from
1438 // a character or void buffer is not diagnosed because that's what
1439 // copy construction and copy assignment do.
1440 T (memcpy, (p, &x, sizeof *p));
1441 T (memcpy, (p, &x, n));
1442 T (memcpy, (p, q, sizeof *p));
1443 T (memcpy, (p, s, sizeof *p));
1444 T (memcpy, (p, b, sizeof *p));
1445 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
1446 T (memcpy, (p, ss, n)); // { dg-warning "memcpy" }
1447 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
1448 T (memcpy, (p, ws, n)); // { dg-warning "memcpy" }
1449 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
1450 T (memcpy, (p, ia, n)); // { dg-warning "memcpy" }
1451
1452 // Same as memcpy above.
1453 T (memmove, (p, &x, sizeof *p));
1454 T (memmove, (p, &x, n));
1455 T (memmove, (p, q, sizeof *p));
1456 T (memmove, (p, s, sizeof *p));
1457 T (memmove, (p, b, sizeof *p));
1458 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1459 T (memmove, (p, ss, n)); // { dg-warning "memmove" }
1460 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1461 T (memmove, (p, ws, n)); // { dg-warning "memmove" }
1462 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1463 T (memmove, (p, ia, n)); // { dg-warning "memmove" }
1464
1465 // Same as memcpy above.
1466 T (mempcpy, (p, &x, sizeof *p));
1467 T (mempcpy, (p, &x, n));
1468 T (mempcpy, (p, q, sizeof *p));
1469 T (mempcpy, (p, q, n));
1470 T (mempcpy, (p, s, sizeof *p));
1471 T (mempcpy, (p, s, n));
1472 T (mempcpy, (p, b, sizeof *p));
1473 T (mempcpy, (p, b, n));
1474 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1475 T (mempcpy, (p, ss, n)); // { dg-warning "mempcpy" }
1476 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1477 T (mempcpy, (p, ws, n)); // { dg-warning "mempcpy" }
1478 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1479 T (mempcpy, (p, ia, n)); // { dg-warning "mempcpy" }
1480
1481 // Reallocating is the same as calling memcpy except that partial
1482 // copies are not diagnosed.
1483 T (q = realloc, (p, 1));
1484 T (q = realloc, (p, n));
1485 T (q = realloc, (p, sizeof *p));
1486 T (q = realloc, (p, sizeof *p + 1));
1487 }
1488
1489 #endif
1490
1491 #if !defined TEST || TEST == TEST_HAS_SOME_PROTECTED_DATA
1492
1493 /* Similarly to HasSomePrivateData, HasSomeProtectedData can be
1494 initialized using value initialization and should not be written
1495 to using memset with a non-zero argument. Doing otherwise would
1496 break encapsulation. */
1497 struct HasSomeProtectedData { char a[2]; protected: char b[2]; };
1498
1499 void test (HasSomeProtectedData *p, const HasSomeProtectedData &x,
1500 const void *q, const unsigned char *s, const std::byte *b,
1501 const signed char *ss, const char16_t *ws,
1502 const int ia[])
1503 {
1504 const int i = *ia;
1505 const size_t n = *ia;
1506
1507 // Zeroing out is not diagnosed because it's equivalent to value
1508 // initialization.
1509 T (bzero, (p, sizeof *p));
1510 T (memset, (p, 0, sizeof *p));
1511 // Calling memset with a (possibly) non-zero argument is diagnosed
1512 // because it breaks encapsulation.
1513 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
1514 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
1515
1516 // Calling memcpy to copy from an object of the same type or from
1517 // a character or void buffer is not diagnosed because that's what
1518 // copy construction and copy assignment do.
1519 T (memcpy, (p, &x, sizeof *p));
1520 T (memcpy, (p, &x, n));
1521 T (memcpy, (p, q, sizeof *p));
1522 T (memcpy, (p, s, sizeof *p));
1523 T (memcpy, (p, b, sizeof *p));
1524 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
1525 T (memcpy, (p, ss, n)); // { dg-warning "memcpy" }
1526 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
1527 T (memcpy, (p, ws, n)); // { dg-warning "memcpy" }
1528 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
1529 T (memcpy, (p, ia, n)); // { dg-warning "memcpy" }
1530
1531 // Same as memcpy above.
1532 T (memmove, (p, &x, sizeof *p));
1533 T (memmove, (p, &x, n));
1534 T (memmove, (p, q, sizeof *p));
1535 T (memmove, (p, s, sizeof *p));
1536 T (memmove, (p, b, sizeof *p));
1537 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1538 T (memmove, (p, ss, n)); // { dg-warning "memmove" }
1539 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1540 T (memmove, (p, ws, n)); // { dg-warning "memmove" }
1541 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1542 T (memmove, (p, ia, n)); // { dg-warning "memmove" }
1543
1544 // Same as memcpy above.
1545 T (mempcpy, (p, &x, sizeof *p));
1546 T (mempcpy, (p, &x, n));
1547 T (mempcpy, (p, q, sizeof *p));
1548 T (mempcpy, (p, q, n));
1549 T (mempcpy, (p, s, sizeof *p));
1550 T (mempcpy, (p, s, n));
1551 T (mempcpy, (p, b, sizeof *p));
1552 T (mempcpy, (p, b, n));
1553 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1554 T (mempcpy, (p, ss, n)); // { dg-warning "mempcpy" }
1555 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1556 T (mempcpy, (p, ws, n)); // { dg-warning "mempcpy" }
1557 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1558 T (mempcpy, (p, ia, n)); // { dg-warning "mempcpy" }
1559
1560 // Reallocating is the same as calling memcpy except that partial
1561 // copies are not diagnosed.
1562 T (q = realloc, (p, 1));
1563 T (q = realloc, (p, n));
1564 T (q = realloc, (p, sizeof *p));
1565 T (q = realloc, (p, sizeof *p + 1));
1566 }
1567
1568 #endif
1569
1570 #if !defined TEST || TEST == TEST_HAS_ALL_PRIVATE_DATA
1571
1572 /* Similarly to HasSomePrivateData, HasAllPrivateData should only be
1573 initialized using value initializatoon and should not be written
1574 to using memset with non-zero argument. They are tested separately
1575 because unlike the former classes, these are standard layout. */
1576 struct HasAllPrivateData { private: char a[4]; };
1577
1578 void test (HasAllPrivateData *p, const HasAllPrivateData &x,
1579 const void *q, const unsigned char *s, const std::byte *b,
1580 const signed char *ss, const char16_t *ws,
1581 const int ia[])
1582 {
1583 const int i = *ia;
1584 const size_t n = *ia;
1585
1586 // Zeroing out is not diagnosed because it's equivalent to value
1587 // initialization.
1588 T (bzero, (p, sizeof *p));
1589 T (memset, (p, 0, sizeof *p));
1590 // Calling memset with a (possibly) non-zero argument is diagnosed
1591 // because it breaks encapsulation.
1592 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
1593 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
1594
1595 // Calling memcpy to copy from an object of the same type or from
1596 // a character or void buffer is not diagnosed because that's what
1597 // copy construction and copy assignment do.
1598 T (memcpy, (p, &x, sizeof *p));
1599 T (memcpy, (p, &x, n));
1600 T (memcpy, (p, q, sizeof *p));
1601 T (memcpy, (p, s, sizeof *p));
1602 T (memcpy, (p, b, sizeof *p));
1603 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
1604 T (memcpy, (p, ss, n)); // { dg-warning "memcpy" }
1605 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
1606 T (memcpy, (p, ws, n)); // { dg-warning "memcpy" }
1607 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
1608 T (memcpy, (p, ia, n)); // { dg-warning "memcpy" }
1609
1610 // Same as memcpy above.
1611 T (memmove, (p, &x, sizeof *p));
1612 T (memmove, (p, &x, n));
1613 T (memmove, (p, q, sizeof *p));
1614 T (memmove, (p, s, sizeof *p));
1615 T (memmove, (p, b, sizeof *p));
1616 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1617 T (memmove, (p, ss, n)); // { dg-warning "memmove" }
1618 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1619 T (memmove, (p, ws, n)); // { dg-warning "memmove" }
1620 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1621 T (memmove, (p, ia, n)); // { dg-warning "memmove" }
1622
1623 // Same as memcpy above.
1624 T (mempcpy, (p, &x, sizeof *p));
1625 T (mempcpy, (p, &x, n));
1626 T (mempcpy, (p, q, sizeof *p));
1627 T (mempcpy, (p, q, n));
1628 T (mempcpy, (p, s, sizeof *p));
1629 T (mempcpy, (p, s, n));
1630 T (mempcpy, (p, b, sizeof *p));
1631 T (mempcpy, (p, b, n));
1632 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1633 T (mempcpy, (p, ss, n)); // { dg-warning "mempcpy" }
1634 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1635 T (mempcpy, (p, ws, n)); // { dg-warning "mempcpy" }
1636 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1637 T (mempcpy, (p, ia, n)); // { dg-warning "mempcpy" }
1638
1639 // Reallocating is the same as calling memcpy except that partial
1640 // copies are not diagnosed.
1641 T (q = realloc, (p, 1));
1642 T (q = realloc, (p, n));
1643 T (q = realloc, (p, sizeof *p));
1644 T (q = realloc, (p, sizeof *p + 1));
1645 }
1646
1647 #endif
1648
1649 #if !defined TEST || TEST == TEST_HAS_ALL_PROTECTED_DATA
1650
1651 /* Similarly to HasSomeProtectedData, HasAllProtectedData should only
1652 be initialized using value initializatoon and should not be written
1653 to using memset with non-zero argument. They are tested separately
1654 because unlike the former classes, these are standard layout. */
1655 struct HasAllProtectedData { protected: char a[4]; };
1656
1657 void test (HasAllProtectedData *p, const HasAllProtectedData &x,
1658 const void *q, const unsigned char *s, const std::byte *b,
1659 const signed char *ss, const char16_t *ws,
1660 const int ia[])
1661 {
1662 const int i = *ia;
1663 const size_t n = *ia;
1664
1665 // Zeroing out is not diagnosed because it's equivalent to value
1666 // initialization.
1667 T (bzero, (p, sizeof *p));
1668 T (memset, (p, 0, sizeof *p));
1669 // Calling memset with a (possibly) non-zero argument is diagnosed
1670 // because it breaks encapsulation.
1671 T (memset, (p, 1, sizeof *p)); // { dg-warning "memset" }
1672 T (memset, (p, i, sizeof *p)); // { dg-warning "memset" }
1673
1674 // Calling memcpy to copy from an object of the same type or from
1675 // a character or void buffer is not diagnosed because that's what
1676 // copy construction and copy assignment do.
1677 T (memcpy, (p, &x, sizeof *p));
1678 T (memcpy, (p, &x, n));
1679 T (memcpy, (p, q, sizeof *p));
1680 T (memcpy, (p, s, sizeof *p));
1681 T (memcpy, (p, b, sizeof *p));
1682 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "memcpy" }
1683 T (memcpy, (p, ss, n)); // { dg-warning "memcpy" }
1684 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "memcpy" }
1685 T (memcpy, (p, ws, n)); // { dg-warning "memcpy" }
1686 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "memcpy" }
1687 T (memcpy, (p, ia, n)); // { dg-warning "memcpy" }
1688
1689 // Same as memcpy above.
1690 T (memmove, (p, &x, sizeof *p));
1691 T (memmove, (p, &x, n));
1692 T (memmove, (p, q, sizeof *p));
1693 T (memmove, (p, s, sizeof *p));
1694 T (memmove, (p, b, sizeof *p));
1695 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1696 T (memmove, (p, ss, n)); // { dg-warning "memmove" }
1697 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1698 T (memmove, (p, ws, n)); // { dg-warning "memmove" }
1699 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1700 T (memmove, (p, ia, n)); // { dg-warning "memmove" }
1701
1702 // Same as memcpy above.
1703 T (mempcpy, (p, &x, sizeof *p));
1704 T (mempcpy, (p, &x, n));
1705 T (mempcpy, (p, q, sizeof *p));
1706 T (mempcpy, (p, q, n));
1707 T (mempcpy, (p, s, sizeof *p));
1708 T (mempcpy, (p, s, n));
1709 T (mempcpy, (p, b, sizeof *p));
1710 T (mempcpy, (p, b, n));
1711 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1712 T (mempcpy, (p, ss, n)); // { dg-warning "mempcpy" }
1713 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1714 T (mempcpy, (p, ws, n)); // { dg-warning "mempcpy" }
1715 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1716 T (mempcpy, (p, ia, n)); // { dg-warning "mempcpy" }
1717
1718 // Reallocating is the same as calling memcpy except that partial
1719 // copies are not diagnosed.
1720 T (q = realloc, (p, 1));
1721 T (q = realloc, (p, n));
1722 T (q = realloc, (p, sizeof *p));
1723 T (q = realloc, (p, sizeof *p + 1));
1724 }
1725
1726 #endif
1727
1728 #if !defined TEST || TEST == TEST_DEFAULT_CTOR_PRIVATE_ASSIGN
1729
1730 /* Used to verify suggested alternatives. */
1731 struct HasDefaultPrivateAssign
1732 {
1733 char a[4];
1734 HasDefaultPrivateAssign ();
1735 private:
1736 void operator= (HasDefaultPrivateAssign&);
1737 };
1738
1739 void test (HasDefaultPrivateAssign *p, const HasDefaultPrivateAssign &x,
1740 const void *q, const unsigned char *s, const std::byte *b,
1741 const signed char *ss, const char16_t *ws,
1742 const int ia[])
1743 {
1744 const int i = *ia;
1745 const size_t n = *ia;
1746
1747 // HasDefaultPrivateAssign isn't trivial or assignable. Verify
1748 // that the alternative suggested in the warning is to use copy or
1749 // default but not assignment.
1750 T (bzero, (p, sizeof *p)); // { dg-warning "bzero(\[^\n\r\]*). clearing an object of type .struct HasDefaultPrivateAssign. with no trivial copy-assignment; use value-initialization instead" "c++ 11 and later" { target { c++11 } } }
1751 // { dg-warning "bzero(\[^\n\r\]*). clearing an object of type .struct HasDefaultPrivateAssign. with no trivial copy-assignment; use value-initialization instead" "c++ 98" { target { c++98_only } } .-1 }
1752
1753 T (memset, (p, 0, sizeof *p)); // { dg-warning ".void\\* memset(\[^\n\r\]*). clearing an object of type .struct HasDefaultPrivateAssign. with (deleted|no trivial) copy-assignment; use value-initialization instead" }
1754
1755 T (memset, (p, 1, sizeof *p)); // { dg-warning ".void\\* memset(\[^\n\r\]*). writing to an object of type .struct HasDefaultPrivateAssign. with (deleted|no trivial) copy-assignment" }
1756
1757 T (memset, (p, i, sizeof *p)); // { dg-warning ".void\\* memset(\[^\n\r\]*). writing to an object of type .struct HasDefaultPrivateAssign. with (deleted|no trivial) copy-assignment" }
1758
1759 // Copying from another object of the same type is diagnosed because
1760 // the copy assignment is inaccessible. Verify that the suggested
1761 // alternative is not copy assignment (C++ 98 is busted).
1762 T (memcpy, (p, &x, sizeof *p)); // { dg-warning ".void\\* memcpy(\[^\n\r\]*). writing to an object of type .struct HasDefaultPrivateAssign. with no trivial copy-assignment; use copy-initialization instead" "c++11 and later" { target c++11 } }
1763 // { dg-warning ".void\\* memcpy(\[^\n\r\]*). writing to an object of type .struct HasDefaultPrivateAssign. with no trivial copy-assignment" "c++98" { target c++98_only } .-1 }
1764 T (memcpy, (p, &x, n)); // { dg-warning "memcpy" }
1765
1766 // Similarly for copying from a void* or character buffer.
1767 T (memcpy, (p, q, sizeof *p)); // { dg-warning ".void\\* memcpy(\[^\n\r\]*). writing to an object of type .struct HasDefaultPrivateAssign. with no trivial copy-assignment; use copy-initialization instead" "c++11 and later" { target c++11 } }
1768 // { dg-warning ".void\\* memcpy(\[^\n\r\]*). writing to an object of type .struct HasDefaultPrivateAssign. with no trivial copy-assignment" "c++98" { target c++98_only } ,-1 }
1769 T (memcpy, (p, q, n)); // { dg-warning "memcpy" }
1770 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
1771 T (memcpy, (p, s, n)); // { dg-warning "memcpy" }
1772 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
1773 T (memcpy, (p, b, n)); // { dg-warning "memcpy" }
1774
1775 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
1776 T (memmove, (p, q, n)); // { dg-warning "memmove" }
1777 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
1778 T (memmove, (p, s, n)); // { dg-warning "memmove" }
1779 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
1780 T (memmove, (p, b, n)); // { dg-warning "memmove" }
1781
1782 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
1783 T (mempcpy, (p, q, n)); // { dg-warning "mempcpy" }
1784 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
1785 T (mempcpy, (p, s, n)); // { dg-warning "mempcpy" }
1786 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
1787 T (mempcpy, (p, b, n)); // { dg-warning "mempcpy" }
1788
1789 // Same for partial copies are diagnosed.
1790 T (memcpy, (p, &x, 1)); // { dg-warning "writing to an object of type .struct HasDefaultPrivateAssign. with (deleted|no trivial) copy-assignment" } */
1791 T (memmove, (p, q, 2)); // { dg-warning "memmove" } */
1792 T (mempcpy, (p, q, 3)); // { dg-warning "mempcpy" } */
1793
1794 // Otherwise, copying from an object of an unrelated type is diagnosed.
1795 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "writing to an object of type .struct HasDefaultPrivateAssign. with (deleted|no trivial) copy-assignment." }
1796 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "writing to an object of type .struct HasDefaultPrivateAssign. with (deleted|no trivial) copy-assignment." }
1797 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "writing to an object of type .struct HasDefaultPrivateAssign. with (deleted|no trivial) copy-assignment." }
1798 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1799 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1800 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1801 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1802 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1803 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1804
1805 // Reallocating is the same as calling memcpy.
1806 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
1807 T (q = realloc, (p, n)); // { dg-warning "realloc" }
1808 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
1809 }
1810
1811 #endif
1812
1813 #if !defined TEST || TEST == TEST_DEFAULT_CTOR_DELETED_ASSIGN
1814
1815 /* Used to verify suggested alternatives. */
1816 struct HasDefaultDeletedAssign
1817 {
1818 char a[4];
1819 HasDefaultDeletedAssign ();
1820 private:
1821 void operator= (HasDefaultDeletedAssign&);
1822 };
1823
1824 void test (HasDefaultDeletedAssign *p, const HasDefaultDeletedAssign &x,
1825 const void *q, const unsigned char *s, const std::byte *b,
1826 const signed char *ss, const char16_t *ws,
1827 const int ia[])
1828 {
1829 const int i = *ia;
1830 const size_t n = *ia;
1831
1832 // HasDefaultDeletedAssign isn't trivial or assignable. Verify
1833 // that the alternative suggested in the warning is to use copy or
1834 // default but not assignment.
1835 T (bzero, (p, sizeof *p)); // { dg-warning "bzero(\[^\n\r\]*). clearing an object of type .struct HasDefaultDeletedAssign. with no trivial copy-assignment; use value-initialization instead" "c++ 11 and later" { target { c++11 } } }
1836 // { dg-warning "bzero(\[^\n\r\]*). clearing an object of type .struct HasDefaultDeletedAssign. with no trivial copy-assignment; use value-initialization instead" "c++ 98" { target { c++98_only } } .-1 }
1837
1838 T (memset, (p, 0, sizeof *p)); // { dg-warning ".void\\* memset(\[^\n\r\]*). clearing an object of type .struct HasDefaultDeletedAssign. with (deleted|no trivial) copy-assignment; use value-initialization instead" }
1839
1840 T (memset, (p, 1, sizeof *p)); // { dg-warning ".void\\* memset(\[^\n\r\]*). writing to an object of type .struct HasDefaultDeletedAssign. with (deleted|no trivial) copy-assignment" }
1841
1842 T (memset, (p, i, sizeof *p)); // { dg-warning ".void\\* memset(\[^\n\r\]*). writing to an object of type .struct HasDefaultDeletedAssign. with (deleted|no trivial) copy-assignment" }
1843
1844 // Copying from another object of the same type is diagnosed because
1845 // the copy assignment is inaccessible. Verify that the suggested
1846 // alternative is not copy assignment (C++ 98 is busted).
1847 T (memcpy, (p, &x, sizeof *p)); // { dg-warning ".void\\* memcpy(\[^\n\r\]*). writing to an object of type .struct HasDefaultDeletedAssign. with no trivial copy-assignment; use copy-initialization instead" "c++11 and later" { target c++11 } }
1848 // { dg-warning ".void\\* memcpy(\[^\n\r\]*). writing to an object of type .struct HasDefaultDeletedAssign. with no trivial copy-assignment" "c++98" { target c++98_only } .-1 }
1849 T (memcpy, (p, &x, n)); // { dg-warning "memcpy" }
1850
1851 // Similarly for copying from a void* or character buffer.
1852 T (memcpy, (p, q, sizeof *p)); // { dg-warning ".void\\* memcpy(\[^\n\r\]*). writing to an object of type .struct HasDefaultDeletedAssign. with no trivial copy-assignment; use copy-initialization instead" "c++11 and later" { target c++11 } }
1853 // { dg-warning ".void\\* memcpy(\[^\n\r\]*). writing to an object of type .struct HasDefaultDeletedAssign. with no trivial copy-assignment" "c++98" { target c++98_only } ,-1 }
1854 T (memcpy, (p, q, n)); // { dg-warning "memcpy" }
1855 T (memcpy, (p, s, sizeof *p)); // { dg-warning "memcpy" }
1856 T (memcpy, (p, s, n)); // { dg-warning "memcpy" }
1857 T (memcpy, (p, b, sizeof *p)); // { dg-warning "memcpy" }
1858 T (memcpy, (p, b, n)); // { dg-warning "memcpy" }
1859
1860 T (memmove, (p, q, sizeof *p)); // { dg-warning "memmove" }
1861 T (memmove, (p, q, n)); // { dg-warning "memmove" }
1862 T (memmove, (p, s, sizeof *p)); // { dg-warning "memmove" }
1863 T (memmove, (p, s, n)); // { dg-warning "memmove" }
1864 T (memmove, (p, b, sizeof *p)); // { dg-warning "memmove" }
1865 T (memmove, (p, b, n)); // { dg-warning "memmove" }
1866
1867 T (mempcpy, (p, q, sizeof *p)); // { dg-warning "mempcpy" }
1868 T (mempcpy, (p, q, n)); // { dg-warning "mempcpy" }
1869 T (mempcpy, (p, s, sizeof *p)); // { dg-warning "mempcpy" }
1870 T (mempcpy, (p, s, n)); // { dg-warning "mempcpy" }
1871 T (mempcpy, (p, b, sizeof *p)); // { dg-warning "mempcpy" }
1872 T (mempcpy, (p, b, n)); // { dg-warning "mempcpy" }
1873
1874 // Same for partial copies are diagnosed.
1875 T (memcpy, (p, &x, 1)); // { dg-warning "writing to an object of type .struct HasDefaultDeletedAssign. with (deleted|no trivial) copy-assignment" } */
1876 T (memmove, (p, q, 2)); // { dg-warning "memmove" } */
1877 T (mempcpy, (p, q, 3)); // { dg-warning "mempcpy" } */
1878
1879 // Otherwise, copying from an object of an unrelated type is diagnosed.
1880 T (memcpy, (p, ss, sizeof *p)); // { dg-warning "writing to an object of type .struct HasDefaultDeletedAssign. with (deleted|no trivial) copy-assignment." }
1881 T (memcpy, (p, ws, sizeof *p)); // { dg-warning "writing to an object of type .struct HasDefaultDeletedAssign. with (deleted|no trivial) copy-assignment." }
1882 T (memcpy, (p, ia, sizeof *p)); // { dg-warning "writing to an object of type .struct HasDefaultDeletedAssign. with (deleted|no trivial) copy-assignment." }
1883 T (memmove, (p, ss, sizeof *p)); // { dg-warning "memmove" }
1884 T (memmove, (p, ws, sizeof *p)); // { dg-warning "memmove" }
1885 T (memmove, (p, ia, sizeof *p)); // { dg-warning "memmove" }
1886 T (mempcpy, (p, ss, sizeof *p)); // { dg-warning "mempcpy" }
1887 T (mempcpy, (p, ws, sizeof *p)); // { dg-warning "mempcpy" }
1888 T (mempcpy, (p, ia, sizeof *p)); // { dg-warning "mempcpy" }
1889
1890 // Reallocating is the same as calling memcpy.
1891 T (q = realloc, (p, 1)); // { dg-warning "realloc" }
1892 T (q = realloc, (p, n)); // { dg-warning "realloc" }
1893 T (q = realloc, (p, sizeof *p)); // { dg-warning "realloc" }
1894 }
1895
1896 #endif
1897
1898 #if !defined TEST || TEST == TEST_EXPRESSION
1899
1900 void test_expr (int i)
1901 {
1902 struct TestClass { TestClass () { } };
1903 TestClass a, b;
1904
1905 static void *p;
1906
1907 T (bzero, (i < 0 ? &a : &b, 1)); // { dg-warning "bzero" }
1908 }
1909
1910 #endif
1911
1912 #if !defined TEST || TEST == TEST_CTOR
1913
1914 void test_ctor ()
1915 {
1916 #undef T
1917 #define T(fn, arglist) (fn arglist, sink (this))
1918
1919 static void *p;
1920
1921 struct TestBase
1922 {
1923 TestBase ()
1924 {
1925 /* A ctor of a base class with no virtual function can do whatever
1926 it wants. */
1927 T (bzero, (this, sizeof *this));
1928 T (memset, (this, 0, sizeof *this));
1929 T (memcpy, (this, p, sizeof *this));
1930 T (memmove, (this, p, sizeof *this));
1931 T (mempcpy, (this, p, sizeof *this));
1932 }
1933
1934 ~TestBase ()
1935 {
1936 /* A dtor of a base class with no virtual function can do whatever
1937 it wants. */
1938 T (bzero, (this, sizeof *this));
1939 T (memset, (this, 0, sizeof *this));
1940 T (memcpy, (this, p, sizeof *this));
1941 T (memmove, (this, p, sizeof *this));
1942 T (mempcpy, (this, p, sizeof *this));
1943 }
1944 };
1945
1946 struct TestBaseVtable
1947 {
1948 TestBaseVtable ()
1949 {
1950 /* A ctor of a base class with virtual function is treated
1951 as an ordinary function. */
1952 T (bzero, (this, sizeof *this)); // { dg-warning "bzero" }
1953 T (memset, (this, 0, sizeof *this)); // { dg-warning "memset" }
1954 T (memcpy, (this, p, sizeof *this)); // { dg-warning "memcpy" }
1955 T (memmove, (this, p, sizeof *this)); // { dg-warning "memmove" }
1956 T (mempcpy, (this, p, sizeof *this)); // { dg-warning "mempcpy" }
1957 }
1958
1959 ~TestBaseVtable ()
1960 {
1961 /* A dtor of a base class with virtual function is treated
1962 as an ordinary function. */
1963 T (bzero, (this, sizeof *this)); // { dg-warning "bzero" }
1964 T (memset, (this, 0, sizeof *this)); // { dg-warning "memset" }
1965 T (memcpy, (this, p, sizeof *this)); // { dg-warning "memcpy" }
1966 T (memmove, (this, p, sizeof *this)); // { dg-warning "memmove" }
1967 T (mempcpy, (this, p, sizeof *this)); // { dg-warning "mempcpy" }
1968 }
1969
1970 virtual void foo ();
1971 };
1972
1973 struct TestDerived: HasDefault
1974 {
1975 TestDerived ()
1976 {
1977 /* A derived class ctor is treated as an ordinary function. */
1978 T (bzero, (this, sizeof *this)); // { dg-warning "bzero" }
1979 T (memset, (this, 0, sizeof *this)); // { dg-warning "memset" }
1980 T (memcpy, (this, p, sizeof *this));
1981 T (memmove, (this, p, sizeof *this));
1982 T (mempcpy, (this, p, sizeof *this));
1983 }
1984 };
1985
1986 struct TestDerivedDtor: HasDefault
1987 {
1988 ~TestDerivedDtor ()
1989 {
1990 /* A derived class dtor is treated as an ordinary function though
1991 it probably shouldn't be unless the base dtor is trivial. But
1992 it doesn't seem worth the trouble. */
1993 T (bzero, (this, sizeof *this)); // { dg-warning "bzero" }
1994 T (memset, (this, 0, sizeof *this)); // { dg-warning "memset" }
1995 T (memcpy, (this, p, sizeof *this)); // { dg-warning "memcpy" }
1996 T (memmove, (this, p, sizeof *this)); // { dg-warning "memmove" }
1997 T (mempcpy, (this, p, sizeof *this)); // { dg-warning "mempcpy" }
1998 }
1999 };
2000 }
2001
2002 #endif
2003
2004 // { dg-prune-output "defaulted and deleted functions" }