Fix typo
[LibreOffice.git] / compilerplugins / clang / test / refcounting.cxx
blob54d4dbe14b38f2230e48b643a7ca802bc9b96ea8
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 <memory>
13 #include <rtl/ref.hxx>
14 #include <boost/intrusive_ptr.hpp>
15 #include <com/sun/star/uno/XInterface.hpp>
16 #include <cppuhelper/weak.hxx>
17 #include <unotools/weakref.hxx>
19 struct UnoObject : public cppu::OWeakObject
22 struct UnoSubObject : public UnoObject
27 // Note, getting duplicate warnings for some reason I cannot fathom
30 struct Foo
32 // expected-error@+2 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
33 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
34 std::unique_ptr<UnoObject> m_foo1;
35 // expected-error@+2 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
36 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
37 std::shared_ptr<UnoObject> m_foo2;
38 // expected-error@+2 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
39 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
40 boost::intrusive_ptr<UnoObject> m_foo3;
41 rtl::Reference<UnoObject> m_foo4; // no warning expected
44 // expected-error@+2 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
45 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
46 std::unique_ptr<UnoObject> foo1();
47 rtl::Reference<UnoObject> foo2(); // no warning expected
48 // expected-error@+2 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
49 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
50 void foo3(std::unique_ptr<UnoObject> p);
52 void test2(UnoObject* pUnoObject)
54 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being deleted via delete, should be managed via rtl::Reference [loplugin:refcounting]}}
55 delete pUnoObject;
58 template <typename T> struct Dependent : T
60 void f() { delete this; }
61 //TODO: missing expected error@+1 {{cppu::OWeakObject subclass 'Dependent<UnoObject>' being deleted via delete, should be managed via rtl::Reference [loplugin:refcounting]}}
62 void g() { delete this; }
64 struct Dummy
67 void dummy(Dependent<Dummy>* p1, Dependent<UnoObject>* p2)
69 p1->f();
70 p2->g();
73 void foo4()
75 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
76 UnoObject* p = new UnoObject;
77 (void)p;
78 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
79 p = new UnoObject;
82 UnoObject* foo5()
84 // expected-error@+1 {{new object of cppu::OWeakObject subclass 'UnoObject' being returned via raw pointer, should be returned by via rtl::Reference [loplugin:refcounting]}}
85 return new UnoObject;
87 rtl::Reference<UnoObject> foo6()
89 // no warning expected
90 return new UnoObject;
92 const rtl::Reference<UnoObject>& getConstRef();
93 void foo7()
95 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoSubObject' being managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
96 UnoSubObject* p1 = static_cast<UnoSubObject*>(foo6().get());
97 (void)p1;
98 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoSubObject' being managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
99 p1 = static_cast<UnoSubObject*>(foo6().get());
101 rtl::Reference<UnoObject> u2;
102 // no warning expected
103 UnoSubObject* p2 = static_cast<UnoSubObject*>(u2.get());
104 (void)p2;
105 p2 = static_cast<UnoSubObject*>(u2.get());
106 // no warning expected
107 UnoSubObject* p3 = static_cast<UnoSubObject*>(getConstRef().get());
108 (void)p3;
109 p3 = static_cast<UnoSubObject*>(getConstRef().get());
112 const unotools::WeakReference<UnoObject>& getWeakRef();
113 void foo8()
115 // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
116 UnoSubObject* p1 = static_cast<UnoSubObject*>(getWeakRef().get().get());
117 (void)p1;
119 // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
120 UnoObject* p2 = getWeakRef().get().get();
121 (void)p2;
123 unotools::WeakReference<UnoObject> weak1;
124 // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
125 UnoSubObject* p3 = dynamic_cast<UnoSubObject*>(weak1.get().get());
126 (void)p3;
128 // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
129 UnoObject* p4 = weak1.get().get();
130 (void)p4;
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */