bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / test / unusedfields.cxx
blob2ec4ab815414699e4a7e800f46fb9a5f44010257
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 <vector>
15 #include <ostream>
16 #include <com/sun/star/uno/Any.hxx>
17 #include <com/sun/star/uno/Sequence.hxx>
19 struct Foo
20 // expected-error@-1 {{read m_foo1 [loplugin:unusedfields]}}
22 int m_foo1;
25 struct Bar
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]}}
43 int m_bar1;
44 int m_bar2 = 1;
45 int* m_bar3;
46 int* m_bar3b;
47 int m_bar4;
48 void (*m_barfunctionpointer)(int&);
49 int m_bar5;
50 std::vector<int> m_bar6;
51 int m_bar7[5];
52 int m_bar8;
53 int m_barstream;
54 sal_Int32 m_bar9;
55 sal_Int32 m_bar10;
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
72 void bar3()
74 m_bar3 = nullptr;
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
92 char bar8()
94 char tmp[5];
95 return tmp[m_bar8];
98 // check that we don't see reads when calling operator>>=
99 void bar9()
101 css::uno::Any any;
102 any >>= m_bar9;
105 // check that we see don't see writes when calling operator<<=
106 void bar10()
108 css::uno::Any any;
109 any <<= m_bar10;
112 // check that we see reads of the LHS when calling operator>>=
113 void bar11()
115 int x;
116 m_bar11 >>= x;
119 // check that we see writes of the LHS when calling operator<<=
120 void bar12()
122 int x;
123 m_bar12 <<= x;
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;
131 return s;
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]}}
147 int m_f1;
148 int m_f2;
149 int m_f3;
150 std::vector<int> m_f4;
151 int m_f5;
152 int m_f6;
154 // check that we don't see a write of m_f1
155 ReadOnlyAnalysis() : m_f1(0) {}
157 void method1(int&);
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
170 void method6()
172 int& r = m_f6;
173 r = 1;
177 struct ReadOnlyAnalysis2
178 // expected-error@-1 {{write m_r2f1 [loplugin:unusedfields]}}
180 int m_r2f1;
183 ReadOnlyAnalysis2 global { 1 };
185 struct ReadOnlyAnalysis3
186 // expected-error@-1 {{read m_f1 [loplugin:unusedfields]}}
188 int m_f1;
190 void func1()
192 if (m_f1)
193 m_f1 = 1;
197 // Verify the special logic for container fields that only contains mutations that
198 // add elements.
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;
208 void func1()
210 int x = m_readonly[0];
211 (void)x;
212 *m_readonly.begin() = 1;
214 m_writeonly.push_back(0);
215 m_writeonly.clear();
217 x = m_readonlyCss.getArray()[0];
221 template<class T>
222 struct VclPtr
224 VclPtr(T*);
225 void clear();
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
247 void setFoo(int);
248 struct Foo1
249 // expected-error@-1 {{read m_field1 [loplugin:unusedfields]}}
250 // expected-error@-2 {{write m_field1 [loplugin:unusedfields]}}
252 int m_field1;
253 Foo1() : m_field1(1) {}
254 ~Foo1()
256 setFoo(m_field1);
261 #endif
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */