Douglas Gregor | c2c1144 | 2011-10-25 03:07:45 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -Wc++11-compat %s |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2 | class C { |
| 3 | public: |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 4 | 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 Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 7 | |
| 8 | static void sm() { |
| 9 | sx = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 10 | 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 Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 12 | } |
| 13 | |
| 14 | class NestedC { |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 15 | public: |
| 16 | NestedC(int); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 17 | void m() { |
| 18 | sx = 0; |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 19 | x = 0; // expected-error {{invalid use of nonstatic data member 'x'}} |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 20 | } |
| 21 | }; |
| 22 | |
| 23 | int b : 1, w : 2; |
| 24 | int : 1, : 2; |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 25 | typedef int E : 1; // expected-error {{typedef member 'E' cannot be a bit-field}} |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 26 | static int sb : 1; // expected-error {{static member 'sb' cannot be a bit-field}} |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 27 | static int vs; |
| 28 | |
| 29 | typedef int func(); |
| 30 | func tm; |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 31 | func *ptm; |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 32 | 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 Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 34 | |
Douglas Gregor | 6697312 | 2009-01-28 17:15:10 +0000 | [diff] [blame] | 35 | enum E1 { en1, en2 }; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 36 | |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 37 | int i = 0; // expected-warning {{in-class initialization of non-static data member is a C++11 extension}} |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 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}} |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 40 | static const int nci = vs; // expected-error {{in-class initializer for static data member is not a constant expression}} |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 41 | static const int vi = 0; |
Douglas Gregor | b3df138 | 2011-10-12 19:26:40 +0000 | [diff] [blame] | 42 | static const volatile int cvi = 0; // ok, illegal in C++11 |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 43 | static const E evi = 0; |
| 44 | |
| 45 | void m() { |
| 46 | sx = 0; |
| 47 | this->x = 0; |
| 48 | y = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 49 | this = 0; // expected-error {{expression is not assignable}} |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | int f1(int p) { |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 53 | A z = 6; |
| 54 | return p + x + this->y + z; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | typedef int A; |
| 58 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 59 | virtual int viv; // expected-error {{'virtual' can only appear on non-static member functions}} |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 60 | virtual static int vsif(); // expected-error {{'virtual' can only appear on non-static member functions}} |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 61 | virtual int vif(); |
| 62 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 63 | private: |
| 64 | int x,y; |
| 65 | static int sx; |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 66 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 67 | mutable int mi; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 68 | 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 Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 71 | |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 72 | static const int number = 50; |
| 73 | static int arr[number]; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | class 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 Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 87 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 88 | struct C3 { |
| 89 | int i; |
| 90 | mutable int j; |
| 91 | }; |
| 92 | void f() |
| 93 | { |
| 94 | const C3 c3 = { 1, 2 }; |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 95 | (void)static_cast<int*>(&c3.i); // expected-error {{static_cast from 'const int *' to 'int *' is not allowed}} |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 96 | // but no error here |
| 97 | (void)static_cast<int*>(&c3.j); |
| 98 | } |
| 99 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 100 | // Play with mutable a bit more, to make sure it doesn't crash anything. |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 101 | mutable int gi; // expected-error {{'mutable' can only be applied to member variables}} |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 102 | mutable void gfn(); // expected-error {{illegal storage class on function}} |
| 103 | void ogfn() |
| 104 | { |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 105 | mutable int ml; // expected-error {{'mutable' can only be applied to member variables}} |
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 106 | |
| 107 | // PR3020: This used to crash due to double ownership of C4. |
| 108 | struct C4; |
Douglas Gregor | cb821d0 | 2010-04-08 21:33:23 +0000 | [diff] [blame] | 109 | C4; // expected-warning {{declaration does not declare anything}} |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 110 | } |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 111 | |
| 112 | struct C4 { |
| 113 | void f(); // expected-note{{previous declaration is here}} |
| 114 | int f; // expected-error{{duplicate member 'f'}} |
| 115 | }; |
Sebastian Redl | 46408ee | 2009-11-24 17:14:34 +0000 | [diff] [blame] | 116 | |
| 117 | // PR5415 - don't hang! |
| 118 | struct S |
| 119 | { |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 120 | void f(); // expected-note 1 {{previous declaration}} |
Francois Pichet | c71d8eb | 2010-10-01 21:19:28 +0000 | [diff] [blame] | 121 | 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 Redl | 46408ee | 2009-11-24 17:14:34 +0000 | [diff] [blame] | 122 | void f() {} // expected-error {{class member cannot be redeclared}} expected-error {{redefinition}} |
| 123 | }; |
John McCall | 4ad287e | 2010-03-17 01:31:25 +0000 | [diff] [blame] | 124 | |
| 125 | // Don't crash on this bogus code. |
| 126 | namespace 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 Gregor | 33f9924 | 2010-05-17 18:19:56 +0000 | [diff] [blame] | 142 | |
| 143 | namespace PR7153 { |
| 144 | class EnclosingClass { |
Douglas Gregor | 293279a | 2010-05-17 19:45:25 +0000 | [diff] [blame] | 145 | public: |
Douglas Gregor | 33f9924 | 2010-05-17 18:19:56 +0000 | [diff] [blame] | 146 | struct A { } mutable *member; |
| 147 | }; |
Douglas Gregor | 293279a | 2010-05-17 19:45:25 +0000 | [diff] [blame] | 148 | |
| 149 | void f(const EnclosingClass &ec) { |
| 150 | ec.member = 0; |
| 151 | } |
Douglas Gregor | 33f9924 | 2010-05-17 18:19:56 +0000 | [diff] [blame] | 152 | } |
Douglas Gregor | d900831 | 2010-05-22 16:25:05 +0000 | [diff] [blame] | 153 | |
| 154 | namespace 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 Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 165 | |
| 166 | namespace rdar8066414 { |
| 167 | class C { |
| 168 | C() {} |
| 169 | } // expected-error{{expected ';' after class}} |
| 170 | } |
John McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 171 | |
| 172 | namespace rdar8367341 { |
| 173 | float foo(); |
| 174 | |
| 175 | struct A { |
Richard Smith | 947be19 | 2011-09-29 23:18:34 +0000 | [diff] [blame] | 176 | static const float x = 5.0f; // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}} |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 177 | 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 McCall | 4e63564 | 2010-09-10 23:21:22 +0000 | [diff] [blame] | 178 | }; |
| 179 | } |
Argyrios Kyrtzidis | 6ad5df1 | 2011-01-31 07:04:33 +0000 | [diff] [blame] | 180 | |
| 181 | namespace with_anon { |
| 182 | struct S { |
| 183 | union { |
| 184 | char c; |
| 185 | }; |
| 186 | }; |
| 187 | |
| 188 | void f() { |
| 189 | S::c; // expected-error {{invalid use of nonstatic data member}} |
| 190 | } |
| 191 | } |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 192 | |
| 193 | struct PR9989 { |
| 194 | static int const PR9989_Member = sizeof PR9989_Member; |
| 195 | }; |