1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++11 %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc class NotFullyDefined {
4*f4a2713aSLionel Sambuc public:
5*f4a2713aSLionel Sambuc NotFullyDefined();
6*f4a2713aSLionel Sambuc private:
7*f4a2713aSLionel Sambuc int y;
8*f4a2713aSLionel Sambuc };
9*f4a2713aSLionel Sambuc
10*f4a2713aSLionel Sambuc class HasUndefinedNestedClass {
11*f4a2713aSLionel Sambuc class Undefined;
12*f4a2713aSLionel Sambuc int unused_;
13*f4a2713aSLionel Sambuc };
14*f4a2713aSLionel Sambuc
15*f4a2713aSLionel Sambuc class HasUndefinedPureVirtualDestructor {
16*f4a2713aSLionel Sambuc virtual ~HasUndefinedPureVirtualDestructor() = 0;
17*f4a2713aSLionel Sambuc int unused_;
18*f4a2713aSLionel Sambuc };
19*f4a2713aSLionel Sambuc
20*f4a2713aSLionel Sambuc class HasDefinedNestedClasses {
21*f4a2713aSLionel Sambuc class DefinedHere {};
22*f4a2713aSLionel Sambuc class DefinedOutside;
23*f4a2713aSLionel Sambuc int unused_; // expected-warning{{private field 'unused_' is not used}}
24*f4a2713aSLionel Sambuc };
25*f4a2713aSLionel Sambuc class HasDefinedNestedClasses::DefinedOutside {};
26*f4a2713aSLionel Sambuc
27*f4a2713aSLionel Sambuc class HasUndefinedFriendFunction {
28*f4a2713aSLionel Sambuc friend void undefinedFriendFunction();
29*f4a2713aSLionel Sambuc int unused_;
30*f4a2713aSLionel Sambuc };
31*f4a2713aSLionel Sambuc
32*f4a2713aSLionel Sambuc class HasUndefinedFriendClass {
33*f4a2713aSLionel Sambuc friend class NotFullyDefined;
34*f4a2713aSLionel Sambuc friend class NotDefined;
35*f4a2713aSLionel Sambuc int unused_;
36*f4a2713aSLionel Sambuc };
37*f4a2713aSLionel Sambuc
38*f4a2713aSLionel Sambuc class HasFriend {
39*f4a2713aSLionel Sambuc friend class FriendClass;
40*f4a2713aSLionel Sambuc friend void friendFunction(HasFriend f);
41*f4a2713aSLionel Sambuc int unused_; // expected-warning{{private field 'unused_' is not used}}
42*f4a2713aSLionel Sambuc int used_by_friend_class_;
43*f4a2713aSLionel Sambuc int used_by_friend_function_;
44*f4a2713aSLionel Sambuc };
45*f4a2713aSLionel Sambuc
46*f4a2713aSLionel Sambuc class ClassWithTemplateFriend {
47*f4a2713aSLionel Sambuc template <typename T> friend class TemplateFriend;
48*f4a2713aSLionel Sambuc int used_by_friend_;
49*f4a2713aSLionel Sambuc int unused_;
50*f4a2713aSLionel Sambuc };
51*f4a2713aSLionel Sambuc
52*f4a2713aSLionel Sambuc template <typename T> class TemplateFriend {
53*f4a2713aSLionel Sambuc public:
TemplateFriend(ClassWithTemplateFriend my_friend)54*f4a2713aSLionel Sambuc TemplateFriend(ClassWithTemplateFriend my_friend) {
55*f4a2713aSLionel Sambuc int var = my_friend.used_by_friend_;
56*f4a2713aSLionel Sambuc }
57*f4a2713aSLionel Sambuc };
58*f4a2713aSLionel Sambuc
59*f4a2713aSLionel Sambuc class FriendClass {
60*f4a2713aSLionel Sambuc HasFriend my_friend_;
use()61*f4a2713aSLionel Sambuc void use() {
62*f4a2713aSLionel Sambuc my_friend_.used_by_friend_class_ = 42;
63*f4a2713aSLionel Sambuc }
64*f4a2713aSLionel Sambuc };
65*f4a2713aSLionel Sambuc
friendFunction(HasFriend my_friend)66*f4a2713aSLionel Sambuc void friendFunction(HasFriend my_friend) {
67*f4a2713aSLionel Sambuc my_friend.used_by_friend_function_ = 42;
68*f4a2713aSLionel Sambuc }
69*f4a2713aSLionel Sambuc
70*f4a2713aSLionel Sambuc class NonTrivialConstructor {
71*f4a2713aSLionel Sambuc public:
NonTrivialConstructor()72*f4a2713aSLionel Sambuc NonTrivialConstructor() {}
73*f4a2713aSLionel Sambuc };
74*f4a2713aSLionel Sambuc
75*f4a2713aSLionel Sambuc class NonTrivialDestructor {
76*f4a2713aSLionel Sambuc public:
~NonTrivialDestructor()77*f4a2713aSLionel Sambuc ~NonTrivialDestructor() {}
78*f4a2713aSLionel Sambuc };
79*f4a2713aSLionel Sambuc
80*f4a2713aSLionel Sambuc class Trivial {
81*f4a2713aSLionel Sambuc public:
82*f4a2713aSLionel Sambuc Trivial() = default;
Trivial(int a)83*f4a2713aSLionel Sambuc Trivial(int a) {}
84*f4a2713aSLionel Sambuc };
85*f4a2713aSLionel Sambuc
side_effect()86*f4a2713aSLionel Sambuc int side_effect() {
87*f4a2713aSLionel Sambuc return 42;
88*f4a2713aSLionel Sambuc }
89*f4a2713aSLionel Sambuc
90*f4a2713aSLionel Sambuc class A {
91*f4a2713aSLionel Sambuc public:
A()92*f4a2713aSLionel Sambuc A() : primitive_type_(42), default_initializer_(), other_initializer_(42),
93*f4a2713aSLionel Sambuc trivial_(), user_constructor_(42),
94*f4a2713aSLionel Sambuc initialized_with_side_effect_(side_effect()) {
95*f4a2713aSLionel Sambuc used_ = 42;
96*f4a2713aSLionel Sambuc attr_used_ = 42; // expected-warning{{'attr_used_' was marked unused but was used}}
97*f4a2713aSLionel Sambuc }
98*f4a2713aSLionel Sambuc
A(int x,A * a)99*f4a2713aSLionel Sambuc A(int x, A* a) : pointer_(a) {}
100*f4a2713aSLionel Sambuc
101*f4a2713aSLionel Sambuc private:
102*f4a2713aSLionel Sambuc int primitive_type_; // expected-warning{{private field 'primitive_type_' is not used}}
103*f4a2713aSLionel Sambuc A* pointer_; // expected-warning{{private field 'pointer_' is not used}}
104*f4a2713aSLionel Sambuc int no_initializer_; // expected-warning{{private field 'no_initializer_' is not used}}
105*f4a2713aSLionel Sambuc int default_initializer_; // expected-warning{{private field 'default_initializer_' is not used}}
106*f4a2713aSLionel Sambuc int other_initializer_; // expected-warning{{private field 'other_initializer_' is not used}}
107*f4a2713aSLionel Sambuc int used_, unused_; // expected-warning{{private field 'unused_' is not used}}
108*f4a2713aSLionel Sambuc int in_class_initializer_ = 42; // expected-warning{{private field 'in_class_initializer_' is not used}}
109*f4a2713aSLionel Sambuc int in_class_initializer_with_side_effect_ = side_effect();
110*f4a2713aSLionel Sambuc Trivial trivial_initializer_ = Trivial(); // expected-warning{{private field 'trivial_initializer_' is not used}}
111*f4a2713aSLionel Sambuc Trivial non_trivial_initializer_ = Trivial(42);
112*f4a2713aSLionel Sambuc int initialized_with_side_effect_;
113*f4a2713aSLionel Sambuc static int static_fields_are_ignored_;
114*f4a2713aSLionel Sambuc
115*f4a2713aSLionel Sambuc Trivial trivial_; // expected-warning{{private field 'trivial_' is not used}}
116*f4a2713aSLionel Sambuc Trivial user_constructor_;
117*f4a2713aSLionel Sambuc NonTrivialConstructor non_trivial_constructor_;
118*f4a2713aSLionel Sambuc NonTrivialDestructor non_trivial_destructor_;
119*f4a2713aSLionel Sambuc
120*f4a2713aSLionel Sambuc int attr_ __attribute__((unused));
121*f4a2713aSLionel Sambuc int attr_used_ __attribute__((unused));
122*f4a2713aSLionel Sambuc };
123*f4a2713aSLionel Sambuc
124*f4a2713aSLionel Sambuc class EverythingUsed {
125*f4a2713aSLionel Sambuc public:
EverythingUsed()126*f4a2713aSLionel Sambuc EverythingUsed() : as_array_index_(0), var_(by_initializer_) {
127*f4a2713aSLionel Sambuc var_ = sizeof(sizeof_);
128*f4a2713aSLionel Sambuc int *use = &by_reference_;
129*f4a2713aSLionel Sambuc int test[2];
130*f4a2713aSLionel Sambuc test[as_array_index_] = 42;
131*f4a2713aSLionel Sambuc }
132*f4a2713aSLionel Sambuc
133*f4a2713aSLionel Sambuc template<class T>
useStuff(T t)134*f4a2713aSLionel Sambuc void useStuff(T t) {
135*f4a2713aSLionel Sambuc by_template_function_ = 42;
136*f4a2713aSLionel Sambuc }
137*f4a2713aSLionel Sambuc
138*f4a2713aSLionel Sambuc private:
139*f4a2713aSLionel Sambuc int var_;
140*f4a2713aSLionel Sambuc int sizeof_;
141*f4a2713aSLionel Sambuc int by_reference_;
142*f4a2713aSLionel Sambuc int by_template_function_;
143*f4a2713aSLionel Sambuc int as_array_index_;
144*f4a2713aSLionel Sambuc int by_initializer_;
145*f4a2713aSLionel Sambuc };
146*f4a2713aSLionel Sambuc
147*f4a2713aSLionel Sambuc class HasFeatureTest {
148*f4a2713aSLionel Sambuc #if __has_feature(attribute_unused_on_fields)
149*f4a2713aSLionel Sambuc int unused_; // expected-warning{{private field 'unused_' is not used}}
150*f4a2713aSLionel Sambuc int unused2_ __attribute__((unused)); // no-warning
151*f4a2713aSLionel Sambuc #endif
152*f4a2713aSLionel Sambuc };
153*f4a2713aSLionel Sambuc
154*f4a2713aSLionel Sambuc namespace templates {
155*f4a2713aSLionel Sambuc class B {
156*f4a2713aSLionel Sambuc template <typename T> void f(T t);
157*f4a2713aSLionel Sambuc int a;
158*f4a2713aSLionel Sambuc };
159*f4a2713aSLionel Sambuc } // namespace templates
160*f4a2713aSLionel Sambuc
161*f4a2713aSLionel Sambuc namespace mutual_friends {
162*f4a2713aSLionel Sambuc // Undefined methods make mutual friends undefined.
163*f4a2713aSLionel Sambuc class A {
164*f4a2713aSLionel Sambuc int a;
165*f4a2713aSLionel Sambuc friend class B;
166*f4a2713aSLionel Sambuc void doSomethingToAOrB();
167*f4a2713aSLionel Sambuc };
168*f4a2713aSLionel Sambuc class B {
169*f4a2713aSLionel Sambuc int b;
170*f4a2713aSLionel Sambuc friend class A;
171*f4a2713aSLionel Sambuc };
172*f4a2713aSLionel Sambuc
173*f4a2713aSLionel Sambuc // Undefined friends do not make a mutual friend undefined.
174*f4a2713aSLionel Sambuc class C {
175*f4a2713aSLionel Sambuc int c;
doSomethingElse()176*f4a2713aSLionel Sambuc void doSomethingElse() {}
177*f4a2713aSLionel Sambuc friend class E;
178*f4a2713aSLionel Sambuc friend class D;
179*f4a2713aSLionel Sambuc };
180*f4a2713aSLionel Sambuc class D {
181*f4a2713aSLionel Sambuc int d; // expected-warning{{private field 'd' is not used}}
182*f4a2713aSLionel Sambuc friend class C;
183*f4a2713aSLionel Sambuc };
184*f4a2713aSLionel Sambuc
185*f4a2713aSLionel Sambuc // Undefined nested classes make mutual friends undefined.
186*f4a2713aSLionel Sambuc class F {
187*f4a2713aSLionel Sambuc int f;
188*f4a2713aSLionel Sambuc class G;
189*f4a2713aSLionel Sambuc friend class H;
190*f4a2713aSLionel Sambuc };
191*f4a2713aSLionel Sambuc class H {
192*f4a2713aSLionel Sambuc int h;
193*f4a2713aSLionel Sambuc friend class F;
194*f4a2713aSLionel Sambuc };
195*f4a2713aSLionel Sambuc } // namespace mutual_friends
196*f4a2713aSLionel Sambuc
197*f4a2713aSLionel Sambuc namespace anonymous_structs_unions {
198*f4a2713aSLionel Sambuc class A {
199*f4a2713aSLionel Sambuc private:
200*f4a2713aSLionel Sambuc // FIXME: Look at the DeclContext for anonymous structs/unions.
201*f4a2713aSLionel Sambuc union {
202*f4a2713aSLionel Sambuc int *Aligner;
203*f4a2713aSLionel Sambuc unsigned char Data[8];
204*f4a2713aSLionel Sambuc };
205*f4a2713aSLionel Sambuc };
206*f4a2713aSLionel Sambuc union S {
207*f4a2713aSLionel Sambuc private:
208*f4a2713aSLionel Sambuc int *Aligner;
209*f4a2713aSLionel Sambuc unsigned char Data[8];
210*f4a2713aSLionel Sambuc };
211*f4a2713aSLionel Sambuc } // namespace anonymous_structs_unions
212*f4a2713aSLionel Sambuc
213*f4a2713aSLionel Sambuc namespace pr13413 {
214*f4a2713aSLionel Sambuc class A {
A()215*f4a2713aSLionel Sambuc A() : p_(__null), b_(false), a_(this), p2_(nullptr) {}
216*f4a2713aSLionel Sambuc void* p_; // expected-warning{{private field 'p_' is not used}}
217*f4a2713aSLionel Sambuc bool b_; // expected-warning{{private field 'b_' is not used}}
218*f4a2713aSLionel Sambuc A* a_; // expected-warning{{private field 'a_' is not used}}
219*f4a2713aSLionel Sambuc void* p2_; // expected-warning{{private field 'p2_' is not used}}
220*f4a2713aSLionel Sambuc };
221*f4a2713aSLionel Sambuc }
222*f4a2713aSLionel Sambuc
223*f4a2713aSLionel Sambuc namespace pr13543 {
224*f4a2713aSLionel Sambuc void f(int);
225*f4a2713aSLionel Sambuc void f(char);
226*f4a2713aSLionel Sambuc struct S {
Spr13543::S227*f4a2713aSLionel Sambuc S() : p(&f) {}
228*f4a2713aSLionel Sambuc private:
229*f4a2713aSLionel Sambuc void (*p)(int); // expected-warning{{private field 'p' is not used}}
230*f4a2713aSLionel Sambuc };
231*f4a2713aSLionel Sambuc
232*f4a2713aSLionel Sambuc struct A { int n; };
233*f4a2713aSLionel Sambuc struct B {
Bpr13543::B234*f4a2713aSLionel Sambuc B() : a(A()) {}
Bpr13543::B235*f4a2713aSLionel Sambuc B(char) {}
Bpr13543::B236*f4a2713aSLionel Sambuc B(int n) : a{n}, b{(f(n), 0)} {}
237*f4a2713aSLionel Sambuc private:
238*f4a2713aSLionel Sambuc A a = A(); // expected-warning{{private field 'a' is not used}}
239*f4a2713aSLionel Sambuc A b;
240*f4a2713aSLionel Sambuc };
241*f4a2713aSLionel Sambuc
242*f4a2713aSLionel Sambuc struct X { ~X(); };
243*f4a2713aSLionel Sambuc class C {
244*f4a2713aSLionel Sambuc X x[4]; // no-warning
245*f4a2713aSLionel Sambuc };
246*f4a2713aSLionel Sambuc }
247