Debian Codegraph @

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

raw
1 /* PR c++/84850 - -Wclass-memaccess on a memcpy in a copy assignment
2 operator with no nontrivial bases or members
3 { dg-do compile }
4 { dg-options "-Wclass-memaccess -ftrack-macro-expansion=0" } */
5
6 typedef __SIZE_TYPE__ size_t;
7
8 extern "C" void* memcpy (void*, const void*, size_t);
9 extern "C" void* memset (void*, int, size_t);
10
11 template <int>
12 struct EmptyClass { };
13
14 template <int>
15 struct TrivialClass
16 {
17 bool a;
18 int b;
19 void *c;
20 double d[2];
21 void (*e)();
22 };
23
24 template <int>
25 struct HasDefault
26 {
27 HasDefault ();
28 };
29
30 /* Verify that raw memory accesses from non-static members of a class with
31 an empty base is not diagnosed. */
32
33 struct EmptyWithBase: EmptyClass<0>, EmptyClass<1>, EmptyClass<2>
34 {
35 EmptyWithBase ()
36 {
37 memset (this, 0, sizeof *this);
38 }
39
40 EmptyWithBase (const EmptyWithBase &x)
41 {
42 memcpy (this, &x, sizeof *this);
43 }
44
45 ~EmptyWithBase ()
46 {
47 memset (this, 0, sizeof *this);
48 }
49
50 void operator= (const EmptyWithBase &x)
51 {
52 memcpy (this, &x, sizeof *this);
53 }
54
55 void clear ()
56 {
57 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
58 }
59
60 void copy (const void *p)
61 {
62 memcpy (this, p, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
63 }
64
65 static void bad_clear (EmptyWithBase &x)
66 {
67 memset (&x, 0, sizeof x); // { dg-warning "\\\[-Wclass-memaccess" }
68 }
69
70 static void bad_copy (EmptyWithBase &x, const void *p)
71 {
72 memcpy (&x, p, sizeof x); // { dg-warning "\\\[-Wclass-memaccess" }
73 }
74 };
75
76 /* Verify that raw memory accesses from non-static members of a class with
77 all trivial members is not diagnosed. */
78
79 struct HasTrivialMembers
80 {
81 bool a;
82 int b;
83 void *c;
84 double d[2];
85 void (*e)();
86
87 TrivialClass<1> trivial;
88
89 HasTrivialMembers ()
90 {
91 memset (this, 0, sizeof *this);
92 }
93
94 HasTrivialMembers (const HasTrivialMembers &x)
95 {
96 memcpy (this, &x, sizeof *this);
97 }
98
99 ~HasTrivialMembers ()
100 {
101 memset (this, 0, sizeof *this);
102 }
103
104 void operator= (const HasTrivialMembers &x)
105 {
106 memcpy (this, &x, sizeof *this);
107 }
108
109 void clear ()
110 {
111 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
112 }
113
114 void copy (const void *p)
115 {
116 memcpy (this, p, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
117 }
118
119 static void bad_clear (HasTrivialMembers &x)
120 {
121 memset (&x, 0, sizeof x); // { dg-warning "\\\[-Wclass-memaccess" }
122 }
123
124 static void bad_copy (HasTrivialMembers &x, const void *p)
125 {
126 memcpy (&x, p, sizeof x); // { dg-warning "\\\[-Wclass-memaccess" }
127 }
128 };
129
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. */
132
133 struct HasTrivialBase: TrivialClass<1>
134 {
135 TrivialClass<2> a[2];
136
137 HasTrivialBase ()
138 {
139 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
140 }
141
142 HasTrivialBase (const HasTrivialBase &x)
143 {
144 memcpy (this, &x, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
145 }
146
147 ~HasTrivialBase ()
148 {
149 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
150 }
151
152 void operator= (const HasTrivialBase &x)
153 {
154 memcpy (this, &x, sizeof *this);
155 }
156
157 void clear ()
158 {
159 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
160 }
161
162 void copy (void *p)
163 {
164 memcpy (this, p, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
165 }
166 };
167
168
169 struct HasTrivialBases: TrivialClass<1>, TrivialClass<2>
170 {
171 TrivialClass<3> a[2];
172
173 HasTrivialBases ()
174 {
175 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
176 }
177
178 HasTrivialBases (const HasTrivialBases &x)
179 {
180 memcpy (this, &x, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
181 }
182
183 ~HasTrivialBases ()
184 {
185 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
186 }
187
188 void operator= (const HasTrivialBases &x)
189 {
190 memcpy (this, &x, sizeof *this);
191 }
192
193 void clear ()
194 {
195 memset (this, 0, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
196 }
197
198 void copy (void *p)
199 {
200 memcpy (this, p, sizeof *this); // { dg-bogus "\\\[-Wclass-memaccess" }
201 }
202 };
203
204 struct DerivesFromNontrivialClass: HasDefault<1> { };
205
206 /* Verify that raw memory accesses from members of a class with a non-trivial
207 base class is diagnosed. */
208
209 struct HasNonTrivialBase: TrivialClass<1>, TrivialClass<2>,
210 DerivesFromNontrivialClass,
211 TrivialClass<3>, TrivialClass<4>
212 {
213 HasNonTrivialBase ()
214 {
215 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
216 }
217
218 HasNonTrivialBase (const HasNonTrivialBase &x)
219 {
220 memcpy (this, &x, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
221 }
222
223 ~HasNonTrivialBase ()
224 {
225 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
226 }
227
228 HasNonTrivialBase& operator= (const HasNonTrivialBase &x)
229 {
230 memcpy (this, &x, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
231 return *this;
232 }
233
234 void clear ()
235 {
236 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
237 }
238
239 void copy (void *p)
240 {
241 memcpy (this, p, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
242 }
243 };
244
245 struct DerivesIndidirectlyFromNontrivialClass:
246 TrivialClass<1>, TrivialClass<2>,
247 DerivesFromNontrivialClass,
248 TrivialClass<3>, TrivialClass<4> { };
249
250 /* Verify that raw memory accesses from members of a class with a non-trivial
251 indirect base class is diagnosed. */
252
253 struct HasIndirectNonTrivialBase: TrivialClass<5>, TrivialClass<6>,
254 TrivialClass<7>, TrivialClass<8>,
255 DerivesIndidirectlyFromNontrivialClass
256 {
257 HasIndirectNonTrivialBase ()
258 {
259 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
260 }
261
262 HasIndirectNonTrivialBase (const HasIndirectNonTrivialBase &x)
263 {
264 memcpy (this, &x, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
265 }
266
267 ~HasIndirectNonTrivialBase ()
268 {
269 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
270 }
271
272 HasIndirectNonTrivialBase& operator= (const HasIndirectNonTrivialBase &x)
273 {
274 memcpy (this, &x, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
275 return *this;
276 }
277
278 void clear ()
279 {
280 memset (this, 0, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
281 }
282
283 void copy (void *p)
284 {
285 memcpy (this, p, sizeof *this); // { dg-warning "\\\[-Wclass-memaccess" }
286 }
287 };