Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / unusedmember.cxx
blob90f3051b98ee8dbab8ec79cfb040fe9d0e3b5e3f
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 <cstddef>
12 namespace Enum
14 namespace
16 struct S
18 enum E1
20 E11,
21 E12
23 E1 e1;
24 enum E2
26 E21,
27 E22
29 E2 e2; // expected-error {{unused class member [loplugin:unusedmember]}}
30 enum E3
32 E31,
33 E32
34 } e3;
35 enum E4
37 E41,
38 E42
39 } e4; // expected-error {{unused class member [loplugin:unusedmember]}}
42 void f(S s)
44 (void)s.e1;
45 (void)s.e3;
49 namespace ElaboratedEnum
51 namespace
53 struct S
55 S()
57 enum E1 e1 = E11;
58 (void)e1;
60 enum E1
62 E11,
63 E12
65 enum E2
67 E21,
68 E22
70 enum E2 e2;
73 void f()
75 S s;
76 (void)s;
77 (void)s.e2;
81 namespace UnusedEnum
83 namespace
85 struct S
87 enum E // expected-error {{unused class member [loplugin:unusedmember]}}
89 E1,
94 void f() { (void)S::E1; }
97 namespace UnusedDataMember
99 namespace
101 struct NT
103 NT(int = 0) {}
104 ~NT() {}
106 struct __attribute__((warn_unused)) T
108 T(int = 0) {}
109 ~T() {}
111 struct S
113 int i1;
114 int i2; // expected-error {{unused class member [loplugin:unusedmember]}}
115 int const& i3; // expected-error {{unused class member [loplugin:unusedmember]}}
116 NT nt;
117 T t1;
118 T t2; // expected-error {{unused class member [loplugin:unusedmember]}}
119 T const& t3; // expected-error {{unused class member [loplugin:unusedmember]}}
121 : i1(0)
122 , i3(i1)
123 , t1(0)
124 , t3(t1)
126 (void)i1;
127 (void)t1;
131 void f()
133 S s;
134 (void)s;
138 namespace Alignof
140 namespace
142 struct S
144 int i;
147 void f() { (void)alignof(S const(&)[][10]); }
150 namespace Aligned
152 namespace
154 struct S1
156 int i;
158 struct S2
160 int i __attribute__((aligned(__alignof__(S1))));
163 void f()
165 S2 s;
166 s.i = 0;
170 namespace Bases
172 namespace
174 struct S1
176 int i1;
178 struct S2 : S1
180 int i2;
182 struct S3 : S2
186 void f() { (void)sizeof(S3); }
189 namespace Unnamed
191 namespace
193 struct S
195 struct
197 struct
199 int i;
200 } s2;
201 struct // anonymous struct extension (widely supported)
203 int j;
205 int k;
206 } s1;
207 #if false //TODO: see corresponding TODO in compilerplugins/clang/unusedmember.cxx
208 static constexpr struct
210 int l; // expected-error {{unused class member [loplugin:unusedmember]}}
211 } s = {};
212 #endif
213 typedef struct
215 int m; // expected-error {{unused class member [loplugin:unusedmember]}}
216 } t; // expected-error {{unused class member [loplugin:unusedmember]}}
219 void f()
221 (void)sizeof(S);
222 #if false //TODO: see corresponding TODO in compilerplugins/clang/unusedmember.cxx
223 (void)S::s; // avoid "unused variable 's'" (non-loplugin) warning
224 #endif
228 namespace Offsetof
230 namespace
232 struct S
234 int i;
237 void f() { (void)offsetof(S, i); }
240 namespace OffsetofTemplate
242 namespace
244 template <typename> struct S
246 int i;
248 template <typename T> void f1() { (void)offsetof(T, i); }
250 void f() { f1<S<void>>(); }
253 int main()
255 (void)&Enum::f;
256 (void)&ElaboratedEnum::f;
257 (void)&UnusedEnum::f;
258 (void)&UnusedDataMember::f;
259 (void)&Alignof::f;
260 (void)&Aligned::f;
261 (void)&Bases::f;
262 (void)&Unnamed::f;
263 (void)&Offsetof::f;
264 (void)&OffsetofTemplate::f;
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */