android: Reuse launcher icon in activities
[LibreOffice.git] / compilerplugins / clang / test / unusedfields.cxx
blobb545f4b1a89fe8ac0eec94673a04b9a66c3caf9d
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 #if defined _WIN32 // TODO, see corresponding TODO in compilerplugins/clang/unusedfields.cxx
11 // expected-no-diagnostics
12 #else
14 #include <memory>
15 #include <vector>
16 #include <ostream>
17 #include <com/sun/star/uno/Any.hxx>
18 #include <com/sun/star/uno/Sequence.hxx>
19 #include <com/sun/star/uno/XInterface.hpp>
20 #include <rtl/ref.hxx>
22 struct Foo
23 // expected-error@-1 {{read m_foo1 [loplugin:unusedfields]}}
24 // expected-error@-2 {{outside m_foo1 [loplugin:unusedfields]}}
26 int m_foo1;
29 struct Bar
30 // expected-error@-1 {{read m_bar2 [loplugin:unusedfields]}}
31 // expected-error@-2 {{read m_bar4 [loplugin:unusedfields]}}
32 // expected-error@-3 {{read m_bar5 [loplugin:unusedfields]}}
33 // expected-error@-4 {{read m_bar6 [loplugin:unusedfields]}}
34 // expected-error@-5 {{read m_barfunctionpointer [loplugin:unusedfields]}}
35 // expected-error@-6 {{read m_bar8 [loplugin:unusedfields]}}
36 // expected-error@-7 {{read m_bar10 [loplugin:unusedfields]}}
37 // expected-error@-8 {{read m_bar11 [loplugin:unusedfields]}}
38 // expected-error@-9 {{write m_bar1 [loplugin:unusedfields]}}
39 // expected-error@-10 {{write m_bar2 [loplugin:unusedfields]}}
40 // expected-error@-11 {{write m_bar3 [loplugin:unusedfields]}}
41 // expected-error@-12 {{write m_bar3b [loplugin:unusedfields]}}
42 // expected-error@-13 {{write m_bar4 [loplugin:unusedfields]}}
43 // expected-error@-14 {{write m_bar7 [loplugin:unusedfields]}}
44 // expected-error@-15 {{write m_bar9 [loplugin:unusedfields]}}
45 // expected-error@-16 {{write m_bar12 [loplugin:unusedfields]}}
46 // expected-error@-17 {{outside-constructor m_bar2 [loplugin:unusedfields]}}
47 // expected-error@-18 {{outside-constructor m_bar3 [loplugin:unusedfields]}}
48 // expected-error@-19 {{outside-constructor m_bar3b [loplugin:unusedfields]}}
49 // expected-error@-20 {{outside-constructor m_bar4 [loplugin:unusedfields]}}
50 // expected-error@-21 {{outside-constructor m_bar5 [loplugin:unusedfields]}}
51 // expected-error@-22 {{outside-constructor m_bar6 [loplugin:unusedfields]}}
52 // expected-error@-23 {{outside-constructor m_bar7 [loplugin:unusedfields]}}
53 // expected-error@-24 {{outside-constructor m_bar8 [loplugin:unusedfields]}}
54 // expected-error@-25 {{outside-constructor m_bar9 [loplugin:unusedfields]}}
55 // expected-error@-26 {{outside-constructor m_bar10 [loplugin:unusedfields]}}
56 // expected-error@-27 {{outside-constructor m_bar11 [loplugin:unusedfields]}}
57 // expected-error@-28 {{outside-constructor m_bar12 [loplugin:unusedfields]}}
58 // expected-error@-29 {{outside-constructor m_barfunctionpointer [loplugin:unusedfields]}}
59 // expected-error@-30 {{outside m_barstream [loplugin:unusedfields]}}
61 int m_bar1;
62 int m_bar2 = 1;
63 int* m_bar3;
64 int* m_bar3b;
65 int m_bar4;
66 void (*m_barfunctionpointer)(int&);
67 int m_bar5;
68 std::vector<int> m_bar6;
69 int m_bar7[5];
70 int m_bar8;
71 int m_barstream;
72 sal_Int32 m_bar9;
73 sal_Int32 m_bar10;
74 css::uno::Any m_bar11;
75 css::uno::Any m_bar12;
77 // check that we see reads of fields like m_foo1 when referred to via constructor initializer
78 Bar(Foo const & foo) : m_bar1(foo.m_foo1) {}
80 // check that we don't see reads when inside copy/move constructor
81 Bar(Bar const & other) { m_bar3 = other.m_bar3; }
83 // check that we don't see reads when inside copy/move assignment operator
84 Bar& operator=(Bar const & other) { m_bar3 = other.m_bar3; return *this; }
86 // check that we DON'T see reads here
87 int bar2() { return m_bar2; }
89 // check that we DON'T see reads here
90 void bar3()
92 m_bar3 = nullptr;
93 m_bar3b = m_bar3 = nullptr;
96 // check that we see reads of field when passed to a function pointer
97 // check that we see read of a field that is a function pointer
98 void bar4() { m_barfunctionpointer(m_bar4); }
100 // check that we see reads of a field when used in variable init
101 void bar5() { int x = m_bar5; (void) x; }
103 // check that we see reads of a field when used in ranged-for
104 void bar6() { for (auto i : m_bar6) { (void)i; } }
106 // check that we see don't see reads of array fields
107 void bar7() { m_bar7[3] = 1; }
109 // check that we see reads when a field is used in an array expression
110 char bar8()
112 char tmp[5];
113 return tmp[m_bar8];
116 // check that we don't see reads when calling operator>>=
117 void bar9()
119 css::uno::Any any;
120 any >>= m_bar9;
123 // check that we see don't see writes when calling operator<<=
124 void bar10()
126 css::uno::Any any;
127 any <<= m_bar10;
128 (void)any;
131 // check that we see reads of the LHS when calling operator>>=
132 void bar11()
134 int x;
135 m_bar11 >>= x;
138 // check that we see writes of the LHS when calling operator<<=
139 void bar12()
141 int x = 0;
142 m_bar12 <<= x;
146 // check that we __dont__ see a read of m_barstream
147 std::ostream& operator<<(std::ostream& s, Bar const & bar)
149 s << bar.m_barstream;
150 return s;
153 struct ReadOnly1 { ReadOnly1(int&); };
155 struct ReadOnlyAnalysis
156 // expected-error@-1 {{read m_f2 [loplugin:unusedfields]}}
157 // expected-error@-2 {{read m_f3 [loplugin:unusedfields]}}
158 // expected-error@-3 {{read m_f5 [loplugin:unusedfields]}}
159 // expected-error@-4 {{read m_f6 [loplugin:unusedfields]}}
160 // expected-error@-5 {{write m_f2 [loplugin:unusedfields]}}
161 // expected-error@-6 {{write m_f3 [loplugin:unusedfields]}}
162 // expected-error@-7 {{write m_f4 [loplugin:unusedfields]}}
163 // expected-error@-8 {{write m_f5 [loplugin:unusedfields]}}
164 // expected-error@-9 {{write m_f6 [loplugin:unusedfields]}}
165 // expected-error@-10 {{outside-constructor m_f2 [loplugin:unusedfields]}}
166 // expected-error@-11 {{outside-constructor m_f3 [loplugin:unusedfields]}}
167 // expected-error@-12 {{outside-constructor m_f4 [loplugin:unusedfields]}}
168 // expected-error@-13 {{outside-constructor m_f5 [loplugin:unusedfields]}}
169 // expected-error@-14 {{outside-constructor m_f6 [loplugin:unusedfields]}}
171 int m_f1;
172 int m_f2;
173 int m_f3;
174 std::vector<int> m_f4;
175 int m_f5;
176 int m_f6;
178 // check that we don't see a write of m_f1
179 ReadOnlyAnalysis() : m_f1(0) {}
181 void method1(int&);
183 // check that we see a write when we pass by non-const ref
184 void method2() { method1(m_f2); }
186 int& method3() { return m_f3; }
188 void method4() { m_f4.push_back(1); }
190 // check that we see a write when we pass by non-const ref
191 void method5() { ReadOnly1 a(m_f5); }
193 // check that we see a write when we pass by non-const ref
194 void method6()
196 int& r = m_f6;
197 r = 1;
201 struct ReadOnlyAnalysis2
202 // expected-error@-1 {{write m_r2f1 [loplugin:unusedfields]}}
204 int m_r2f1;
207 ReadOnlyAnalysis2 global { 1 };
209 struct ReadOnlyAnalysis3
210 // expected-error@-1 {{read m_f1 [loplugin:unusedfields]}}
211 // expected-error@-2 {{outside-constructor m_f1 [loplugin:unusedfields]}}
213 int m_f1;
215 void func1()
217 if (m_f1)
218 m_f1 = 1;
222 // Verify the special logic for container fields that only contains mutations that
223 // add elements.
224 struct ReadOnlyAnalysis4
225 // expected-error@-1 {{read m_readonly [loplugin:unusedfields]}}
226 // expected-error@-2 {{write m_writeonly [loplugin:unusedfields]}}
227 // expected-error@-3 {{read m_readonlyCss [loplugin:unusedfields]}}
228 // expected-error@-4 {{outside-constructor m_readonly [loplugin:unusedfields]}}
229 // expected-error@-5 {{outside-constructor m_readonlyCss [loplugin:unusedfields]}}
230 // expected-error@-6 {{outside-constructor m_writeonly [loplugin:unusedfields]}}
232 std::vector<int> m_readonly;
233 std::vector<int> m_writeonly;
234 css::uno::Sequence<sal_Int32> m_readonlyCss;
236 void func1()
238 int x = m_readonly[0];
239 (void)x;
240 *m_readonly.begin() = 1;
242 m_writeonly.push_back(0);
243 m_writeonly.clear();
245 x = m_readonlyCss.getArray()[0];
249 template<class T>
250 struct VclPtr
252 VclPtr(T*);
253 void clear();
256 // Check calls to operators
257 struct WriteOnlyAnalysis2
258 // expected-error@-1 {{write m_vclwriteonly [loplugin:unusedfields]}}
259 // expected-error@-2 {{outside-constructor m_vclwriteonly [loplugin:unusedfields]}}
261 VclPtr<int> m_vclwriteonly;
263 WriteOnlyAnalysis2() : m_vclwriteonly(nullptr)
265 m_vclwriteonly = nullptr;
268 ~WriteOnlyAnalysis2()
270 m_vclwriteonly.clear();
274 namespace WriteOnlyAnalysis3
276 void setFoo(int);
277 struct Foo1
278 // expected-error@-1 {{read m_field1 [loplugin:unusedfields]}}
279 // expected-error@-2 {{write m_field1 [loplugin:unusedfields]}}
280 // expected-error@-3 {{outside-constructor m_field1 [loplugin:unusedfields]}}
282 int m_field1;
283 Foo1() : m_field1(1) {}
284 ~Foo1()
286 setFoo(m_field1);
291 // Check that writes to fields that are wrapped by conditional checks are ignored,
292 // where those conditional checks use an 'operator bool'
293 namespace ReadOnlyAnalysis5
295 struct RefTarget
297 void acquire();
298 void release();
300 struct Foo1
301 // expected-error@-1 {{read m_field1 [loplugin:unusedfields]}}
302 // expected-error@-2 {{read m_field2 [loplugin:unusedfields]}}
303 // expected-error@-3 {{read m_field3xx [loplugin:unusedfields]}}
304 // expected-error@-4 {{outside-constructor m_field1 [loplugin:unusedfields]}}
305 // expected-error@-5 {{outside-constructor m_field2 [loplugin:unusedfields]}}
306 // expected-error@-6 {{outside-constructor m_field3xx [loplugin:unusedfields]}}
308 std::unique_ptr<int> m_field1;
309 rtl::Reference<RefTarget> m_field2;
310 css::uno::Reference<css::uno::XInterface> m_field3xx;
311 void f1(css::uno::Reference<css::uno::XInterface> a)
313 if (m_field1)
314 m_field1.reset(new int);
315 if (m_field1.get())
316 m_field1.reset(new int);
317 if (m_field2)
318 m_field2 = new RefTarget;
319 if (m_field2.get())
320 m_field2 = new RefTarget;
321 if (m_field3xx)
322 m_field3xx = a;
323 if (m_field3xx.get())
324 m_field3xx = a;
330 namespace TouchFromOutsideConstructorAnalysis1
332 struct RenderContextGuard
333 // expected-error@-1 {{write m_pRef [loplugin:unusedfields]}}
334 // expected-error@-2 {{read m_pRef [loplugin:unusedfields]}}
335 // expected-error@-3 {{write m_pOriginalValue [loplugin:unusedfields]}}
337 int& m_pRef;
338 int m_pOriginalValue;
340 RenderContextGuard(int& pRef, int pValue)
341 : m_pRef(pRef),
342 m_pOriginalValue(m_pRef)
344 m_pRef = pValue;
349 namespace TouchFromOutsideAnalysis1
351 struct SwViewShell
353 int* GetWin();
354 int* Imp();
356 struct RenderContextGuard
357 // expected-error@-1 {{write m_pShell [loplugin:unusedfields]}}
358 // expected-error@-2 {{read m_pShell [loplugin:unusedfields]}}
360 SwViewShell* m_pShell;
362 RenderContextGuard(SwViewShell* pShell)
363 : m_pShell(pShell)
365 if (m_pShell->GetWin())
367 int* pDrawView(m_pShell->Imp());
369 if (nullptr != pDrawView)
371 FindPageWindow(*m_pShell->GetWin());
376 void FindPageWindow(int x);
380 #endif
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */