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