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/.
9 #include "config_clang.h"
11 // CLANG_VERSION = older versions of clang need something different in getParentFunctionDecl
12 // WIN32 = TODO, see corresponding TODO in compilerplugins/clang/unusedfields.cxx
13 #if CLANG_VERSION < 110000 || defined _WIN32
14 // expected-no-diagnostics
19 #include <com/sun/star/uno/Any.hxx>
20 #include <com/sun/star/uno/Sequence.hxx>
21 #include <com/sun/star/uno/XInterface.hpp>
22 #include <rtl/ref.hxx>
25 // expected-error@-1 {{read m_foo1 [loplugin:unusedfields]}}
26 // expected-error@-2 {{outside m_foo1 [loplugin:unusedfields]}}
32 // expected-error@-1 {{read m_bar2 [loplugin:unusedfields]}}
33 // expected-error@-2 {{read m_bar4 [loplugin:unusedfields]}}
34 // expected-error@-3 {{read m_bar5 [loplugin:unusedfields]}}
35 // expected-error@-4 {{read m_bar6 [loplugin:unusedfields]}}
36 // expected-error@-5 {{read m_barfunctionpointer [loplugin:unusedfields]}}
37 // expected-error@-6 {{read m_bar8 [loplugin:unusedfields]}}
38 // expected-error@-7 {{read m_bar10 [loplugin:unusedfields]}}
39 // expected-error@-8 {{read m_bar11 [loplugin:unusedfields]}}
40 // expected-error@-9 {{write m_bar1 [loplugin:unusedfields]}}
41 // expected-error@-10 {{write m_bar2 [loplugin:unusedfields]}}
42 // expected-error@-11 {{write m_bar3 [loplugin:unusedfields]}}
43 // expected-error@-12 {{write m_bar3b [loplugin:unusedfields]}}
44 // expected-error@-13 {{write m_bar4 [loplugin:unusedfields]}}
45 // expected-error@-14 {{write m_bar7 [loplugin:unusedfields]}}
46 // expected-error@-15 {{write m_bar9 [loplugin:unusedfields]}}
47 // expected-error@-16 {{write m_bar12 [loplugin:unusedfields]}}
48 // expected-error@-17 {{outside-constructor m_bar2 [loplugin:unusedfields]}}
49 // expected-error@-18 {{outside-constructor m_bar3 [loplugin:unusedfields]}}
50 // expected-error@-19 {{outside-constructor m_bar3b [loplugin:unusedfields]}}
51 // expected-error@-20 {{outside-constructor m_bar4 [loplugin:unusedfields]}}
52 // expected-error@-21 {{outside-constructor m_bar5 [loplugin:unusedfields]}}
53 // expected-error@-22 {{outside-constructor m_bar6 [loplugin:unusedfields]}}
54 // expected-error@-23 {{outside-constructor m_bar7 [loplugin:unusedfields]}}
55 // expected-error@-24 {{outside-constructor m_bar8 [loplugin:unusedfields]}}
56 // expected-error@-25 {{outside-constructor m_bar9 [loplugin:unusedfields]}}
57 // expected-error@-26 {{outside-constructor m_bar10 [loplugin:unusedfields]}}
58 // expected-error@-27 {{outside-constructor m_bar11 [loplugin:unusedfields]}}
59 // expected-error@-28 {{outside-constructor m_bar12 [loplugin:unusedfields]}}
60 // expected-error@-29 {{outside-constructor m_barfunctionpointer [loplugin:unusedfields]}}
61 // expected-error@-30 {{outside m_barstream [loplugin:unusedfields]}}
68 void (*m_barfunctionpointer
)(int&);
70 std::vector
<int> m_bar6
;
76 css::uno::Any m_bar11
;
77 css::uno::Any m_bar12
;
79 // check that we see reads of fields like m_foo1 when referred to via constructor initializer
80 Bar(Foo
const & foo
) : m_bar1(foo
.m_foo1
) {}
82 // check that we don't see reads when inside copy/move constructor
83 Bar(Bar
const & other
) { m_bar3
= other
.m_bar3
; }
85 // check that we don't see reads when inside copy/move assignment operator
86 Bar
& operator=(Bar
const & other
) { m_bar3
= other
.m_bar3
; return *this; }
88 // check that we DON'T see reads here
89 int bar2() { return m_bar2
; }
91 // check that we DON'T see reads here
95 m_bar3b
= m_bar3
= nullptr;
98 // check that we see reads of field when passed to a function pointer
99 // check that we see read of a field that is a function pointer
100 void bar4() { m_barfunctionpointer(m_bar4
); }
102 // check that we see reads of a field when used in variable init
103 void bar5() { int x
= m_bar5
; (void) x
; }
105 // check that we see reads of a field when used in ranged-for
106 void bar6() { for (auto i
: m_bar6
) { (void)i
; } }
108 // check that we see don't see reads of array fields
109 void bar7() { m_bar7
[3] = 1; }
111 // check that we see reads when a field is used in an array expression
118 // check that we don't see reads when calling operator>>=
125 // check that we see don't see writes when calling operator<<=
132 // check that we see reads of the LHS when calling operator>>=
139 // check that we see writes of the LHS when calling operator<<=
147 // check that we __dont__ see a read of m_barstream
148 std::ostream
& operator<<(std::ostream
& s
, Bar
const & bar
)
150 s
<< bar
.m_barstream
;
154 struct ReadOnly1
{ ReadOnly1(int&); };
156 struct ReadOnlyAnalysis
157 // expected-error@-1 {{read m_f2 [loplugin:unusedfields]}}
158 // expected-error@-2 {{read m_f3 [loplugin:unusedfields]}}
159 // expected-error@-3 {{read m_f5 [loplugin:unusedfields]}}
160 // expected-error@-4 {{read m_f6 [loplugin:unusedfields]}}
161 // expected-error@-5 {{write m_f2 [loplugin:unusedfields]}}
162 // expected-error@-6 {{write m_f3 [loplugin:unusedfields]}}
163 // expected-error@-7 {{write m_f4 [loplugin:unusedfields]}}
164 // expected-error@-8 {{write m_f5 [loplugin:unusedfields]}}
165 // expected-error@-9 {{write m_f6 [loplugin:unusedfields]}}
166 // expected-error@-10 {{outside-constructor m_f2 [loplugin:unusedfields]}}
167 // expected-error@-11 {{outside-constructor m_f3 [loplugin:unusedfields]}}
168 // expected-error@-12 {{outside-constructor m_f4 [loplugin:unusedfields]}}
169 // expected-error@-13 {{outside-constructor m_f5 [loplugin:unusedfields]}}
170 // expected-error@-14 {{outside-constructor m_f6 [loplugin:unusedfields]}}
175 std::vector
<int> m_f4
;
179 // check that we don't see a write of m_f1
180 ReadOnlyAnalysis() : m_f1(0) {}
184 // check that we see a write when we pass by non-const ref
185 void method2() { method1(m_f2
); }
187 int& method3() { return m_f3
; }
189 void method4() { m_f4
.push_back(1); }
191 // check that we see a write when we pass by non-const ref
192 void method5() { ReadOnly1
a(m_f5
); }
194 // check that we see a write when we pass by non-const ref
202 struct ReadOnlyAnalysis2
203 // expected-error@-1 {{write m_r2f1 [loplugin:unusedfields]}}
208 ReadOnlyAnalysis2 global
{ 1 };
210 struct ReadOnlyAnalysis3
211 // expected-error@-1 {{read m_f1 [loplugin:unusedfields]}}
212 // expected-error@-2 {{outside-constructor m_f1 [loplugin:unusedfields]}}
223 // Verify the special logic for container fields that only contains mutations that
225 struct ReadOnlyAnalysis4
226 // expected-error@-1 {{read m_readonly [loplugin:unusedfields]}}
227 // expected-error@-2 {{write m_writeonly [loplugin:unusedfields]}}
228 // expected-error@-3 {{read m_readonlyCss [loplugin:unusedfields]}}
229 // expected-error@-4 {{outside-constructor m_readonly [loplugin:unusedfields]}}
230 // expected-error@-5 {{outside-constructor m_readonlyCss [loplugin:unusedfields]}}
231 // expected-error@-6 {{outside-constructor m_writeonly [loplugin:unusedfields]}}
233 std::vector
<int> m_readonly
;
234 std::vector
<int> m_writeonly
;
235 css::uno::Sequence
<sal_Int32
> m_readonlyCss
;
239 int x
= m_readonly
[0];
241 *m_readonly
.begin() = 1;
243 m_writeonly
.push_back(0);
246 x
= m_readonlyCss
.getArray()[0];
257 // Check calls to operators
258 struct WriteOnlyAnalysis2
259 // expected-error@-1 {{write m_vclwriteonly [loplugin:unusedfields]}}
260 // expected-error@-2 {{outside-constructor m_vclwriteonly [loplugin:unusedfields]}}
262 VclPtr
<int> m_vclwriteonly
;
264 WriteOnlyAnalysis2() : m_vclwriteonly(nullptr)
266 m_vclwriteonly
= nullptr;
269 ~WriteOnlyAnalysis2()
271 m_vclwriteonly
.clear();
275 namespace WriteOnlyAnalysis3
279 // expected-error@-1 {{read m_field1 [loplugin:unusedfields]}}
280 // expected-error@-2 {{write m_field1 [loplugin:unusedfields]}}
281 // expected-error@-3 {{outside-constructor m_field1 [loplugin:unusedfields]}}
284 Foo1() : m_field1(1) {}
292 // Check that writes to fields that are wrapped by conditional checks are ignored,
293 // where those conditional checks use an 'operator bool'
294 namespace ReadOnlyAnalysis5
302 // expected-error@-1 {{read m_field1 [loplugin:unusedfields]}}
303 // expected-error@-2 {{read m_field2 [loplugin:unusedfields]}}
304 // expected-error@-3 {{read m_field3xx [loplugin:unusedfields]}}
305 // expected-error@-4 {{outside-constructor m_field1 [loplugin:unusedfields]}}
306 // expected-error@-5 {{outside-constructor m_field2 [loplugin:unusedfields]}}
307 // expected-error@-6 {{outside-constructor m_field3xx [loplugin:unusedfields]}}
309 std::unique_ptr
<int> m_field1
;
310 rtl::Reference
<RefTarget
> m_field2
;
311 css::uno::Reference
<css::uno::XInterface
> m_field3xx
;
312 void f1(css::uno::Reference
<css::uno::XInterface
> a
)
315 m_field1
.reset(new int);
317 m_field1
.reset(new int);
319 m_field2
= new RefTarget
;
321 m_field2
= new RefTarget
;
324 if (m_field3xx
.get())
331 namespace TouchFromOutsideConstructorAnalysis1
333 struct RenderContextGuard
334 // expected-error@-1 {{write m_pRef [loplugin:unusedfields]}}
335 // expected-error@-2 {{read m_pRef [loplugin:unusedfields]}}
336 // expected-error@-3 {{write m_pOriginalValue [loplugin:unusedfields]}}
339 int m_pOriginalValue
;
341 RenderContextGuard(int& pRef
, int pValue
)
343 m_pOriginalValue(m_pRef
)
350 namespace TouchFromOutsideAnalysis1
357 struct RenderContextGuard
358 // expected-error@-1 {{write m_pShell [loplugin:unusedfields]}}
359 // expected-error@-2 {{read m_pShell [loplugin:unusedfields]}}
361 SwViewShell
* m_pShell
;
363 RenderContextGuard(SwViewShell
* pShell
)
366 if (m_pShell
->GetWin())
368 int* pDrawView(m_pShell
->Imp());
370 if (nullptr != pDrawView
)
372 FindPageWindow(*m_pShell
->GetWin());
377 void FindPageWindow(int x
);
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */