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