Clang Project

clang_source_code/test/SemaCXX/class.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -Wc++11-compat %s 
2class C {
3public:
4  auto int errx; // expected-error {{storage class specified for a member declaration}}
5#if __cplusplus <= 199711L
6  // expected-warning@-2 {{'auto' storage class specifier is redundant}}
7#else
8  // expected-warning@-4 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}}
9#endif
10  register int erry; // expected-error {{storage class specified for a member declaration}}
11  extern int errz; // expected-error {{storage class specified for a member declaration}}
12
13  static void sm() {
14    sx = 0;
15    this->x = 0; // expected-error {{invalid use of 'this' outside of a non-static member function}}
16    x = 0; // expected-error {{invalid use of member 'x' in static member function}}
17  }
18
19  class NestedC {
20  public:
21    NestedC(int);
22    void f() {
23      sx = 0;
24      x = 0; // expected-error {{use of non-static data member 'x' of 'C' from nested type 'NestedC'}}
25      sm();
26      m(); // expected-error {{call to non-static member function 'm' of 'C' from nested type 'NestedC'}}
27    }
28  };
29
30  int b : 1, w : 2;
31  int : 1, : 2;
32  typedef int E : 1; // expected-error {{typedef member 'E' cannot be a bit-field}}
33  static int sb : 1; // expected-error {{static member 'sb' cannot be a bit-field}}
34  static int vs;
35
36  typedef int func();
37  func tm;
38  func *ptm;
39  func btm : 1; // expected-error {{bit-field 'btm' has non-integral type}}
40  NestedC bc : 1; // expected-error {{bit-field 'bc' has non-integral type}}
41
42  enum E1 { en1, en2 };
43
44  int i = 0;
45#if __cplusplus <= 199711L
46  // expected-warning@-2 {{in-class initialization of non-static data member is a C++11 extension}}
47#endif
48  static int si = 0; // expected-error {{non-const static data member must be initialized out of line}}
49  static const NestedC ci = 0; // expected-error {{static data member of type 'const C::NestedC' must be initialized out of line}}
50  static const int nci = vs; // expected-error {{in-class initializer for static data member is not a constant expression}}
51  static const int vi = 0;
52  static const volatile int cvi = 0; // ok, illegal in C++11
53#if __cplusplus >= 201103L
54  // expected-error@-2 {{static const volatile data member must be initialized out of line}}
55#endif
56  static const E evi = 0;
57
58  void m() {
59    sx = 0;
60    this->x = 0;
61    y = 0;
62    this = 0; // expected-error {{expression is not assignable}}
63  }
64
65  int f1(int p) {
66    A z = 6;
67    return p + x + this->y + z;
68  }
69
70  typedef int A;
71
72  virtual int viv; // expected-error {{'virtual' can only appear on non-static member functions}}
73  virtual static int vsif(); // expected-error {{'virtual' can only appear on non-static member functions}}
74  virtual int vif();
75
76private:
77  int x,y;
78  static int sx;
79
80  mutable int mi;
81  mutable int &mir; // expected-error {{'mutable' cannot be applied to references}}
82  mutable void mfn(); // expected-error {{'mutable' cannot be applied to functions}}
83  mutable const int mci; // expected-error {{'mutable' and 'const' cannot be mixed}}
84
85  static const int number = 50;
86  static int arr[number];
87};
88
89class C2 {
90  void f() {
91    static int lx;
92    class LC1 {
93      int m() { return lx; }
94    };
95    class LC2 {
96      int m() { return lx; }
97    };
98  }
99};
100
101struct C3 {
102  int i;
103  mutable int j;
104};
105void f()
106{
107  const C3 c3 = { 1, 2 };
108  (void)static_cast<int*>(&c3.i); // expected-error {{static_cast from 'const int *' to 'int *' is not allowed}}
109  // but no error here
110  (void)static_cast<int*>(&c3.j);
111}
112
113// Play with mutable a bit more, to make sure it doesn't crash anything.
114mutable int gi; // expected-error {{'mutable' can only be applied to member variables}}
115mutable void gfn(); // expected-error {{illegal storage class on function}}
116void ogfn()
117{
118  mutable int ml; // expected-error {{'mutable' can only be applied to member variables}}
119
120  // PR3020: This used to crash due to double ownership of C4.
121  struct C4;
122  C4; // expected-warning {{declaration does not declare anything}}
123}
124
125struct C4 {
126  void f(); // expected-note{{previous declaration is here}}
127  int f; // expected-error{{duplicate member 'f'}}
128};
129
130// PR5415 - don't hang!
131struct S
132{
133  void f(); // expected-note 1 {{previous declaration}} expected-note {{previous declaration}}
134  void S::f() {} // expected-error {{extra qualification on member}} expected-error {{class member cannot be redeclared}}
135  void f() {} // expected-error {{class member cannot be redeclared}}
136};
137
138// Don't crash on this bogus code.
139namespace pr6629 {
140  template<class T1, class T2> struct foo :
141    bogus<foo<T1,T2> > // expected-error {{unknown template name 'bogus'}}
142  { };
143
144  template<> struct foo<unknown,unknown> { // expected-error {{undeclared identifier 'unknown'}}
145    template <typename U1, typename U2> struct bar {
146      typedef bar type;
147      static const int value = 0;
148    };
149  };
150}
151
152namespace PR7153 {
153  class EnclosingClass {
154  public:
155    struct A { } mutable *member;
156  };
157 
158  void f(const EnclosingClass &ec) {
159    ec.member = 0;
160  }
161}
162
163namespace PR7196 {
164  struct A {
165    int a;
166
167    void f() {
168      char i[sizeof(a)];
169      enum { x = sizeof(i) };
170      enum { y = sizeof(a) };
171    }
172  };
173}
174
175namespace rdar8066414 {
176  class C {
177    C() {}
178  } // expected-error{{expected ';' after class}}
179}
180
181namespace rdar8367341 {
182  float foo();
183#if __cplusplus >= 201103L
184  // expected-note@-2 {{declared here}}
185#endif
186
187  struct A {
188#if __cplusplus <= 199711L
189    static const float x = 5.0f; // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}}
190    static const float y = foo(); // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}} expected-error {{in-class initializer for static data member is not a constant expression}}
191#else
192    static constexpr float x = 5.0f;
193    static constexpr float y = foo(); // expected-error {{constexpr variable 'y' must be initialized by a constant expression}} expected-note {{non-constexpr function 'foo' cannot be used in a constant expression}}
194#endif
195  };
196}
197
198namespace with_anon {
199struct S {
200  union {
201    char c;
202  };
203};
204
205void f() {
206    S::c; // expected-error {{invalid use of non-static data member}}
207}
208}
209
210struct PR9989 { 
211  static int const PR9989_Member = sizeof PR9989_Member; 
212};
213