nss: upgrade to release 3.73
[LibreOffice.git] / compilerplugins / clang / test / unusedfields.cxx
blob42b49a5df832999bd698301377367d146ea1dfe6
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 */
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
15 #else
17 #include <vector>
18 #include <ostream>
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>
24 struct Foo
25 // expected-error@-1 {{read m_foo1 [loplugin:unusedfields]}}
26 // expected-error@-2 {{outside m_foo1 [loplugin:unusedfields]}}
28 int m_foo1;
31 struct Bar
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]}}
63 int m_bar1;
64 int m_bar2 = 1;
65 int* m_bar3;
66 int* m_bar3b;
67 int m_bar4;
68 void (*m_barfunctionpointer)(int&);
69 int m_bar5;
70 std::vector<int> m_bar6;
71 int m_bar7[5];
72 int m_bar8;
73 int m_barstream;
74 sal_Int32 m_bar9;
75 sal_Int32 m_bar10;
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
92 void bar3()
94 m_bar3 = nullptr;
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
112 char bar8()
114 char tmp[5];
115 return tmp[m_bar8];
118 // check that we don't see reads when calling operator>>=
119 void bar9()
121 css::uno::Any any;
122 any >>= m_bar9;
125 // check that we see don't see writes when calling operator<<=
126 void bar10()
128 css::uno::Any any;
129 any <<= m_bar10;
132 // check that we see reads of the LHS when calling operator>>=
133 void bar11()
135 int x;
136 m_bar11 >>= x;
139 // check that we see writes of the LHS when calling operator<<=
140 void bar12()
142 int x = 0;
143 m_bar12 <<= x;
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;
151 return s;
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]}}
172 int m_f1;
173 int m_f2;
174 int m_f3;
175 std::vector<int> m_f4;
176 int m_f5;
177 int m_f6;
179 // check that we don't see a write of m_f1
180 ReadOnlyAnalysis() : m_f1(0) {}
182 void method1(int&);
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
195 void method6()
197 int& r = m_f6;
198 r = 1;
202 struct ReadOnlyAnalysis2
203 // expected-error@-1 {{write m_r2f1 [loplugin:unusedfields]}}
205 int m_r2f1;
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]}}
214 int m_f1;
216 void func1()
218 if (m_f1)
219 m_f1 = 1;
223 // Verify the special logic for container fields that only contains mutations that
224 // add elements.
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;
237 void func1()
239 int x = m_readonly[0];
240 (void)x;
241 *m_readonly.begin() = 1;
243 m_writeonly.push_back(0);
244 m_writeonly.clear();
246 x = m_readonlyCss.getArray()[0];
250 template<class T>
251 struct VclPtr
253 VclPtr(T*);
254 void clear();
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
277 void setFoo(int);
278 struct Foo1
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]}}
283 int m_field1;
284 Foo1() : m_field1(1) {}
285 ~Foo1()
287 setFoo(m_field1);
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
296 struct RefTarget
298 void acquire();
299 void release();
301 struct Foo1
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)
314 if (m_field1)
315 m_field1.reset(new int);
316 if (m_field1.get())
317 m_field1.reset(new int);
318 if (m_field2)
319 m_field2 = new RefTarget;
320 if (m_field2.get())
321 m_field2 = new RefTarget;
322 if (m_field3xx)
323 m_field3xx = a;
324 if (m_field3xx.get())
325 m_field3xx = a;
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]}}
338 int& m_pRef;
339 int m_pOriginalValue;
341 RenderContextGuard(int& pRef, int pValue)
342 : m_pRef(pRef),
343 m_pOriginalValue(m_pRef)
345 m_pRef = pValue;
350 namespace TouchFromOutsideAnalysis1
352 struct SwViewShell
354 int* GetWin();
355 int* Imp();
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)
364 : m_pShell(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);
381 #endif
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */