Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / vclwidgets.cxx
blob1ff828e280ad45dec8ff1de751f5f4f8de25ff08
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 #include <sal/config.h>
12 #include <atomic>
14 #include <vcl/vclptr.hxx>
15 #include <vcl/vclreferencebase.hxx>
17 struct Widget : public VclReferenceBase
19 VclPtr<Widget> mpParent;
21 void widget1()
23 // test that we ignore assignments from a member field
24 Widget* p = mpParent;
25 (void)p;
26 // test against false+
27 p = (true) ? mpParent.get() : nullptr;
30 ~Widget() override
32 disposeOnce();
35 void dispose() override
37 mpParent.clear();
38 VclReferenceBase::dispose();
42 VclPtr<Widget> f()
44 return nullptr;
47 Widget* g()
49 return nullptr;
52 // test the variable init detection
53 void bar()
55 Widget* p = f(); // expected-error {{assigning a returned-by-value VclPtr<T> to a T* variable is dodgy, should be assigned to a VclPtr. If you know that the RHS does not return a newly created T, then add a '.get()' to the RHS [loplugin:vclwidgets]}}
56 (void)p;
57 Widget* q = g();
58 (void)q;
59 Widget* r = nullptr;
60 (void)r;
63 // test the assignment detection
64 void bar2()
66 Widget* p;
67 p = nullptr;
68 p = f(); // expected-error {{assigning a returned-by-value VclPtr<T> to a T* variable is dodgy, should be assigned to a VclPtr. If you know that the RHS does not return a newly created T, then add a '.get()' to the RHS [loplugin:vclwidgets]}}
69 (void)p;
70 Widget* q;
71 q = g();
72 (void)q;
76 // test against false+
78 template<class T>
79 T * get() { return nullptr; }
81 void bar3()
83 Widget* p;
84 p = get<Widget>();
85 (void)p;
88 void bar4() {
89 VclPtr<Widget> p1;
90 //TODO: one error should be enough here?
91 // expected-error@+2 {{calling delete on instance of VclReferenceBase subclass, must rather call disposeAndClear() [loplugin:vclwidgets]}}
92 // expected-error@+1 {{calling delete on instance of VclPtr, must rather call disposeAndClear() [loplugin:vclwidgets]}}
93 delete p1;
94 std::atomic<int *> p2;
95 // No false positive here:
96 delete p2;
99 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */