Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / test / unusedfields.cxx
blobfe81c88ed2050133b7acbecdb4795a40e3a2210a
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 #include <vector>
11 #include <ostream>
12 #include <com/sun/star/uno/Any.hxx>
14 struct Foo
15 // expected-error@-1 {{read m_foo1 [loplugin:unusedfields]}}
17 int m_foo1;
20 struct Bar
21 // expected-error@-1 {{read m_bar2 [loplugin:unusedfields]}}
22 // expected-error@-2 {{read m_bar4 [loplugin:unusedfields]}}
23 // expected-error@-3 {{read m_bar5 [loplugin:unusedfields]}}
24 // expected-error@-4 {{read m_bar6 [loplugin:unusedfields]}}
25 // expected-error@-5 {{read m_barfunctionpointer [loplugin:unusedfields]}}
26 // expected-error@-6 {{read m_bar8 [loplugin:unusedfields]}}
27 // expected-error@-7 {{read m_bar10 [loplugin:unusedfields]}}
28 // expected-error@-8 {{write m_bar1 [loplugin:unusedfields]}}
29 // expected-error@-9 {{write m_bar2 [loplugin:unusedfields]}}
30 // expected-error@-10 {{write m_bar3 [loplugin:unusedfields]}}
31 // expected-error@-11 {{write m_bar3b [loplugin:unusedfields]}}
32 // expected-error@-12 {{write m_bar4 [loplugin:unusedfields]}}
33 // expected-error@-13 {{write m_bar7 [loplugin:unusedfields]}}
34 // expected-error@-14 {{write m_bar9 [loplugin:unusedfields]}}
36 int m_bar1;
37 int m_bar2 = 1;
38 int* m_bar3;
39 int* m_bar3b;
40 int m_bar4;
41 void (*m_barfunctionpointer)(int&);
42 int m_bar5;
43 std::vector<int> m_bar6;
44 int m_bar7[5];
45 int m_bar8;
46 int m_barstream;
47 int m_bar9;
48 int m_bar10;
50 // check that we see reads of fields like m_foo1 when referred to via constructor initializer
51 Bar(Foo const & foo) : m_bar1(foo.m_foo1) {}
53 // check that we don't see reads when inside copy/move constructor
54 Bar(Bar const & other) { m_bar3 = other.m_bar3; }
56 // check that we don't see reads when inside copy/move assignment operator
57 Bar& operator=(Bar const & other) { m_bar3 = other.m_bar3; return *this; }
59 // check that we DON'T see reads here
60 int bar2() { return m_bar2; }
62 // check that we DON'T see reads here
63 void bar3()
65 m_bar3 = nullptr;
66 m_bar3b = m_bar3 = nullptr;
69 // check that we see reads of field when passed to a function pointer
70 // check that we see read of a field that is a function pointer
71 void bar4() { m_barfunctionpointer(m_bar4); }
73 // check that we see reads of a field when used in variable init
74 void bar5() { int x = m_bar5; (void) x; }
76 // check that we see reads of a field when used in ranged-for
77 void bar6() { for (auto i : m_bar6) { (void)i; } }
79 // check that we see don't see reads of array fields
80 void bar7() { m_bar7[3] = 1; }
82 // check that we see reads when a field is used in an array expression
83 char bar8()
85 char tmp[5];
86 return tmp[m_bar8];
89 // check that we don't see reads when calling operator>>=
90 void bar9()
92 css::uno::Any any;
93 any >>= m_bar9;
96 // check that we see don't see writes when calling operator<<=
97 void bar10()
99 css::uno::Any any;
100 any <<= m_bar10;
104 // check that we __dont__ see a read of m_barstream
105 std::ostream& operator<<(std::ostream& s, Bar const & bar)
107 s << bar.m_barstream;
108 return s;
111 struct ReadOnly1 { ReadOnly1(int&); };
113 struct ReadOnlyAnalysis
114 // expected-error@-1 {{read m_f2 [loplugin:unusedfields]}}
115 // expected-error@-2 {{read m_f3 [loplugin:unusedfields]}}
116 // expected-error@-3 {{read m_f4 [loplugin:unusedfields]}}
117 // expected-error@-4 {{read m_f5 [loplugin:unusedfields]}}
118 // expected-error@-5 {{read m_f6 [loplugin:unusedfields]}}
119 // expected-error@-6 {{write m_f2 [loplugin:unusedfields]}}
120 // expected-error@-7 {{write m_f3 [loplugin:unusedfields]}}
121 // expected-error@-8 {{write m_f4 [loplugin:unusedfields]}}
122 // expected-error@-9 {{write m_f5 [loplugin:unusedfields]}}
123 // expected-error@-10 {{write m_f6 [loplugin:unusedfields]}}
125 int m_f1;
126 int m_f2;
127 int m_f3;
128 std::vector<int> m_f4;
129 int m_f5;
130 int m_f6;
132 // check that we don't see a write of m_f1
133 ReadOnlyAnalysis() : m_f1(0) {}
135 void method1(int&);
137 // check that we see a write when we pass by non-const ref
138 void method2() { method1(m_f2); }
140 int& method3() { return m_f3; }
142 void method4() { m_f4.push_back(1); }
144 // check that we see a write when we pass by non-const ref
145 void method5() { ReadOnly1 a(m_f5); }
147 // check that we see a write when we pass by non-const ref
148 void method6()
150 int& r = m_f6;
151 r = 1;
155 struct ReadOnlyAnalysis2
156 // expected-error@-1 {{write m_r2f1 [loplugin:unusedfields]}}
158 int m_r2f1;
161 ReadOnlyAnalysis2 global { 1 };
163 struct ReadOnlyAnalysis3
164 // expected-error@-1 {{read m_f1 [loplugin:unusedfields]}}
166 int m_f1;
168 void func1()
170 if (m_f1)
171 m_f1 = 1;
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */