1651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// RUN: %clang_cc1 -std=c++11 -fsyntax-only -triple %itanium_abi_triple -verify -Wreinterpret-base-class -Wno-unused-volatile-lvalue %s
2651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// RUN: %clang_cc1 -std=c++11 -fsyntax-only -triple %ms_abi_triple -DMSABI -verify -Wreinterpret-base-class -Wno-unused-volatile-lvalue %s
3651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
4651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// 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
5651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// 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
6437da054125a1e5cdd8c9765e09e42da922482f3John McCall
7437da054125a1e5cdd8c9765e09e42da922482f3John McCall// PR 13824
8437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass A {
9437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
10437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass DA : public A {
11437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
12437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass DDA : public DA {
13437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
14437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass DAo : protected A {
15437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
16437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass DAi : private A {
17437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
18437da054125a1e5cdd8c9765e09e42da922482f3John McCall
19437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass DVA : public virtual A {
20437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
21437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass DDVA : public virtual DA {
22437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
23437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass DMA : public virtual A, public virtual DA {
24437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
25437da054125a1e5cdd8c9765e09e42da922482f3John McCall
26437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass B;
27437da054125a1e5cdd8c9765e09e42da922482f3John McCall
28437da054125a1e5cdd8c9765e09e42da922482f3John McCallstruct C {
29437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // Do not fail on incompletely-defined classes.
30437da054125a1e5cdd8c9765e09e42da922482f3John McCall  decltype(reinterpret_cast<C *>(0)) foo;
31437da054125a1e5cdd8c9765e09e42da922482f3John McCall  decltype(reinterpret_cast<A *>((C *) 0)) bar;
32437da054125a1e5cdd8c9765e09e42da922482f3John McCall  decltype(reinterpret_cast<C *>((A *) 0)) baz;
33437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
34437da054125a1e5cdd8c9765e09e42da922482f3John McCall
35437da054125a1e5cdd8c9765e09e42da922482f3John McCallvoid reinterpret_not_defined_class(B *b, C *c) {
36437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // Should not fail if class has no definition.
37437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<C *>(b);
38437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<B *>(c);
39437da054125a1e5cdd8c9765e09e42da922482f3John McCall
40437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<C &>(*b);
41437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<B &>(*c);
42437da054125a1e5cdd8c9765e09e42da922482f3John McCall}
43437da054125a1e5cdd8c9765e09e42da922482f3John McCall
44fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall// Do not fail on erroneous classes with fields of incompletely-defined types.
45fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall// Base class is malformed.
46fdb468fc8ab84af5f32868cc6511cf1160074d53John McCallnamespace BaseMalformed {
47fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct A; // expected-note {{forward declaration of 'BaseMalformed::A'}}
48fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct B {
49fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall    A a; // expected-error {{field has incomplete type 'BaseMalformed::A'}}
50fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  };
51fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct C : public B {} c;
52fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  B *b = reinterpret_cast<B *>(&c);
53fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall} // end anonymous namespace
54fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall
55fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall// Child class is malformed.
56fdb468fc8ab84af5f32868cc6511cf1160074d53John McCallnamespace ChildMalformed {
57fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct A; // expected-note {{forward declaration of 'ChildMalformed::A'}}
58fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct B {};
59fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct C : public B {
60fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall    A a; // expected-error {{field has incomplete type 'ChildMalformed::A'}}
61fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  } c;
62fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  B *b = reinterpret_cast<B *>(&c);
63fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall} // end anonymous namespace
64fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall
65fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall// Base class outside upcast base-chain is malformed.
66fdb468fc8ab84af5f32868cc6511cf1160074d53John McCallnamespace BaseBaseMalformed {
67fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct A; // expected-note {{forward declaration of 'BaseBaseMalformed::A'}}
68fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct Y {};
69fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct X { A a; }; // expected-error {{field has incomplete type 'BaseBaseMalformed::A'}}
70fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct B : Y, X {};
71fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct C : B {} c;
72fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  B *p = reinterpret_cast<B*>(&c);
73fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall}
74fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall
75fdb468fc8ab84af5f32868cc6511cf1160074d53John McCallnamespace InheritanceMalformed {
76fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct A; // expected-note {{forward declaration of 'InheritanceMalformed::A'}}
77fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct B : A {}; // expected-error {{base class has incomplete type}}
78fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct C : B {} c;
79fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  B *p = reinterpret_cast<B*>(&c);
80fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall}
81fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall
82fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall// Virtual base class outside upcast base-chain is malformed.
83fdb468fc8ab84af5f32868cc6511cf1160074d53John McCallnamespace VBaseMalformed{
84fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct A; // expected-note {{forward declaration of 'VBaseMalformed::A'}}
85fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct X { A a; };  // expected-error {{field has incomplete type 'VBaseMalformed::A'}}
86fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct B : public virtual X {};
87fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  struct C : B {} c;
88fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall  B *p = reinterpret_cast<B*>(&c);
89fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall}
90fdb468fc8ab84af5f32868cc6511cf1160074d53John McCall
91437da054125a1e5cdd8c9765e09e42da922482f3John McCallvoid reinterpret_not_updowncast(A *pa, const A *pca, A &a, const A &ca) {
92437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<C *>(pa);
93437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const C *>(pa);
94437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<volatile C *>(pa);
95437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const volatile C *>(pa);
96437da054125a1e5cdd8c9765e09e42da922482f3John McCall
97437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const C *>(pca);
98437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const volatile C *>(pca);
99437da054125a1e5cdd8c9765e09e42da922482f3John McCall
100437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<C &>(a);
101437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const C &>(a);
102437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<volatile C &>(a);
103437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const volatile C &>(a);
104437da054125a1e5cdd8c9765e09e42da922482f3John McCall
105437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const C &>(ca);
106437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const volatile C &>(ca);
107437da054125a1e5cdd8c9765e09e42da922482f3John McCall}
108437da054125a1e5cdd8c9765e09e42da922482f3John McCall
109437da054125a1e5cdd8c9765e09e42da922482f3John McCallvoid reinterpret_pointer_downcast(A *a, const A *ca) {
110437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<DA *>(a);
111437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const DA *>(a);
112437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<volatile DA *>(a);
113437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const volatile DA *>(a);
114437da054125a1e5cdd8c9765e09e42da922482f3John McCall
115437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const DA *>(ca);
116437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const volatile DA *>(ca);
117437da054125a1e5cdd8c9765e09e42da922482f3John McCall
118437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<DDA *>(a);
119437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<DAo *>(a);
120437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<DAi *>(a);
121437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' to class 'DVA *' from its virtual base 'A *' behaves differently from 'static_cast'}}
122437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
123437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<DVA *>(a);
1245fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
1255fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
126437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' to class 'DDVA *' from its virtual base 'A *' behaves differently from 'static_cast'}}
127437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
128437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<DDVA *>(a);
1295fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
1305fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
131437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' to class 'DMA *' from its virtual base 'A *' behaves differently from 'static_cast'}}
132437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
133437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<DMA *>(a);
1345fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
135437da054125a1e5cdd8c9765e09e42da922482f3John McCall}
136437da054125a1e5cdd8c9765e09e42da922482f3John McCall
137437da054125a1e5cdd8c9765e09e42da922482f3John McCallvoid reinterpret_reference_downcast(A a, A &ra, const A &cra) {
138437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DA &>(a);
139437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const DA &>(a);
140437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<volatile DA &>(a);
141437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const volatile DA &>(a);
142437da054125a1e5cdd8c9765e09e42da922482f3John McCall
143437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DA &>(ra);
144437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const DA &>(ra);
145437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<volatile DA &>(ra);
146437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const volatile DA &>(ra);
147437da054125a1e5cdd8c9765e09e42da922482f3John McCall
148437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const DA &>(cra);
149437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const volatile DA &>(cra);
150437da054125a1e5cdd8c9765e09e42da922482f3John McCall
151437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DDA &>(a);
152437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DAo &>(a);
153437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DAi &>(a);
154437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' to class 'DVA &' from its virtual base 'A' behaves differently from 'static_cast'}}
155437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
156437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DVA &>(a);
1575fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
1585fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
159437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' to class 'DDVA &' from its virtual base 'A' behaves differently from 'static_cast'}}
160437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
161437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DDVA &>(a);
1625fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
1635fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
164437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' to class 'DMA &' from its virtual base 'A' behaves differently from 'static_cast'}}
165437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
166437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DMA &>(a);
1675fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
168437da054125a1e5cdd8c9765e09e42da922482f3John McCall}
169437da054125a1e5cdd8c9765e09e42da922482f3John McCall
170437da054125a1e5cdd8c9765e09e42da922482f3John McCallvoid reinterpret_pointer_upcast(DA *da, const DA *cda, DDA *dda, DAo *dao,
171437da054125a1e5cdd8c9765e09e42da922482f3John McCall                                DAi *dai, DVA *dva, DDVA *ddva, DMA *dma) {
172437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<A *>(da);
173437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const A *>(da);
174437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<volatile A *>(da);
175437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const volatile A *>(da);
176437da054125a1e5cdd8c9765e09e42da922482f3John McCall
177437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const A *>(cda);
178437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<const volatile A *>(cda);
179437da054125a1e5cdd8c9765e09e42da922482f3John McCall
180437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<A *>(dda);
181437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<DA *>(dda);
182437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<A *>(dao);
183437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<A *>(dai);
184437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'DVA *' to its virtual base 'A *' behaves differently from 'static_cast'}}
185437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
186437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<A *>(dva);
1875fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
1885fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
189437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'DDVA *' to its virtual base 'A *' behaves differently from 'static_cast'}}
190437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
191437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<A *>(ddva);
1925fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
1935fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
194437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'DDVA *' to its virtual base 'DA *' behaves differently from 'static_cast'}}
195437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
196437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<DA *>(ddva);
1975fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
1985fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
199437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'DMA *' to its virtual base 'A *' behaves differently from 'static_cast'}}
200437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
201437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<A *>(dma);
2025fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
2035fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
204437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'DMA *' to its virtual base 'DA *' behaves differently from 'static_cast'}}
205437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
206437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)*reinterpret_cast<DA *>(dma);
2075fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast"
208437da054125a1e5cdd8c9765e09e42da922482f3John McCall}
209437da054125a1e5cdd8c9765e09e42da922482f3John McCall
210437da054125a1e5cdd8c9765e09e42da922482f3John McCallvoid reinterpret_reference_upcast(DA &da, const DA &cda, DDA &dda, DAo &dao,
211437da054125a1e5cdd8c9765e09e42da922482f3John McCall                                  DAi &dai, DVA &dva, DDVA &ddva, DMA &dma) {
212437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<A &>(da);
213437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const A &>(da);
214437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<volatile A &>(da);
215437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const volatile A &>(da);
216437da054125a1e5cdd8c9765e09e42da922482f3John McCall
217437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const A &>(cda);
218437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<const volatile A &>(cda);
219437da054125a1e5cdd8c9765e09e42da922482f3John McCall
220437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<A &>(dda);
221437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DA &>(dda);
222437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<A &>(dao);
223437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<A &>(dai);
224437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'DVA' to its virtual base 'A &' behaves differently from 'static_cast'}}
225437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
226437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<A &>(dva);
2275fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
2285fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
229437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'DDVA' to its virtual base 'A &' behaves differently from 'static_cast'}}
230437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
231437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<A &>(ddva);
2325fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
2335fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
234437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'DDVA' to its virtual base 'DA &' behaves differently from 'static_cast'}}
235437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
236437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DA &>(ddva);
2375fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
2385fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
239437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'DMA' to its virtual base 'A &' behaves differently from 'static_cast'}}
240437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
241437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<A &>(dma);
2425fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
2435fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
244437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'DMA' to its virtual base 'DA &' behaves differently from 'static_cast'}}
245437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
246437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<DA &>(dma);
2475fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
248437da054125a1e5cdd8c9765e09e42da922482f3John McCall}
249437da054125a1e5cdd8c9765e09e42da922482f3John McCall
250437da054125a1e5cdd8c9765e09e42da922482f3John McCallstruct E {
251437da054125a1e5cdd8c9765e09e42da922482f3John McCall  int x;
252437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
253437da054125a1e5cdd8c9765e09e42da922482f3John McCall
254437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass F : public E {
255437da054125a1e5cdd8c9765e09e42da922482f3John McCall  virtual int foo() { return x; }
256437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
257437da054125a1e5cdd8c9765e09e42da922482f3John McCall
258437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass G : public F {
259437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
260437da054125a1e5cdd8c9765e09e42da922482f3John McCall
261437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass H : public E, public A {
262437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
263437da054125a1e5cdd8c9765e09e42da922482f3John McCall
264437da054125a1e5cdd8c9765e09e42da922482f3John McCallclass I : virtual public F {
265437da054125a1e5cdd8c9765e09e42da922482f3John McCall};
266437da054125a1e5cdd8c9765e09e42da922482f3John McCall
267437da054125a1e5cdd8c9765e09e42da922482f3John McCalltypedef const F * K;
268437da054125a1e5cdd8c9765e09e42da922482f3John McCalltypedef volatile K L;
269437da054125a1e5cdd8c9765e09e42da922482f3John McCall
270437da054125a1e5cdd8c9765e09e42da922482f3John McCallvoid different_subobject_downcast(E *e, F *f, A *a) {
271437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' to class 'F *' from its base at non-zero offset 'E *' behaves differently from 'static_cast'}}
272437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
273437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<F *>(e);
2745fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
2755fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
276437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' to class 'G *' from its base at non-zero offset 'E *' behaves differently from 'static_cast'}}
277437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
278437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<G *>(e);
2795fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
2805fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
281437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<H *>(e);
282437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' to class 'I *' from its virtual base 'E *' behaves differently from 'static_cast'}}
283437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
284437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<I *>(e);
2855fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
2865fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
287437da054125a1e5cdd8c9765e09e42da922482f3John McCall
288437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<G *>(f);
289437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' to class 'I *' from its virtual base 'F *' behaves differently from 'static_cast'}}
290437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
291437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<I *>(f);
2925fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
293437da054125a1e5cdd8c9765e09e42da922482f3John McCall
294651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#ifdef MSABI
295651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // In MS ABI mode, A is at non-zero offset in H.
296651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // expected-warning@+3 {{'reinterpret_cast' to class 'H *' from its base at non-zero offset 'A *' behaves differently from 'static_cast'}}
297651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // expected-note@+2 {{use 'static_cast'}}
298651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#endif
299437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<H *>(a);
300437da054125a1e5cdd8c9765e09e42da922482f3John McCall
301437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // 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'}}
302437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}}
303437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<L>(e);
3045fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
305437da054125a1e5cdd8c9765e09e42da922482f3John McCall}
306437da054125a1e5cdd8c9765e09e42da922482f3John McCall
307437da054125a1e5cdd8c9765e09e42da922482f3John McCallvoid different_subobject_upcast(F *f, G *g, H *h, I *i) {
308437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'F *' to its base at non-zero offset 'E *' behaves differently from 'static_cast'}}
309437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
310437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<E *>(f);
3115fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
312437da054125a1e5cdd8c9765e09e42da922482f3John McCall
313437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<F *>(g);
314437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'G *' to its base at non-zero offset 'E *' behaves differently from 'static_cast'}}
315437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
316437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<E *>(g);
3175fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
318437da054125a1e5cdd8c9765e09e42da922482f3John McCall
319437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<E *>(h);
320651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
321651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#ifdef MSABI
322651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // In MS ABI mode, A is at non-zero offset in H.
323651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // expected-warning@+3 {{'reinterpret_cast' from class 'H *' to its base at non-zero offset 'A *' behaves differently from 'static_cast'}}
324651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // expected-note@+2 {{use 'static_cast'}}
325651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#endif
326437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<A *>(h);
327437da054125a1e5cdd8c9765e09e42da922482f3John McCall
328437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'I *' to its virtual base 'F *' behaves differently from 'static_cast'}}
329437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
330437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<F *>(i);
3315fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
3325fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose
333437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-warning@+2 {{'reinterpret_cast' from class 'I *' to its virtual base 'E *' behaves differently from 'static_cast'}}
334437da054125a1e5cdd8c9765e09e42da922482f3John McCall  // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}}
335437da054125a1e5cdd8c9765e09e42da922482f3John McCall  (void)reinterpret_cast<E *>(i);
3365fd1fac0104e22533bc0284fdd0f42f843c975f5Jordan Rose  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast"
337437da054125a1e5cdd8c9765e09e42da922482f3John McCall}
338