blob: e079d0cc1757e6d2e10b14a48d6814d35dbd790c [file] [log] [blame]
Douglas Gregorc2c11442011-10-25 03:07:45 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wc++11-compat %s
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002class C {
3public:
Richard Trieu2fe9b7f2011-12-15 00:38:15 +00004 auto int errx; // expected-error {{storage class specified for a member declaration}} expected-warning {{'auto' storage class specifier is redundant}}
5 register int erry; // expected-error {{storage class specified for a member declaration}}
6 extern int errz; // expected-error {{storage class specified for a member declaration}}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00007
8 static void sm() {
9 sx = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +000010 this->x = 0; // expected-error {{invalid use of 'this' outside of a nonstatic member function}}
11 x = 0; // expected-error {{invalid use of member 'x' in static member function}}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000012 }
13
14 class NestedC {
John McCall4e635642010-09-10 23:21:22 +000015 public:
16 NestedC(int);
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000017 void m() {
18 sx = 0;
John McCall4e635642010-09-10 23:21:22 +000019 x = 0; // expected-error {{invalid use of nonstatic data member 'x'}}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000020 }
21 };
22
23 int b : 1, w : 2;
24 int : 1, : 2;
Chris Lattner8b963ef2009-03-05 23:01:03 +000025 typedef int E : 1; // expected-error {{typedef member 'E' cannot be a bit-field}}
John McCall4e635642010-09-10 23:21:22 +000026 static int sb : 1; // expected-error {{static member 'sb' cannot be a bit-field}}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000027 static int vs;
28
29 typedef int func();
30 func tm;
Argyrios Kyrtzidisd6caa9e2008-10-15 20:23:22 +000031 func *ptm;
Douglas Gregor3cf538d2009-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 Kyrtzidis07952322008-07-01 10:37:29 +000034
Douglas Gregor66973122009-01-28 17:15:10 +000035 enum E1 { en1, en2 };
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000036
Richard Smithd7c56e12011-12-29 21:57:33 +000037 int i = 0; // expected-warning {{in-class initialization of non-static data member is a C++11 extension}}
John McCall4e635642010-09-10 23:21:22 +000038 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}}
Richard Smithd7c56e12011-12-29 21:57:33 +000040 static const int nci = vs; // expected-error {{in-class initializer for static data member is not a constant expression}}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000041 static const int vi = 0;
Douglas Gregorb3df1382011-10-12 19:26:40 +000042 static const volatile int cvi = 0; // ok, illegal in C++11
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000043 static const E evi = 0;
44
45 void m() {
46 sx = 0;
47 this->x = 0;
48 y = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +000049 this = 0; // expected-error {{expression is not assignable}}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000050 }
51
52 int f1(int p) {
Sebastian Redld93f0dd2008-11-06 15:59:35 +000053 A z = 6;
54 return p + x + this->y + z;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000055 }
56
57 typedef int A;
58
Douglas Gregor021c3b32009-03-11 23:00:04 +000059 virtual int viv; // expected-error {{'virtual' can only appear on non-static member functions}}
Richard Trieu2fe9b7f2011-12-15 00:38:15 +000060 virtual static int vsif(); // expected-error {{'virtual' can only appear on non-static member functions}}
Sebastian Redld93f0dd2008-11-06 15:59:35 +000061 virtual int vif();
62
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000063private:
64 int x,y;
65 static int sx;
Argyrios Kyrtzidisde933f02008-10-08 22:20:31 +000066
Sebastian Redl669d5d72008-11-14 23:42:31 +000067 mutable int mi;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +000068 mutable int &mir; // expected-error {{'mutable' cannot be applied to references}}
69 mutable void mfn(); // expected-error {{'mutable' cannot be applied to functions}}
70 mutable const int mci; // expected-error {{'mutable' and 'const' cannot be mixed}}
Sebastian Redl669d5d72008-11-14 23:42:31 +000071
Argyrios Kyrtzidisde933f02008-10-08 22:20:31 +000072 static const int number = 50;
73 static int arr[number];
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000074};
75
76class C2 {
77 void f() {
78 static int lx;
79 class LC1 {
80 int m() { return lx; }
81 };
82 class LC2 {
83 int m() { return lx; }
84 };
85 }
86};
Sebastian Redl669d5d72008-11-14 23:42:31 +000087
Sebastian Redla11f42f2008-11-17 23:24:37 +000088struct C3 {
89 int i;
90 mutable int j;
91};
92void f()
93{
94 const C3 c3 = { 1, 2 };
Chris Lattner58f9e132010-09-05 00:04:01 +000095 (void)static_cast<int*>(&c3.i); // expected-error {{static_cast from 'const int *' to 'int *' is not allowed}}
Sebastian Redla11f42f2008-11-17 23:24:37 +000096 // but no error here
97 (void)static_cast<int*>(&c3.j);
98}
99
Sebastian Redl669d5d72008-11-14 23:42:31 +0000100// Play with mutable a bit more, to make sure it doesn't crash anything.
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000101mutable int gi; // expected-error {{'mutable' can only be applied to member variables}}
Sebastian Redl669d5d72008-11-14 23:42:31 +0000102mutable void gfn(); // expected-error {{illegal storage class on function}}
103void ogfn()
104{
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000105 mutable int ml; // expected-error {{'mutable' can only be applied to member variables}}
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000106
107 // PR3020: This used to crash due to double ownership of C4.
108 struct C4;
Douglas Gregorcb821d02010-04-08 21:33:23 +0000109 C4; // expected-warning {{declaration does not declare anything}}
Sebastian Redl669d5d72008-11-14 23:42:31 +0000110}
Douglas Gregor72de6672009-01-08 20:45:30 +0000111
112struct C4 {
113 void f(); // expected-note{{previous declaration is here}}
114 int f; // expected-error{{duplicate member 'f'}}
115};
Sebastian Redl46408ee2009-11-24 17:14:34 +0000116
117// PR5415 - don't hang!
118struct S
119{
Sebastian Redld1a78462009-11-24 23:38:44 +0000120 void f(); // expected-note 1 {{previous declaration}}
Francois Pichetc71d8eb2010-10-01 21:19:28 +0000121 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 Redl46408ee2009-11-24 17:14:34 +0000122 void f() {} // expected-error {{class member cannot be redeclared}} expected-error {{redefinition}}
123};
John McCall4ad287e2010-03-17 01:31:25 +0000124
125// Don't crash on this bogus code.
126namespace pr6629 {
127 // TODO: most of these errors are spurious
128 template<class T1, class T2> struct foo :
129 bogus<foo<T1,T2> > // expected-error {{unknown template name 'bogus'}} \
130 // BOGUS expected-error {{expected '{' after base class list}} \
131 // BOGUS expected-error {{expected ';' after struct}} \
132 // BOGUS expected-error {{expected unqualified-id}} \
133 { };
134
135 template<> struct foo<unknown,unknown> { // why isn't there an error here?
136 template <typename U1, typename U2> struct bar {
137 typedef bar type;
138 static const int value = 0;
139 };
140 };
141}
Douglas Gregor33f99242010-05-17 18:19:56 +0000142
143namespace PR7153 {
144 class EnclosingClass {
Douglas Gregor293279a2010-05-17 19:45:25 +0000145 public:
Douglas Gregor33f99242010-05-17 18:19:56 +0000146 struct A { } mutable *member;
147 };
Douglas Gregor293279a2010-05-17 19:45:25 +0000148
149 void f(const EnclosingClass &ec) {
150 ec.member = 0;
151 }
Douglas Gregor33f99242010-05-17 18:19:56 +0000152}
Douglas Gregord9008312010-05-22 16:25:05 +0000153
154namespace PR7196 {
155 struct A {
156 int a;
157
158 void f() {
159 char i[sizeof(a)];
160 enum { x = sizeof(i) };
161 enum { y = sizeof(a) };
162 }
163 };
164}
Douglas Gregore0cc0472010-06-16 23:45:56 +0000165
166namespace rdar8066414 {
167 class C {
168 C() {}
169 } // expected-error{{expected ';' after class}}
170}
John McCall4e635642010-09-10 23:21:22 +0000171
172namespace rdar8367341 {
173 float foo();
174
175 struct A {
Richard Smith947be192011-09-29 23:18:34 +0000176 static const float x = 5.0f; // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}}
Richard Smithd7c56e12011-12-29 21:57:33 +0000177 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}}
John McCall4e635642010-09-10 23:21:22 +0000178 };
179}
Argyrios Kyrtzidis6ad5df12011-01-31 07:04:33 +0000180
181namespace with_anon {
182struct S {
183 union {
184 char c;
185 };
186};
187
188void f() {
189 S::c; // expected-error {{invalid use of nonstatic data member}}
190}
191}
Douglas Gregor147545d2011-10-10 14:49:18 +0000192
193struct PR9989 {
194 static int const PR9989_Member = sizeof PR9989_Member;
195};