[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / SemaCXX / warn-unused-private-field.cpp
blobe67603eaceae66d1920259c39648998c74823852
1 // RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++11 %s
2 // RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++17 %s
3 // RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++20 %s
5 #if __cplusplus >= 202002L
7 class EqDefaultCompare {
8 int used;
10 public:
11 EqDefaultCompare(int x) : used(x) {}
12 bool operator==(const EqDefaultCompare &) const = default;
15 class SpaceShipDefaultCompare {
16 int used;
18 public:
19 SpaceShipDefaultCompare(int x) : used(x) {}
20 int operator<=>(const SpaceShipDefaultCompare &) const = default;
23 #endif
25 class NotFullyDefined {
26 public:
27 NotFullyDefined();
28 private:
29 int y;
32 class HasUndefinedNestedClass {
33 class Undefined;
34 int unused_;
37 class HasUndefinedPureVirtualDestructor {
38 virtual ~HasUndefinedPureVirtualDestructor() = 0;
39 int unused_;
42 class HasDefinedNestedClasses {
43 class DefinedHere {};
44 class DefinedOutside;
45 int unused_; // expected-warning{{private field 'unused_' is not used}}
47 class HasDefinedNestedClasses::DefinedOutside {};
49 class HasUndefinedFriendFunction {
50 friend void undefinedFriendFunction();
51 int unused_;
54 class HasUndefinedFriendClass {
55 friend class NotFullyDefined;
56 friend class NotDefined;
57 int unused_;
60 class HasFriend {
61 friend class FriendClass;
62 friend void friendFunction(HasFriend f);
63 int unused_; // expected-warning{{private field 'unused_' is not used}}
64 int used_by_friend_class_;
65 int used_by_friend_function_;
68 class ClassWithTemplateFriend {
69 template <typename T> friend class TemplateFriend;
70 int used_by_friend_;
71 int unused_;
74 template <typename T> class TemplateFriend {
75 public:
76 TemplateFriend(ClassWithTemplateFriend my_friend) {
77 int var = my_friend.used_by_friend_;
81 class FriendClass {
82 HasFriend my_friend_;
83 void use() {
84 my_friend_.used_by_friend_class_ = 42;
88 void friendFunction(HasFriend my_friend) {
89 my_friend.used_by_friend_function_ = 42;
92 class NonTrivialConstructor {
93 public:
94 NonTrivialConstructor() {}
97 class NonTrivialDestructor {
98 public:
99 ~NonTrivialDestructor() {}
102 class Trivial {
103 public:
104 Trivial() = default;
105 Trivial(int a) {}
108 int side_effect() {
109 return 42;
112 class A {
113 public:
114 A() : primitive_type_(42), default_initializer_(), other_initializer_(42),
115 trivial_(), user_constructor_(42),
116 initialized_with_side_effect_(side_effect()) {
117 used_ = 42;
118 attr_used_ = 42; // expected-warning{{'attr_used_' was marked unused but was used}}
121 A(int x, A* a) : pointer_(a) {}
123 private:
124 int primitive_type_; // expected-warning{{private field 'primitive_type_' is not used}}
125 A* pointer_; // expected-warning{{private field 'pointer_' is not used}}
126 int no_initializer_; // expected-warning{{private field 'no_initializer_' is not used}}
127 int default_initializer_; // expected-warning{{private field 'default_initializer_' is not used}}
128 int other_initializer_; // expected-warning{{private field 'other_initializer_' is not used}}
129 int used_, unused_; // expected-warning{{private field 'unused_' is not used}}
130 int in_class_initializer_ = 42; // expected-warning{{private field 'in_class_initializer_' is not used}}
131 int in_class_initializer_with_side_effect_ = side_effect();
132 Trivial trivial_initializer_ = Trivial(); // expected-warning{{private field 'trivial_initializer_' is not used}}
133 Trivial non_trivial_initializer_ = Trivial(42);
134 int initialized_with_side_effect_;
135 static int static_fields_are_ignored_;
137 Trivial trivial_; // expected-warning{{private field 'trivial_' is not used}}
138 Trivial user_constructor_;
139 NonTrivialConstructor non_trivial_constructor_;
140 NonTrivialDestructor non_trivial_destructor_;
142 int attr_ __attribute__((unused));
143 int attr_used_ __attribute__((unused));
146 class EverythingUsed {
147 public:
148 EverythingUsed() : as_array_index_(0), var_(by_initializer_) {
149 var_ = sizeof(sizeof_);
150 int *use = &by_reference_;
151 int test[2];
152 test[as_array_index_] = 42;
153 int EverythingUsed::*ptr = &EverythingUsed::by_pointer_to_member_;
156 template<class T>
157 void useStuff(T t) {
158 by_template_function_ = 42;
161 private:
162 int var_;
163 int sizeof_;
164 int by_reference_;
165 int by_template_function_;
166 int as_array_index_;
167 int by_initializer_;
168 int by_pointer_to_member_;
171 class HasFeatureTest {
172 #if __has_feature(attribute_unused_on_fields)
173 int unused_; // expected-warning{{private field 'unused_' is not used}}
174 int unused2_ __attribute__((unused)); // no-warning
175 #endif
178 namespace templates {
179 class B {
180 template <typename T> void f(T t);
181 int a;
183 } // namespace templates
185 namespace mutual_friends {
186 // Undefined methods make mutual friends undefined.
187 class A {
188 int a;
189 friend class B;
190 void doSomethingToAOrB();
192 class B {
193 int b;
194 friend class A;
197 // Undefined friends do not make a mutual friend undefined.
198 class C {
199 int c;
200 void doSomethingElse() {}
201 friend class E;
202 friend class D;
204 class D {
205 int d; // expected-warning{{private field 'd' is not used}}
206 friend class C;
209 // Undefined nested classes make mutual friends undefined.
210 class F {
211 int f;
212 class G;
213 friend class H;
215 class H {
216 int h;
217 friend class F;
219 } // namespace mutual_friends
221 namespace anonymous_structs_unions {
222 class A {
223 private:
224 // FIXME: Look at the DeclContext for anonymous structs/unions.
225 union {
226 int *Aligner;
227 unsigned char Data[8];
230 union S {
231 private:
232 int *Aligner;
233 unsigned char Data[8];
235 } // namespace anonymous_structs_unions
237 namespace pr13413 {
238 class A {
239 A() : p_(__null), b_(false), a_(this), p2_(nullptr) {}
240 void* p_; // expected-warning{{private field 'p_' is not used}}
241 bool b_; // expected-warning{{private field 'b_' is not used}}
242 A* a_; // expected-warning{{private field 'a_' is not used}}
243 void* p2_; // expected-warning{{private field 'p2_' is not used}}
247 namespace pr13543 {
248 void f(int);
249 void f(char);
250 struct S {
251 S() : p(&f) {}
252 private:
253 void (*p)(int); // expected-warning{{private field 'p' is not used}}
256 struct A { int n; };
257 struct B {
258 B() : a(A()) {}
259 B(char) {}
260 B(int n) : a{n}, b{(f(n), 0)} {}
261 private:
262 A a = A(); // expected-warning{{private field 'a' is not used}}
263 A b;
266 struct X { ~X(); };
267 class C {
268 X x[4]; // no-warning
272 class implicit_special_member {
273 public:
274 static implicit_special_member make() { return implicit_special_member(); }
276 private:
277 int n; // expected-warning{{private field 'n' is not used}}
280 class defaulted_special_member {
281 public:
282 defaulted_special_member(const defaulted_special_member&) = default;
284 private:
285 int n; // expected-warning{{private field 'n' is not used}}