1 /* PR c++/84850 - -Wclass-memaccess on a memcpy in a copy assignment
2 operator with no nontrivial bases or members
4 { dg-options "-Wclass-memaccess -ftrack-macro-expansion=0" } */
6 typedef __SIZE_TYPE__ size_t;
8 extern "C" void* memcpy (void*, const void*, size_t);
9 extern "C" void* memset (void*, int, size_t);
12 struct EmptyClass { };
30 /* Verify that raw memory accesses from non-static members of a class with
31 an empty base is not diagnosed. */
33 struct EmptyWithBase: EmptyClass<0>, EmptyClass<1>, EmptyClass<2>
37 memset (this, 0, sizeof *this);
40 EmptyWithBase (const EmptyWithBase &x)
42 memcpy (this, &x, sizeof *this);
47 memset (this, 0, sizeof *this);
50 void operator= (const EmptyWithBase &x)
52 memcpy (this, &x, sizeof *this);
57 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
60 void copy (const void *p)
62 memcpy (this, p, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
65 static void bad_clear (EmptyWithBase &x)
67 memset (&x, 0, sizeof x); // { dg-warning "\\\[-Wclass-memaccess" }
70 static void bad_copy (EmptyWithBase &x, const void *p)
72 memcpy (&x, p, sizeof x); // { dg-warning "\\\[-Wclass-memaccess" }
76 /* Verify that raw memory accesses from non-static members of a class with
77 all trivial members is not diagnosed. */
79 struct HasTrivialMembers
87 TrivialClass<1> trivial;
91 memset (this, 0, sizeof *this);
94 HasTrivialMembers (const HasTrivialMembers &x)
96 memcpy (this, &x, sizeof *this);
101 memset (this, 0, sizeof *this);
104 void operator= (const HasTrivialMembers &x)
106 memcpy (this, &x, sizeof *this);
111 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
114 void copy (const void *p)
116 memcpy (this, p, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
119 static void bad_clear (HasTrivialMembers &x)
121 memset (&x, 0, sizeof x); // { dg-warning "\\\[-Wclass-memaccess" }
124 static void bad_copy (HasTrivialMembers &x, const void *p)
126 memcpy (&x, p, sizeof x); // { dg-warning "\\\[-Wclass-memaccess" }
130 /* Verify that raw memory accesses from non-static members of a class with
131 a trivial base class and no non-trivial members is not diagnosed. */
133 struct HasTrivialBase: TrivialClass<1>
135 TrivialClass<2> a[2];
139 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
142 HasTrivialBase (const HasTrivialBase &x)
144 memcpy (this, &x, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
149 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
152 void operator= (const HasTrivialBase &x)
154 memcpy (this, &x, sizeof *this);
159 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
164 memcpy (this, p, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
169 struct HasTrivialBases: TrivialClass<1>, TrivialClass<2>
171 TrivialClass<3> a[2];
175 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
178 HasTrivialBases (const HasTrivialBases &x)
180 memcpy (this, &x, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
185 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
188 void operator= (const HasTrivialBases &x)
190 memcpy (this, &x, sizeof *this);
195 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
200 memcpy (this, p, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
204 struct DerivesFromNontrivialClass: HasDefault<1> { };
206 /* Verify that raw memory accesses from members of a class with a non-trivial
207 base class is diagnosed. */
209 struct HasNonTrivialBase: TrivialClass<1>, TrivialClass<2>,
210 DerivesFromNontrivialClass,
211 TrivialClass<3>, TrivialClass<4>
215 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
218 HasNonTrivialBase (const HasNonTrivialBase &x)
220 memcpy (this, &x, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
223 ~HasNonTrivialBase ()
225 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
228 HasNonTrivialBase& operator= (const HasNonTrivialBase &x)
230 memcpy (this, &x, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
236 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
241 memcpy (this, p, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
245 struct DerivesIndidirectlyFromNontrivialClass:
246 TrivialClass<1>, TrivialClass<2>,
247 DerivesFromNontrivialClass,
248 TrivialClass<3>, TrivialClass<4> { };
250 /* Verify that raw memory accesses from members of a class with a non-trivial
251 indirect base class is diagnosed. */
253 struct HasIndirectNonTrivialBase: TrivialClass<5>, TrivialClass<6>,
254 TrivialClass<7>, TrivialClass<8>,
255 DerivesIndidirectlyFromNontrivialClass
257 HasIndirectNonTrivialBase ()
259 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
262 HasIndirectNonTrivialBase (const HasIndirectNonTrivialBase &x)
264 memcpy (this, &x, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
267 ~HasIndirectNonTrivialBase ()
269 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
272 HasIndirectNonTrivialBase& operator= (const HasIndirectNonTrivialBase &x)
274 memcpy (this, &x, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
280 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
285 memcpy (this, p, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }