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
{
11 EqDefaultCompare(int x
) : used(x
) {}
12 bool operator==(const EqDefaultCompare
&) const = default;
15 class SpaceShipDefaultCompare
{
19 SpaceShipDefaultCompare(int x
) : used(x
) {}
20 int operator<=>(const SpaceShipDefaultCompare
&) const = default;
25 class NotFullyDefined
{
32 class HasUndefinedNestedClass
{
37 class HasUndefinedPureVirtualDestructor
{
38 virtual ~HasUndefinedPureVirtualDestructor() = 0;
42 class HasDefinedNestedClasses
{
45 int unused_
; // expected-warning{{private field 'unused_' is not used}}
47 class HasDefinedNestedClasses::DefinedOutside
{};
49 class HasUndefinedFriendFunction
{
50 friend void undefinedFriendFunction();
54 class HasUndefinedFriendClass
{
55 friend class NotFullyDefined
;
56 friend class NotDefined
;
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
;
74 template <typename T
> class TemplateFriend
{
76 TemplateFriend(ClassWithTemplateFriend my_friend
) {
77 int var
= my_friend
.used_by_friend_
;
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
{
94 NonTrivialConstructor() {}
97 class NonTrivialDestructor
{
99 ~NonTrivialDestructor() {}
114 A() : primitive_type_(42), default_initializer_(), other_initializer_(42),
115 trivial_(), user_constructor_(42),
116 initialized_with_side_effect_(side_effect()) {
118 attr_used_
= 42; // expected-warning{{'attr_used_' was marked unused but was used}}
121 A(int x
, A
* a
) : pointer_(a
) {}
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
{
148 EverythingUsed() : as_array_index_(0), var_(by_initializer_
) {
149 var_
= sizeof(sizeof_
);
150 int *use
= &by_reference_
;
152 test
[as_array_index_
] = 42;
153 int EverythingUsed::*ptr
= &EverythingUsed::by_pointer_to_member_
;
158 by_template_function_
= 42;
165 int by_template_function_
;
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
178 namespace templates
{
180 template <typename T
> void f(T t
);
183 } // namespace templates
185 namespace mutual_friends
{
186 // Undefined methods make mutual friends undefined.
190 void doSomethingToAOrB();
197 // Undefined friends do not make a mutual friend undefined.
200 void doSomethingElse() {}
205 int d
; // expected-warning{{private field 'd' is not used}}
209 // Undefined nested classes make mutual friends undefined.
219 } // namespace mutual_friends
221 namespace anonymous_structs_unions
{
224 // FIXME: Look at the DeclContext for anonymous structs/unions.
227 unsigned char Data
[8];
233 unsigned char Data
[8];
235 } // namespace anonymous_structs_unions
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}}
253 void (*p
)(int); // expected-warning{{private field 'p' is not used}}
260 B(int n
) : a
{n
}, b
{(f(n
), 0)} {}
262 A a
= A(); // expected-warning{{private field 'a' is not used}}
268 X x
[4]; // no-warning
272 class implicit_special_member
{
274 static implicit_special_member
make() { return implicit_special_member(); }
277 int n
; // expected-warning{{private field 'n' is not used}}
280 class defaulted_special_member
{
282 defaulted_special_member(const defaulted_special_member
&) = default;
285 int n
; // expected-warning{{private field 'n' is not used}}