Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / crosscast.cxx
blob1135573371137c1c0a9c36c174c3a16abbc47599
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 struct Base
12 virtual ~Base();
15 struct Derived : Base
19 struct Virtual1 : virtual Base
23 struct Virtual2 : virtual Base
27 struct Virtual2a : Virtual2
31 struct Other
33 virtual ~Other();
36 struct[[clang::annotate("loplugin:crosscast")]] Ok { virtual ~Ok(); };
38 Base* up(Derived* p) { return dynamic_cast<Base*>(p); }
40 Base& up(Derived& p) { return dynamic_cast<Base&>(p); }
42 Derived* down(Base* p) { return dynamic_cast<Derived*>(p); }
44 Derived& down(Base& p) { return dynamic_cast<Derived&>(p); }
46 Base* self(Base* p) { return dynamic_cast<Base*>(p); }
48 Base& self(Base& p) { return dynamic_cast<Base&>(p); }
50 Virtual1* crossVirtual(Virtual2a* p) { return dynamic_cast<Virtual1*>(p); }
52 Virtual1& crossVirtual(Virtual2a& p) { return dynamic_cast<Virtual1&>(p); }
54 Base* crossFromOther(Other* p)
56 // expected-error@+1 {{suspicious dynamic cross cast from 'Other *' to 'Base *' [loplugin:crosscast]}}
57 return dynamic_cast<Base*>(p);
60 Base& crossFromOther(Other& p)
62 // expected-error@+1 {{suspicious dynamic cross cast from 'Other' to 'Base &' [loplugin:crosscast]}}
63 return dynamic_cast<Base&>(p);
66 Other* crossToOther(Base* p)
68 // expected-error@+1 {{suspicious dynamic cross cast from 'Base *' to 'Other *' [loplugin:crosscast]}}
69 return dynamic_cast<Other*>(p);
72 Other& crossToOther(Base& p)
74 // expected-error@+1 {{suspicious dynamic cross cast from 'Base' to 'Other &' [loplugin:crosscast]}}
75 return dynamic_cast<Other&>(p);
78 Base* crossFromOk(Ok* p) { return dynamic_cast<Base*>(p); }
80 Base& crossFromOk(Ok& p) { return dynamic_cast<Base&>(p); }
82 Ok* crossToOk(Base* p) { return dynamic_cast<Ok*>(p); }
84 Ok& crossToOk(Base& p) { return dynamic_cast<Ok&>(p); }
86 void* mostDerived(Base* p) { return dynamic_cast<void*>(p); }
88 template <typename T> T generic1(Base* p) { return dynamic_cast<T>(p); }
90 template <typename T> T* generic2(Base* p) { return dynamic_cast<T*>(p); }
92 template <typename T> Derived* generic3(T p) { return dynamic_cast<Derived*>(p); }
94 template <typename T> Derived* generic4(T* p) { return dynamic_cast<Derived*>(p); }
96 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */