1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
10 #if defined _WIN32 // TODO, see corresponding TODO in compilerplugins/clang/unusedfields.cxx
11 // expected-no-diagnostics
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>
23 // expected-error@-1 {{read m_foo1 [loplugin:unusedfields]}}
24 // expected-error@-2 {{outside m_foo1 [loplugin:unusedfields]}}
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]}}
66 void (*m_barfunctionpointer
)(int&);
68 std::vector
<int> m_bar6
;
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
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
116 // check that we don't see reads when calling operator>>=
123 // check that we see don't see writes when calling operator<<=
131 // check that we see reads of the LHS when calling operator>>=
138 // check that we see writes of the LHS when calling operator<<=
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
;
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]}}
174 std::vector
<int> m_f4
;
178 // check that we don't see a write of m_f1
179 ReadOnlyAnalysis() : m_f1(0) {}
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
201 struct ReadOnlyAnalysis2
202 // expected-error@-1 {{write m_r2f1 [loplugin:unusedfields]}}
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]}}
222 // Verify the special logic for container fields that only contains mutations that
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
;
238 int x
= m_readonly
[0];
240 *m_readonly
.begin() = 1;
242 m_writeonly
.push_back(0);
245 x
= m_readonlyCss
.getArray()[0];
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
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]}}
283 Foo1() : m_field1(1) {}
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
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
)
314 m_field1
.reset(new int);
316 m_field1
.reset(new int);
318 m_field2
= new RefTarget
;
320 m_field2
= new RefTarget
;
323 if (m_field3xx
.get())
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]}}
338 int m_pOriginalValue
;
340 RenderContextGuard(int& pRef
, int pValue
)
342 m_pOriginalValue(m_pRef
)
349 namespace TouchFromOutsideAnalysis1
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
)
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
);
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */