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