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