class.cpp revision 4e6356426fcfef84e2484820814a8eaaaf547eda
1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -verify %s 207952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidisclass C { 307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidispublic: 407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis auto int errx; // expected-error {{error: storage class specified for a member declaration}} 507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis register int erry; // expected-error {{error: storage class specified for a member declaration}} 607952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis extern int errz; // expected-error {{error: storage class specified for a member declaration}} 707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static void sm() { 907952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis sx = 0; 1007952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis this->x = 0; // expected-error {{error: invalid use of 'this' outside of a nonstatic member function}} 1107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis x = 0; // expected-error {{error: invalid use of member 'x' in static member function}} 1207952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis } 1307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 1407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis class NestedC { 154e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall public: 164e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall NestedC(int); 1707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis void m() { 1807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis sx = 0; 194e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall x = 0; // expected-error {{invalid use of nonstatic data member 'x'}} 2007952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis } 2107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis }; 2207952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 2307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis int b : 1, w : 2; 2407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis int : 1, : 2; 258b963ef99be6235f1e9fe866180fff7dbbe5e85bChris Lattner typedef int E : 1; // expected-error {{typedef member 'E' cannot be a bit-field}} 264e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall static int sb : 1; // expected-error {{static member 'sb' cannot be a bit-field}} 2707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static int vs; 2807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 2907952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis typedef int func(); 3007952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis func tm; 31d6caa9ef4cc68290b0bf33432934cc11dd5594e6Argyrios Kyrtzidis func *ptm; 323cf538d5c49bbebac1afa6f4a5010e3d877440bbDouglas Gregor func btm : 1; // expected-error {{bit-field 'btm' has non-integral type}} 333cf538d5c49bbebac1afa6f4a5010e3d877440bbDouglas Gregor NestedC bc : 1; // expected-error {{bit-field 'bc' has non-integral type}} 3407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 3566973121788ca645fe3d4a66179b9cfb6f2bce08Douglas Gregor enum E1 { en1, en2 }; 3607952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 374e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall int i = 0; // expected-error {{fields can only be initialized in constructors}} 384e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall static int si = 0; // expected-error {{non-const static data member must be initialized out of line}} 394e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall static const NestedC ci = 0; // expected-error {{static data member of type 'const C::NestedC' must be initialized out of line}} 404e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall static const int nci = vs; // expected-error {{in-class initializer is not a constant expression}} 4107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static const int vi = 0; 4207952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static const E evi = 0; 4307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 4407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis void m() { 4507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis sx = 0; 4607952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis this->x = 0; 4707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis y = 0; 4807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis this = 0; // expected-error {{error: expression is not assignable}} 4907952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis } 5007952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 5107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis int f1(int p) { 52d93f0ddba0965ded252e228134b30ce30e863fb0Sebastian Redl A z = 6; 53d93f0ddba0965ded252e228134b30ce30e863fb0Sebastian Redl return p + x + this->y + z; 5407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis } 5507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 5607952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis typedef int A; 5707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 58021c3b372c58f5423b4fa2a5be6933d1c7ecc663Douglas Gregor virtual int viv; // expected-error {{'virtual' can only appear on non-static member functions}} 59d93f0ddba0965ded252e228134b30ce30e863fb0Sebastian Redl virtual static int vsif(); // expected-error {{error: 'virtual' can only appear on non-static member functions}} 60d93f0ddba0965ded252e228134b30ce30e863fb0Sebastian Redl virtual int vif(); 61d93f0ddba0965ded252e228134b30ce30e863fb0Sebastian Redl 6207952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidisprivate: 6307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis int x,y; 6407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static int sx; 65de933f025e839bde4b119f3437c320c2137bbe1fArgyrios Kyrtzidis 66669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl mutable int mi; 67669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl mutable int &mir; // expected-error {{error: 'mutable' cannot be applied to references}} 68669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl mutable void mfn(); // expected-error {{error: 'mutable' cannot be applied to functions}} 69669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl mutable const int mci; // expected-error {{error: 'mutable' and 'const' cannot be mixed}} 70669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl 71de933f025e839bde4b119f3437c320c2137bbe1fArgyrios Kyrtzidis static const int number = 50; 72de933f025e839bde4b119f3437c320c2137bbe1fArgyrios Kyrtzidis static int arr[number]; 7307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis}; 7407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 7507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidisclass C2 { 7607952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis void f() { 7707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static int lx; 7807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis class LC1 { 7907952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis int m() { return lx; } 8007952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis }; 8107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis class LC2 { 8207952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis int m() { return lx; } 8307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis }; 8407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis } 8507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis}; 86669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl 87a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redlstruct C3 { 88a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl int i; 89a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl mutable int j; 90a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl}; 91a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redlvoid f() 92a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl{ 93a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl const C3 c3 = { 1, 2 }; 9458f9e13e87e57236fee4b914eea9be6f92a1c345Chris Lattner (void)static_cast<int*>(&c3.i); // expected-error {{static_cast from 'const int *' to 'int *' is not allowed}} 95a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl // but no error here 96a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl (void)static_cast<int*>(&c3.j); 97a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl} 98a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl 99669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl// Play with mutable a bit more, to make sure it doesn't crash anything. 100669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redlmutable int gi; // expected-error {{error: 'mutable' can only be applied to member variables}} 101669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redlmutable void gfn(); // expected-error {{illegal storage class on function}} 102669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redlvoid ogfn() 103669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl{ 104669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl mutable int ml; // expected-error {{error: 'mutable' can only be applied to member variables}} 105a4ed0d8d75212dc01b4438829a4b0c846d99458dSebastian Redl 106a4ed0d8d75212dc01b4438829a4b0c846d99458dSebastian Redl // PR3020: This used to crash due to double ownership of C4. 107a4ed0d8d75212dc01b4438829a4b0c846d99458dSebastian Redl struct C4; 108cb821d045f5e445384f34d05a526955036073c4aDouglas Gregor C4; // expected-warning {{declaration does not declare anything}} 109669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl} 11072de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor 11172de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregorstruct C4 { 11272de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor void f(); // expected-note{{previous declaration is here}} 11372de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor int f; // expected-error{{duplicate member 'f'}} 11472de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor}; 11546408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redl 11646408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redl// PR5415 - don't hang! 11746408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redlstruct S 11846408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redl{ 119d1a7846699a82f85ff3ce6b2e383409537c3f5c5Sebastian Redl void f(); // expected-note 1 {{previous declaration}} 120d1a7846699a82f85ff3ce6b2e383409537c3f5c5Sebastian Redl void S::f() {} // expected-error {{class member cannot be redeclared}} expected-note {{previous declaration}} expected-note {{previous definition}} 12146408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redl void f() {} // expected-error {{class member cannot be redeclared}} expected-error {{redefinition}} 12246408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redl}; 1234ad287edcc7c019c413484b86feb8457701eaccdJohn McCall 1244ad287edcc7c019c413484b86feb8457701eaccdJohn McCall// Don't crash on this bogus code. 1254ad287edcc7c019c413484b86feb8457701eaccdJohn McCallnamespace pr6629 { 1264ad287edcc7c019c413484b86feb8457701eaccdJohn McCall // TODO: most of these errors are spurious 1274ad287edcc7c019c413484b86feb8457701eaccdJohn McCall template<class T1, class T2> struct foo : 1284ad287edcc7c019c413484b86feb8457701eaccdJohn McCall bogus<foo<T1,T2> > // expected-error {{unknown template name 'bogus'}} \ 1294ad287edcc7c019c413484b86feb8457701eaccdJohn McCall // BOGUS expected-error {{expected '{' after base class list}} \ 1304ad287edcc7c019c413484b86feb8457701eaccdJohn McCall // BOGUS expected-error {{expected ';' after struct}} \ 1314ad287edcc7c019c413484b86feb8457701eaccdJohn McCall // BOGUS expected-error {{expected unqualified-id}} \ 1324ad287edcc7c019c413484b86feb8457701eaccdJohn McCall { }; 1334ad287edcc7c019c413484b86feb8457701eaccdJohn McCall 1344ad287edcc7c019c413484b86feb8457701eaccdJohn McCall template<> struct foo<unknown,unknown> { // why isn't there an error here? 1354ad287edcc7c019c413484b86feb8457701eaccdJohn McCall template <typename U1, typename U2> struct bar { 1364ad287edcc7c019c413484b86feb8457701eaccdJohn McCall typedef bar type; 1374ad287edcc7c019c413484b86feb8457701eaccdJohn McCall static const int value = 0; 1384ad287edcc7c019c413484b86feb8457701eaccdJohn McCall }; 1394ad287edcc7c019c413484b86feb8457701eaccdJohn McCall }; 1404ad287edcc7c019c413484b86feb8457701eaccdJohn McCall} 14133f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor 14233f992425213f381fc503699b26ee8cf9b60494eDouglas Gregornamespace PR7153 { 14333f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor class EnclosingClass { 144293279ae4351f4f17ce44aa4f72861d0bc74c918Douglas Gregor public: 14533f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor struct A { } mutable *member; 14633f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor }; 147293279ae4351f4f17ce44aa4f72861d0bc74c918Douglas Gregor 148293279ae4351f4f17ce44aa4f72861d0bc74c918Douglas Gregor void f(const EnclosingClass &ec) { 149293279ae4351f4f17ce44aa4f72861d0bc74c918Douglas Gregor ec.member = 0; 150293279ae4351f4f17ce44aa4f72861d0bc74c918Douglas Gregor } 15133f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor} 152d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor 153d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregornamespace PR7196 { 154d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor struct A { 155d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor int a; 156d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor 157d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor void f() { 158d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor char i[sizeof(a)]; 159d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor enum { x = sizeof(i) }; 160d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor enum { y = sizeof(a) }; 161d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor } 162d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor }; 163d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor} 164e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor 165e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregornamespace rdar8066414 { 166e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor class C { 167e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor C() {} 168e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor } // expected-error{{expected ';' after class}} 169e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor} 1704e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall 1714e6356426fcfef84e2484820814a8eaaaf547edaJohn McCallnamespace rdar8367341 { 1724e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall float foo(); 1734e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall 1744e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall struct A { 1754e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall static const float x = 5.0f; // expected-warning {{in-class initializer for static data member of type 'const float' is a C++0x extension}} 1764e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall 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}} 1774e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall }; 1784e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall} 179