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