blob: 52140cb0743d66a1f53fc4785264db7663c83774 [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002class C {
3public:
4 auto int errx; // expected-error {{error: storage class specified for a member declaration}}
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 {
John McCalldb768922010-09-10 23:21:22 +000015 public:
16 NestedC(int);
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000017 void m() {
18 sx = 0;
John McCalldb768922010-09-10 23:21:22 +000019 x = 0; // expected-error {{invalid use of nonstatic data member 'x'}}
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000020 }
21 };
22
23 int b : 1, w : 2;
24 int : 1, : 2;
Chris Lattnerd26760a2009-03-05 23:01:03 +000025 typedef int E : 1; // expected-error {{typedef member 'E' cannot be a bit-field}}
John McCalldb768922010-09-10 23:21:22 +000026 static int sb : 1; // expected-error {{static member 'sb' cannot be a bit-field}}
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000027 static int vs;
28
29 typedef int func();
30 func tm;
Argyrios Kyrtzidis2e3e7562008-10-15 20:23:22 +000031 func *ptm;
Douglas Gregor1efa4372009-03-11 18:59:21 +000032 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}}
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000034
Douglas Gregorfb034662009-01-28 17:15:10 +000035 enum E1 { en1, en2 };
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000036
John McCalldb768922010-09-10 23:21:22 +000037 int i = 0; // expected-error {{fields can only be initialized in constructors}}
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}}
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000041 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) {
Sebastian Redlb426f6332008-11-06 15:59:35 +000052 A z = 6;
53 return p + x + this->y + z;
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000054 }
55
56 typedef int A;
57
Douglas Gregor0c880302009-03-11 23:00:04 +000058 virtual int viv; // expected-error {{'virtual' can only appear on non-static member functions}}
Sebastian Redlb426f6332008-11-06 15:59:35 +000059 virtual static int vsif(); // expected-error {{error: 'virtual' can only appear on non-static member functions}}
60 virtual int vif();
61
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000062private:
63 int x,y;
64 static int sx;
Argyrios Kyrtzidis1207d312008-10-08 22:20:31 +000065
Sebastian Redlccdfaba2008-11-14 23:42:31 +000066 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
Argyrios Kyrtzidis1207d312008-10-08 22:20:31 +000071 static const int number = 50;
72 static int arr[number];
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000073};
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};
Sebastian Redlccdfaba2008-11-14 23:42:31 +000086
Sebastian Redl8071edb2008-11-17 23:24:37 +000087struct C3 {
88 int i;
89 mutable int j;
90};
91void f()
92{
93 const C3 c3 = { 1, 2 };
Chris Lattner53fa0492010-09-05 00:04:01 +000094 (void)static_cast<int*>(&c3.i); // expected-error {{static_cast from 'const int *' to 'int *' is not allowed}}
Sebastian Redl8071edb2008-11-17 23:24:37 +000095 // but no error here
96 (void)static_cast<int*>(&c3.j);
97}
98
Sebastian Redlccdfaba2008-11-14 23:42:31 +000099// 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}}
Sebastian Redla2b5e312008-12-28 15:28:59 +0000105
106 // PR3020: This used to crash due to double ownership of C4.
107 struct C4;
Douglas Gregorf19ac0e2010-04-08 21:33:23 +0000108 C4; // expected-warning {{declaration does not declare anything}}
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000109}
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000110
111struct C4 {
112 void f(); // expected-note{{previous declaration is here}}
113 int f; // expected-error{{duplicate member 'f'}}
114};
Sebastian Redl03b67ea2009-11-24 17:14:34 +0000115
116// PR5415 - don't hang!
117struct S
118{
Sebastian Redld6f78502009-11-24 23:38:44 +0000119 void f(); // expected-note 1 {{previous declaration}}
Francois Pichet6d76e6c2010-10-01 21:19:28 +0000120 void S::f() {} // expected-warning {{extra qualification on member}} expected-error {{class member cannot be redeclared}} expected-note {{previous declaration}} expected-note {{previous definition}}
Sebastian Redl03b67ea2009-11-24 17:14:34 +0000121 void f() {} // expected-error {{class member cannot be redeclared}} expected-error {{redefinition}}
122};
John McCall9a3da8e62010-03-17 01:31:25 +0000123
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}
Douglas Gregorc9a99c52010-05-17 18:19:56 +0000141
142namespace PR7153 {
143 class EnclosingClass {
Douglas Gregorc4c574b2010-05-17 19:45:25 +0000144 public:
Douglas Gregorc9a99c52010-05-17 18:19:56 +0000145 struct A { } mutable *member;
146 };
Douglas Gregorc4c574b2010-05-17 19:45:25 +0000147
148 void f(const EnclosingClass &ec) {
149 ec.member = 0;
150 }
Douglas Gregorc9a99c52010-05-17 18:19:56 +0000151}
Douglas Gregor0c6f5392010-05-22 16:25:05 +0000152
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}
Douglas Gregor428119e2010-06-16 23:45:56 +0000164
165namespace rdar8066414 {
166 class C {
167 C() {}
168 } // expected-error{{expected ';' after class}}
169}
John McCalldb768922010-09-10 23:21:22 +0000170
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}
Argyrios Kyrtzidisb85cd7c2011-01-31 07:04:33 +0000179
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}