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