blob: ec82925fe4ed72c7e7c821a56035515e661ca410 [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 Smitha85cf392012-04-05 01:13:04 +000010 this->x = 0; // expected-error {{invalid use of 'this' outside of a non-static member function}}
Richard Trieu2fe9b7f2011-12-15 00:38:15 +000011 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);
Richard Smitha85cf392012-04-05 01:13:04 +000017 void f() {
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000018 sx = 0;
Richard Smitha85cf392012-04-05 01:13:04 +000019 x = 0; // expected-error {{use of non-static data member 'x' of 'C' from nested type 'NestedC'}}
20 sm();
21 m(); // expected-error {{call to non-static member function 'm' of 'C' from nested type 'NestedC'}}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000022 }
23 };
24
25 int b : 1, w : 2;
26 int : 1, : 2;
Chris Lattner8b963ef2009-03-05 23:01:03 +000027 typedef int E : 1; // expected-error {{typedef member 'E' cannot be a bit-field}}
John McCall4e635642010-09-10 23:21:22 +000028 static int sb : 1; // expected-error {{static member 'sb' cannot be a bit-field}}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000029 static int vs;
30
31 typedef int func();
32 func tm;
Argyrios Kyrtzidisd6caa9e2008-10-15 20:23:22 +000033 func *ptm;
Douglas Gregor3cf538d2009-03-11 18:59:21 +000034 func btm : 1; // expected-error {{bit-field 'btm' has non-integral type}}
35 NestedC bc : 1; // expected-error {{bit-field 'bc' has non-integral type}}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000036
Douglas Gregor66973122009-01-28 17:15:10 +000037 enum E1 { en1, en2 };
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000038
Richard Smithd7c56e12011-12-29 21:57:33 +000039 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 +000040 static int si = 0; // expected-error {{non-const static data member must be initialized out of line}}
41 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 +000042 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 +000043 static const int vi = 0;
Douglas Gregorb3df1382011-10-12 19:26:40 +000044 static const volatile int cvi = 0; // ok, illegal in C++11
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000045 static const E evi = 0;
46
47 void m() {
48 sx = 0;
49 this->x = 0;
50 y = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +000051 this = 0; // expected-error {{expression is not assignable}}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000052 }
53
54 int f1(int p) {
Sebastian Redld93f0dd2008-11-06 15:59:35 +000055 A z = 6;
56 return p + x + this->y + z;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000057 }
58
59 typedef int A;
60
Douglas Gregor021c3b32009-03-11 23:00:04 +000061 virtual int viv; // expected-error {{'virtual' can only appear on non-static member functions}}
Richard Trieu2fe9b7f2011-12-15 00:38:15 +000062 virtual static int vsif(); // expected-error {{'virtual' can only appear on non-static member functions}}
Sebastian Redld93f0dd2008-11-06 15:59:35 +000063 virtual int vif();
64
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000065private:
66 int x,y;
67 static int sx;
Argyrios Kyrtzidisde933f02008-10-08 22:20:31 +000068
Sebastian Redl669d5d72008-11-14 23:42:31 +000069 mutable int mi;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +000070 mutable int &mir; // expected-error {{'mutable' cannot be applied to references}}
71 mutable void mfn(); // expected-error {{'mutable' cannot be applied to functions}}
72 mutable const int mci; // expected-error {{'mutable' and 'const' cannot be mixed}}
Sebastian Redl669d5d72008-11-14 23:42:31 +000073
Argyrios Kyrtzidisde933f02008-10-08 22:20:31 +000074 static const int number = 50;
75 static int arr[number];
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000076};
77
78class C2 {
79 void f() {
80 static int lx;
81 class LC1 {
82 int m() { return lx; }
83 };
84 class LC2 {
85 int m() { return lx; }
86 };
87 }
88};
Sebastian Redl669d5d72008-11-14 23:42:31 +000089
Sebastian Redla11f42f2008-11-17 23:24:37 +000090struct C3 {
91 int i;
92 mutable int j;
93};
94void f()
95{
96 const C3 c3 = { 1, 2 };
Chris Lattner58f9e132010-09-05 00:04:01 +000097 (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 +000098 // but no error here
99 (void)static_cast<int*>(&c3.j);
100}
101
Sebastian Redl669d5d72008-11-14 23:42:31 +0000102// Play with mutable a bit more, to make sure it doesn't crash anything.
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000103mutable int gi; // expected-error {{'mutable' can only be applied to member variables}}
Sebastian Redl669d5d72008-11-14 23:42:31 +0000104mutable void gfn(); // expected-error {{illegal storage class on function}}
105void ogfn()
106{
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000107 mutable int ml; // expected-error {{'mutable' can only be applied to member variables}}
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000108
109 // PR3020: This used to crash due to double ownership of C4.
110 struct C4;
Douglas Gregorcb821d02010-04-08 21:33:23 +0000111 C4; // expected-warning {{declaration does not declare anything}}
Sebastian Redl669d5d72008-11-14 23:42:31 +0000112}
Douglas Gregor72de6672009-01-08 20:45:30 +0000113
114struct C4 {
115 void f(); // expected-note{{previous declaration is here}}
116 int f; // expected-error{{duplicate member 'f'}}
117};
Sebastian Redl46408ee2009-11-24 17:14:34 +0000118
119// PR5415 - don't hang!
120struct S
121{
Sebastian Redld1a78462009-11-24 23:38:44 +0000122 void f(); // expected-note 1 {{previous declaration}}
Francois Pichetc71d8eb2010-10-01 21:19:28 +0000123 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 +0000124 void f() {} // expected-error {{class member cannot be redeclared}} expected-error {{redefinition}}
125};
John McCall4ad287e2010-03-17 01:31:25 +0000126
127// Don't crash on this bogus code.
128namespace pr6629 {
129 // TODO: most of these errors are spurious
130 template<class T1, class T2> struct foo :
131 bogus<foo<T1,T2> > // expected-error {{unknown template name 'bogus'}} \
132 // BOGUS expected-error {{expected '{' after base class list}} \
133 // BOGUS expected-error {{expected ';' after struct}} \
134 // BOGUS expected-error {{expected unqualified-id}} \
135 { };
136
137 template<> struct foo<unknown,unknown> { // why isn't there an error here?
138 template <typename U1, typename U2> struct bar {
139 typedef bar type;
140 static const int value = 0;
141 };
142 };
143}
Douglas Gregor33f99242010-05-17 18:19:56 +0000144
145namespace PR7153 {
146 class EnclosingClass {
Douglas Gregor293279a2010-05-17 19:45:25 +0000147 public:
Douglas Gregor33f99242010-05-17 18:19:56 +0000148 struct A { } mutable *member;
149 };
Douglas Gregor293279a2010-05-17 19:45:25 +0000150
151 void f(const EnclosingClass &ec) {
152 ec.member = 0;
153 }
Douglas Gregor33f99242010-05-17 18:19:56 +0000154}
Douglas Gregord9008312010-05-22 16:25:05 +0000155
156namespace PR7196 {
157 struct A {
158 int a;
159
160 void f() {
161 char i[sizeof(a)];
162 enum { x = sizeof(i) };
163 enum { y = sizeof(a) };
164 }
165 };
166}
Douglas Gregore0cc0472010-06-16 23:45:56 +0000167
168namespace rdar8066414 {
169 class C {
170 C() {}
171 } // expected-error{{expected ';' after class}}
172}
John McCall4e635642010-09-10 23:21:22 +0000173
174namespace rdar8367341 {
175 float foo();
176
177 struct A {
Richard Smith947be192011-09-29 23:18:34 +0000178 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 +0000179 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 +0000180 };
181}
Argyrios Kyrtzidis6ad5df12011-01-31 07:04:33 +0000182
183namespace with_anon {
184struct S {
185 union {
186 char c;
187 };
188};
189
190void f() {
Richard Smitha85cf392012-04-05 01:13:04 +0000191 S::c; // expected-error {{invalid use of non-static data member}}
Argyrios Kyrtzidis6ad5df12011-01-31 07:04:33 +0000192}
193}
Douglas Gregor147545d2011-10-10 14:49:18 +0000194
195struct PR9989 {
196 static int const PR9989_Member = sizeof PR9989_Member;
197};