class.cpp revision d7c56e1114bfe7d461786903bb720d2c6efc05a1
1c2c11446caaee2d5a787cc8d0310330c6364210dDouglas Gregor// RUN: %clang_cc1 -fsyntax-only -verify -Wc++11-compat %s 207952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidisclass C { 307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidispublic: 42fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu auto int errx; // expected-error {{storage class specified for a member declaration}} expected-warning {{'auto' storage class specifier is redundant}} 52fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu register int erry; // expected-error {{storage class specified for a member declaration}} 62fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu extern int errz; // expected-error {{storage class specified for a member declaration}} 707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static void sm() { 907952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis sx = 0; 102fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu this->x = 0; // expected-error {{invalid use of 'this' outside of a nonstatic member function}} 112fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu x = 0; // expected-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 37d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith int i = 0; // expected-warning {{in-class initialization of non-static data member is a C++11 extension}} 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}} 40d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith static const int nci = vs; // expected-error {{in-class initializer for static data member is not a constant expression}} 4107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static const int vi = 0; 42b3df1386680b3830d2f4d300d4d7eaba134135fcDouglas Gregor static const volatile int cvi = 0; // ok, illegal in C++11 4307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static const E evi = 0; 4407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 4507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis void m() { 4607952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis sx = 0; 4707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis this->x = 0; 4807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis y = 0; 492fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu this = 0; // expected-error {{expression is not assignable}} 5007952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis } 5107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 5207952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis int f1(int p) { 53d93f0ddba0965ded252e228134b30ce30e863fb0Sebastian Redl A z = 6; 54d93f0ddba0965ded252e228134b30ce30e863fb0Sebastian Redl return p + x + this->y + z; 5507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis } 5607952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 5707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis typedef int A; 5807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 59021c3b372c58f5423b4fa2a5be6933d1c7ecc663Douglas Gregor virtual int viv; // expected-error {{'virtual' can only appear on non-static member functions}} 602fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu virtual static int vsif(); // expected-error {{'virtual' can only appear on non-static member functions}} 61d93f0ddba0965ded252e228134b30ce30e863fb0Sebastian Redl virtual int vif(); 62d93f0ddba0965ded252e228134b30ce30e863fb0Sebastian Redl 6307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidisprivate: 6407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis int x,y; 6507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static int sx; 66de933f025e839bde4b119f3437c320c2137bbe1fArgyrios Kyrtzidis 67669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl mutable int mi; 682fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu mutable int &mir; // expected-error {{'mutable' cannot be applied to references}} 692fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu mutable void mfn(); // expected-error {{'mutable' cannot be applied to functions}} 702fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu mutable const int mci; // expected-error {{'mutable' and 'const' cannot be mixed}} 71669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl 72de933f025e839bde4b119f3437c320c2137bbe1fArgyrios Kyrtzidis static const int number = 50; 73de933f025e839bde4b119f3437c320c2137bbe1fArgyrios Kyrtzidis static int arr[number]; 7407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis}; 7507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis 7607952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidisclass C2 { 7707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis void f() { 7807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis static int lx; 7907952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis class LC1 { 8007952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis int m() { return lx; } 8107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis }; 8207952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis class LC2 { 8307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis int m() { return lx; } 8407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis }; 8507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis } 8607952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis}; 87669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl 88a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redlstruct C3 { 89a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl int i; 90a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl mutable int j; 91a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl}; 92a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redlvoid f() 93a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl{ 94a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl const C3 c3 = { 1, 2 }; 9558f9e13e87e57236fee4b914eea9be6f92a1c345Chris Lattner (void)static_cast<int*>(&c3.i); // expected-error {{static_cast from 'const int *' to 'int *' is not allowed}} 96a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl // but no error here 97a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl (void)static_cast<int*>(&c3.j); 98a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl} 99a11f42f4bca694b9be91350d0a74815f119e3fbfSebastian Redl 100669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl// Play with mutable a bit more, to make sure it doesn't crash anything. 1012fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieumutable int gi; // expected-error {{'mutable' can only be applied to member variables}} 102669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redlmutable void gfn(); // expected-error {{illegal storage class on function}} 103669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redlvoid ogfn() 104669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl{ 1052fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu mutable int ml; // expected-error {{'mutable' can only be applied to member variables}} 106a4ed0d8d75212dc01b4438829a4b0c846d99458dSebastian Redl 107a4ed0d8d75212dc01b4438829a4b0c846d99458dSebastian Redl // PR3020: This used to crash due to double ownership of C4. 108a4ed0d8d75212dc01b4438829a4b0c846d99458dSebastian Redl struct C4; 109cb821d045f5e445384f34d05a526955036073c4aDouglas Gregor C4; // expected-warning {{declaration does not declare anything}} 110669d5d74b880a8497b92a3ec159145713f4d6519Sebastian Redl} 11172de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor 11272de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregorstruct C4 { 11372de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor void f(); // expected-note{{previous declaration is here}} 11472de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor int f; // expected-error{{duplicate member 'f'}} 11572de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor}; 11646408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redl 11746408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redl// PR5415 - don't hang! 11846408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redlstruct S 11946408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redl{ 120d1a7846699a82f85ff3ce6b2e383409537c3f5c5Sebastian Redl void f(); // expected-note 1 {{previous declaration}} 121c71d8eb6592ae3ef498fc57db3563d1dfae48dffFrancois Pichet void S::f() {} // expected-warning {{extra qualification on member}} expected-error {{class member cannot be redeclared}} expected-note {{previous declaration}} expected-note {{previous definition}} 12246408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redl void f() {} // expected-error {{class member cannot be redeclared}} expected-error {{redefinition}} 12346408eedfff5aa33662cedb6716a20616f3bad31Sebastian Redl}; 1244ad287edcc7c019c413484b86feb8457701eaccdJohn McCall 1254ad287edcc7c019c413484b86feb8457701eaccdJohn McCall// Don't crash on this bogus code. 1264ad287edcc7c019c413484b86feb8457701eaccdJohn McCallnamespace pr6629 { 1274ad287edcc7c019c413484b86feb8457701eaccdJohn McCall // TODO: most of these errors are spurious 1284ad287edcc7c019c413484b86feb8457701eaccdJohn McCall template<class T1, class T2> struct foo : 1294ad287edcc7c019c413484b86feb8457701eaccdJohn McCall bogus<foo<T1,T2> > // expected-error {{unknown template name 'bogus'}} \ 1304ad287edcc7c019c413484b86feb8457701eaccdJohn McCall // BOGUS expected-error {{expected '{' after base class list}} \ 1314ad287edcc7c019c413484b86feb8457701eaccdJohn McCall // BOGUS expected-error {{expected ';' after struct}} \ 1324ad287edcc7c019c413484b86feb8457701eaccdJohn McCall // BOGUS expected-error {{expected unqualified-id}} \ 1334ad287edcc7c019c413484b86feb8457701eaccdJohn McCall { }; 1344ad287edcc7c019c413484b86feb8457701eaccdJohn McCall 1354ad287edcc7c019c413484b86feb8457701eaccdJohn McCall template<> struct foo<unknown,unknown> { // why isn't there an error here? 1364ad287edcc7c019c413484b86feb8457701eaccdJohn McCall template <typename U1, typename U2> struct bar { 1374ad287edcc7c019c413484b86feb8457701eaccdJohn McCall typedef bar type; 1384ad287edcc7c019c413484b86feb8457701eaccdJohn McCall static const int value = 0; 1394ad287edcc7c019c413484b86feb8457701eaccdJohn McCall }; 1404ad287edcc7c019c413484b86feb8457701eaccdJohn McCall }; 1414ad287edcc7c019c413484b86feb8457701eaccdJohn McCall} 14233f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor 14333f992425213f381fc503699b26ee8cf9b60494eDouglas Gregornamespace PR7153 { 14433f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor class EnclosingClass { 145293279ae4351f4f17ce44aa4f72861d0bc74c918Douglas Gregor public: 14633f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor struct A { } mutable *member; 14733f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor }; 148293279ae4351f4f17ce44aa4f72861d0bc74c918Douglas Gregor 149293279ae4351f4f17ce44aa4f72861d0bc74c918Douglas Gregor void f(const EnclosingClass &ec) { 150293279ae4351f4f17ce44aa4f72861d0bc74c918Douglas Gregor ec.member = 0; 151293279ae4351f4f17ce44aa4f72861d0bc74c918Douglas Gregor } 15233f992425213f381fc503699b26ee8cf9b60494eDouglas Gregor} 153d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor 154d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregornamespace PR7196 { 155d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor struct A { 156d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor int a; 157d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor 158d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor void f() { 159d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor char i[sizeof(a)]; 160d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor enum { x = sizeof(i) }; 161d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor enum { y = sizeof(a) }; 162d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor } 163d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor }; 164d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor} 165e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor 166e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregornamespace rdar8066414 { 167e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor class C { 168e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor C() {} 169e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor } // expected-error{{expected ';' after class}} 170e0cc047b1984fc301bbe6e98b6d197bed39ad562Douglas Gregor} 1714e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall 1724e6356426fcfef84e2484820814a8eaaaf547edaJohn McCallnamespace rdar8367341 { 1734e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall float foo(); 1744e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall 1754e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall struct A { 176947be1941e9a1d4233116f51a45799d3904d4231Richard Smith static const float x = 5.0f; // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}} 177d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith 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}} 1784e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall }; 1794e6356426fcfef84e2484820814a8eaaaf547edaJohn McCall} 1806ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidis 1816ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidisnamespace with_anon { 1826ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidisstruct S { 1836ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidis union { 1846ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidis char c; 1856ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidis }; 1866ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidis}; 1876ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidis 1886ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidisvoid f() { 1896ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidis S::c; // expected-error {{invalid use of nonstatic data member}} 1906ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidis} 1916ad5df132a0bcb3f6975362901270be5bf60dc56Argyrios Kyrtzidis} 192147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor 193147545d698972cfd34ece30a5d55e8180784161eDouglas Gregorstruct PR9989 { 194147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor static int const PR9989_Member = sizeof PR9989_Member; 195147545d698972cfd34ece30a5d55e8180784161eDouglas Gregor}; 196