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
16 #include <com/sun/star/uno/Any.hxx>
17 #include <com/sun/star/uno/Sequence.hxx>
20 // expected-error@-1 {{read m_foo1 [loplugin:unusedfields]}}
26 // expected-error@-1 {{read m_bar2 [loplugin:unusedfields]}}
27 // expected-error@-2 {{read m_bar4 [loplugin:unusedfields]}}
28 // expected-error@-3 {{read m_bar5 [loplugin:unusedfields]}}
29 // expected-error@-4 {{read m_bar6 [loplugin:unusedfields]}}
30 // expected-error@-5 {{read m_barfunctionpointer [loplugin:unusedfields]}}
31 // expected-error@-6 {{read m_bar8 [loplugin:unusedfields]}}
32 // expected-error@-7 {{read m_bar10 [loplugin:unusedfields]}}
33 // expected-error@-8 {{read m_bar11 [loplugin:unusedfields]}}
34 // expected-error@-9 {{write m_bar1 [loplugin:unusedfields]}}
35 // expected-error@-10 {{write m_bar2 [loplugin:unusedfields]}}
36 // expected-error@-11 {{write m_bar3 [loplugin:unusedfields]}}
37 // expected-error@-12 {{write m_bar3b [loplugin:unusedfields]}}
38 // expected-error@-13 {{write m_bar4 [loplugin:unusedfields]}}
39 // expected-error@-14 {{write m_bar7 [loplugin:unusedfields]}}
40 // expected-error@-15 {{write m_bar9 [loplugin:unusedfields]}}
41 // expected-error@-16 {{write m_bar12 [loplugin:unusedfields]}}
48 void (*m_barfunctionpointer
)(int&);
50 std::vector
<int> m_bar6
;
56 css::uno::Any m_bar11
;
57 css::uno::Any m_bar12
;
59 // check that we see reads of fields like m_foo1 when referred to via constructor initializer
60 Bar(Foo
const & foo
) : m_bar1(foo
.m_foo1
) {}
62 // check that we don't see reads when inside copy/move constructor
63 Bar(Bar
const & other
) { m_bar3
= other
.m_bar3
; }
65 // check that we don't see reads when inside copy/move assignment operator
66 Bar
& operator=(Bar
const & other
) { m_bar3
= other
.m_bar3
; return *this; }
68 // check that we DON'T see reads here
69 int bar2() { return m_bar2
; }
71 // check that we DON'T see reads here
75 m_bar3b
= m_bar3
= nullptr;
78 // check that we see reads of field when passed to a function pointer
79 // check that we see read of a field that is a function pointer
80 void bar4() { m_barfunctionpointer(m_bar4
); }
82 // check that we see reads of a field when used in variable init
83 void bar5() { int x
= m_bar5
; (void) x
; }
85 // check that we see reads of a field when used in ranged-for
86 void bar6() { for (auto i
: m_bar6
) { (void)i
; } }
88 // check that we see don't see reads of array fields
89 void bar7() { m_bar7
[3] = 1; }
91 // check that we see reads when a field is used in an array expression
98 // check that we don't see reads when calling operator>>=
105 // check that we see don't see writes when calling operator<<=
112 // check that we see reads of the LHS when calling operator>>=
119 // check that we see writes of the LHS when calling operator<<=
127 // check that we __dont__ see a read of m_barstream
128 std::ostream
& operator<<(std::ostream
& s
, Bar
const & bar
)
130 s
<< bar
.m_barstream
;
134 struct ReadOnly1
{ ReadOnly1(int&); };
136 struct ReadOnlyAnalysis
137 // expected-error@-1 {{read m_f2 [loplugin:unusedfields]}}
138 // expected-error@-2 {{read m_f3 [loplugin:unusedfields]}}
139 // expected-error@-3 {{read m_f5 [loplugin:unusedfields]}}
140 // expected-error@-4 {{read m_f6 [loplugin:unusedfields]}}
141 // expected-error@-5 {{write m_f2 [loplugin:unusedfields]}}
142 // expected-error@-6 {{write m_f3 [loplugin:unusedfields]}}
143 // expected-error@-7 {{write m_f4 [loplugin:unusedfields]}}
144 // expected-error@-8 {{write m_f5 [loplugin:unusedfields]}}
145 // expected-error@-9 {{write m_f6 [loplugin:unusedfields]}}
150 std::vector
<int> m_f4
;
154 // check that we don't see a write of m_f1
155 ReadOnlyAnalysis() : m_f1(0) {}
159 // check that we see a write when we pass by non-const ref
160 void method2() { method1(m_f2
); }
162 int& method3() { return m_f3
; }
164 void method4() { m_f4
.push_back(1); }
166 // check that we see a write when we pass by non-const ref
167 void method5() { ReadOnly1
a(m_f5
); }
169 // check that we see a write when we pass by non-const ref
177 struct ReadOnlyAnalysis2
178 // expected-error@-1 {{write m_r2f1 [loplugin:unusedfields]}}
183 ReadOnlyAnalysis2 global
{ 1 };
185 struct ReadOnlyAnalysis3
186 // expected-error@-1 {{read m_f1 [loplugin:unusedfields]}}
197 // Verify the special logic for container fields that only contains mutations that
199 struct ReadOnlyAnalysis4
200 // expected-error@-1 {{read m_readonly [loplugin:unusedfields]}}
201 // expected-error@-2 {{write m_writeonly [loplugin:unusedfields]}}
202 // expected-error@-3 {{read m_readonlyCss [loplugin:unusedfields]}}
204 std::vector
<int> m_readonly
;
205 std::vector
<int> m_writeonly
;
206 css::uno::Sequence
<sal_Int32
> m_readonlyCss
;
210 int x
= m_readonly
[0];
212 *m_readonly
.begin() = 1;
214 m_writeonly
.push_back(0);
217 x
= m_readonlyCss
.getArray()[0];
228 // Check calls to operators
229 struct WriteOnlyAnalysis2
230 // expected-error@-1 {{write m_vclwriteonly [loplugin:unusedfields]}}
232 VclPtr
<int> m_vclwriteonly
;
234 WriteOnlyAnalysis2() : m_vclwriteonly(nullptr)
236 m_vclwriteonly
= nullptr;
239 ~WriteOnlyAnalysis2()
241 m_vclwriteonly
.clear();
245 namespace WriteOnlyAnalysis3
249 // expected-error@-1 {{read m_field1 [loplugin:unusedfields]}}
250 // expected-error@-2 {{write m_field1 [loplugin:unusedfields]}}
253 Foo1() : m_field1(1) {}
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */