warn-reinterpret-base-class.cpp
15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -triple %itanium_abi_triple -verify -Wreinterpret-base-class -Wno-unused-volatile-lvalue %s
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -triple %ms_abi_triple -DMSABI -verify -Wreinterpret-base-class -Wno-unused-volatile-lvalue %s
// RUN: not %clang_cc1 -std=c++11 -fsyntax-only -triple %itanium_abi_triple -fdiagnostics-parseable-fixits -Wreinterpret-base-class -Wno-unused-volatile-lvalue %s 2>&1 | FileCheck %s
// RUN: not %clang_cc1 -std=c++11 -fsyntax-only -triple %ms_abi_triple -fdiagnostics-parseable-fixits -Wreinterpret-base-class -Wno-unused-volatile-lvalue %s 2>&1 | FileCheck %s
// PR 13824
class A {
};
class DA : public A {
};
class DDA : public DA {
};
class DAo : protected A {
};
class DAi : private A {
};
class DVA : public virtual A {
};
class DDVA : public virtual DA {
};
class DMA : public virtual A, public virtual DA { //expected-warning{{direct base 'A' is inaccessible due to ambiguity:\n class DMA -> class A\n class DMA -> class DA -> class A}}
};
class B;
struct C {
// Do not fail on incompletely-defined classes.
decltype(reinterpret_cast<C *>(0)) foo;
decltype(reinterpret_cast<A *>((C *) 0)) bar;
decltype(reinterpret_cast<C *>((A *) 0)) baz;
};
void reinterpret_not_defined_class(B *b, C *c) {
// Should not fail if class has no definition.
(void)*reinterpret_cast<C *>(b);
(void)*reinterpret_cast<B *>(c);
(void)reinterpret_cast<C &>(*b);
(void)reinterpret_cast<B &>(*c);
}
// Do not fail on erroneous classes with fields of incompletely-defined types.
// Base class is malformed.
namespace BaseMalformed {
struct A; // expected-note {{forward declaration of 'BaseMalformed::A'}}
struct B {
A a; // expected-error {{field has incomplete type 'BaseMalformed::A'}}
};
struct C : public B {} c;
B *b = reinterpret_cast<B *>(&c);
} // end anonymous namespace
// Child class is malformed.
namespace ChildMalformed {
struct A; // expected-note {{forward declaration of 'ChildMalformed::A'}}
struct B {};
struct C : public B {
A a; // expected-error {{field has incomplete type 'ChildMalformed::A'}}
} c;
B *b = reinterpret_cast<B *>(&c);
} // end anonymous namespace
// Base class outside upcast base-chain is malformed.
namespace BaseBaseMalformed {
struct A; // expected-note {{forward declaration of 'BaseBaseMalformed::A'}}
struct Y {};
struct X { A a; }; // expected-error {{field has incomplete type 'BaseBaseMalformed::A'}}
struct B : Y, X {};
struct C : B {} c;
B *p = reinterpret_cast<B*>(&c);
}
namespace InheritanceMalformed {
struct A; // expected-note {{forward declaration of 'InheritanceMalformed::A'}}
struct B : A {}; // expected-error {{base class has incomplete type}}
struct C : B {} c;
B *p = reinterpret_cast<B*>(&c);
}
// Virtual base class outside upcast base-chain is malformed.
namespace VBaseMalformed{
struct A; // expected-note {{forward declaration of 'VBaseMalformed::A'}}
struct X { A a; }; // expected-error {{field has incomplete type 'VBaseMalformed::A'}}
struct B : public virtual X {};
struct C : B {} c;
B *p = reinterpret_cast<B*>(&c);
}
void reinterpret_not_updowncast(A *pa, const A *pca, A &a, const A &ca) {
(void)*reinterpret_cast<C *>(pa);
(void)*reinterpret_cast<const C *>(pa);
(void)*reinterpret_cast<volatile C *>(pa);
(void)*reinterpret_cast<const volatile C *>(pa);
(void)*reinterpret_cast<const C *>(pca);
(void)*reinterpret_cast<const volatile C *>(pca);
(void)reinterpret_cast<C &>(a);
(void)reinterpret_cast<const C &>(a);
(void)reinterpret_cast<volatile C &>(a);
(void)reinterpret_cast<const volatile C &>(a);
(void)reinterpret_cast<const C &>(ca);
(void)reinterpret_cast<const volatile C &>(ca);
}
void reinterpret_pointer_downcast(A *a, const A *ca) {
(void)*reinterpret_cast<DA *>(a);
(void)*reinterpret_cast<const DA *>(a);
(void)*reinterpret_cast<volatile DA *>(a);
(void)*reinterpret_cast<const volatile DA *>(a);
(void)*reinterpret_cast<const DA *>(ca);
(void)*reinterpret_cast<const volatile DA *>(ca);
(void)*reinterpret_cast<DDA *>(a);
(void)*reinterpret_cast<DAo *>(a);
(void)*reinterpret_cast<DAi *>(a);
// expected-warning@+2 {{'reinterpret_cast' to class 'DVA *' from its virtual base 'A *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)*reinterpret_cast<DVA *>(a);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' to class 'DDVA *' from its virtual base 'A *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)*reinterpret_cast<DDVA *>(a);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' to class 'DMA *' from its virtual base 'A *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)*reinterpret_cast<DMA *>(a);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
}
void reinterpret_reference_downcast(A a, A &ra, const A &cra) {
(void)reinterpret_cast<DA &>(a);
(void)reinterpret_cast<const DA &>(a);
(void)reinterpret_cast<volatile DA &>(a);
(void)reinterpret_cast<const volatile DA &>(a);
(void)reinterpret_cast<DA &>(ra);
(void)reinterpret_cast<const DA &>(ra);
(void)reinterpret_cast<volatile DA &>(ra);
(void)reinterpret_cast<const volatile DA &>(ra);
(void)reinterpret_cast<const DA &>(cra);
(void)reinterpret_cast<const volatile DA &>(cra);
(void)reinterpret_cast<DDA &>(a);
(void)reinterpret_cast<DAo &>(a);
(void)reinterpret_cast<DAi &>(a);
// expected-warning@+2 {{'reinterpret_cast' to class 'DVA &' from its virtual base 'A' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)reinterpret_cast<DVA &>(a);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' to class 'DDVA &' from its virtual base 'A' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)reinterpret_cast<DDVA &>(a);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' to class 'DMA &' from its virtual base 'A' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)reinterpret_cast<DMA &>(a);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
}
void reinterpret_pointer_upcast(DA *da, const DA *cda, DDA *dda, DAo *dao,
DAi *dai, DVA *dva, DDVA *ddva, DMA *dma) {
(void)*reinterpret_cast<A *>(da);
(void)*reinterpret_cast<const A *>(da);
(void)*reinterpret_cast<volatile A *>(da);
(void)*reinterpret_cast<const volatile A *>(da);
(void)*reinterpret_cast<const A *>(cda);
(void)*reinterpret_cast<const volatile A *>(cda);
(void)*reinterpret_cast<A *>(dda);
(void)*reinterpret_cast<DA *>(dda);
(void)*reinterpret_cast<A *>(dao);
(void)*reinterpret_cast<A *>(dai);
// expected-warning@+2 {{'reinterpret_cast' from class 'DVA *' to its virtual base 'A *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)*reinterpret_cast<A *>(dva);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' from class 'DDVA *' to its virtual base 'A *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)*reinterpret_cast<A *>(ddva);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' from class 'DDVA *' to its virtual base 'DA *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)*reinterpret_cast<DA *>(ddva);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' from class 'DMA *' to its virtual base 'A *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)*reinterpret_cast<A *>(dma);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' from class 'DMA *' to its virtual base 'DA *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)*reinterpret_cast<DA *>(dma);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
}
void reinterpret_reference_upcast(DA &da, const DA &cda, DDA &dda, DAo &dao,
DAi &dai, DVA &dva, DDVA &ddva, DMA &dma) {
(void)reinterpret_cast<A &>(da);
(void)reinterpret_cast<const A &>(da);
(void)reinterpret_cast<volatile A &>(da);
(void)reinterpret_cast<const volatile A &>(da);
(void)reinterpret_cast<const A &>(cda);
(void)reinterpret_cast<const volatile A &>(cda);
(void)reinterpret_cast<A &>(dda);
(void)reinterpret_cast<DA &>(dda);
(void)reinterpret_cast<A &>(dao);
(void)reinterpret_cast<A &>(dai);
// expected-warning@+2 {{'reinterpret_cast' from class 'DVA' to its virtual base 'A &' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)reinterpret_cast<A &>(dva);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' from class 'DDVA' to its virtual base 'A &' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)reinterpret_cast<A &>(ddva);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' from class 'DDVA' to its virtual base 'DA &' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)reinterpret_cast<DA &>(ddva);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' from class 'DMA' to its virtual base 'A &' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)reinterpret_cast<A &>(dma);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' from class 'DMA' to its virtual base 'DA &' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)reinterpret_cast<DA &>(dma);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
}
struct E {
int x;
};
class F : public E {
virtual int foo() { return x; }
};
class G : public F {
};
class H : public E, public A {
};
class I : virtual public F {
};
typedef const F * K;
typedef volatile K L;
void different_subobject_downcast(E *e, F *f, A *a) {
// expected-warning@+2 {{'reinterpret_cast' to class 'F *' from its base at non-zero offset 'E *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)reinterpret_cast<F *>(e);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' to class 'G *' from its base at non-zero offset 'E *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)reinterpret_cast<G *>(e);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
(void)reinterpret_cast<H *>(e);
// expected-warning@+2 {{'reinterpret_cast' to class 'I *' from its virtual base 'E *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)reinterpret_cast<I *>(e);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
(void)reinterpret_cast<G *>(f);
// expected-warning@+2 {{'reinterpret_cast' to class 'I *' from its virtual base 'F *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)reinterpret_cast<I *>(f);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
#ifdef MSABI
// In MS ABI mode, A is at non-zero offset in H.
// expected-warning@+3 {{'reinterpret_cast' to class 'H *' from its base at non-zero offset 'A *' behaves differently from 'static_cast'}}
// expected-note@+2 {{use 'static_cast'}}
#endif
(void)reinterpret_cast<H *>(a);
// expected-warning@+2 {{'reinterpret_cast' to class 'L' (aka 'const F *volatile') from its base at non-zero offset 'E *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
(void)reinterpret_cast<L>(e);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
}
void different_subobject_upcast(F *f, G *g, H *h, I *i) {
// expected-warning@+2 {{'reinterpret_cast' from class 'F *' to its base at non-zero offset 'E *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)reinterpret_cast<E *>(f);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
(void)reinterpret_cast<F *>(g);
// expected-warning@+2 {{'reinterpret_cast' from class 'G *' to its base at non-zero offset 'E *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)reinterpret_cast<E *>(g);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
(void)reinterpret_cast<E *>(h);
#ifdef MSABI
// In MS ABI mode, A is at non-zero offset in H.
// expected-warning@+3 {{'reinterpret_cast' from class 'H *' to its base at non-zero offset 'A *' behaves differently from 'static_cast'}}
// expected-note@+2 {{use 'static_cast'}}
#endif
(void)reinterpret_cast<A *>(h);
// expected-warning@+2 {{'reinterpret_cast' from class 'I *' to its virtual base 'F *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)reinterpret_cast<F *>(i);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
// expected-warning@+2 {{'reinterpret_cast' from class 'I *' to its virtual base 'E *' behaves differently from 'static_cast'}}
// expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
(void)reinterpret_cast<E *>(i);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
}