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