14f4bd835ccc14c54177e9e7ad97bbdd09b161630John McCall// RUN: %clang_cc1 -fsyntax-only -verify %s 27db083f218f6dca7312610367675183343c91dd3John McCall 37db083f218f6dca7312610367675183343c91dd3John McCall// C++0x [class.access.base]p1(a): 47db083f218f6dca7312610367675183343c91dd3John McCall// If a class is declared to be a base class for another class using 57db083f218f6dca7312610367675183343c91dd3John McCall// the public access specifier, the public members of the base class 67db083f218f6dca7312610367675183343c91dd3John McCall// are accessible as public members of the derived class and protected 77db083f218f6dca7312610367675183343c91dd3John McCall// members of the base class are accessible as protected members of 87db083f218f6dca7312610367675183343c91dd3John McCall// the derived class. 97db083f218f6dca7312610367675183343c91dd3John McCallnamespace test0 { 107db083f218f6dca7312610367675183343c91dd3John McCall class Base { 117db083f218f6dca7312610367675183343c91dd3John McCall public: int pub; static int spub; 127db083f218f6dca7312610367675183343c91dd3John McCall protected: int prot; static int sprot; // expected-note 4 {{declared protected here}} 137db083f218f6dca7312610367675183343c91dd3John McCall private: int priv; static int spriv; // expected-note 8 {{declared private here}} 147db083f218f6dca7312610367675183343c91dd3John McCall }; 157db083f218f6dca7312610367675183343c91dd3John McCall 167db083f218f6dca7312610367675183343c91dd3John McCall class Test : public Base { 177db083f218f6dca7312610367675183343c91dd3John McCall void test() { 187db083f218f6dca7312610367675183343c91dd3John McCall pub++; 197db083f218f6dca7312610367675183343c91dd3John McCall spub++; 207db083f218f6dca7312610367675183343c91dd3John McCall prot++; 217db083f218f6dca7312610367675183343c91dd3John McCall sprot++; 227db083f218f6dca7312610367675183343c91dd3John McCall priv++; // expected-error {{private member}} 237db083f218f6dca7312610367675183343c91dd3John McCall spriv++; // expected-error {{private member}} 247db083f218f6dca7312610367675183343c91dd3John McCall 257db083f218f6dca7312610367675183343c91dd3John McCall Base::pub++; 267db083f218f6dca7312610367675183343c91dd3John McCall Base::spub++; 277db083f218f6dca7312610367675183343c91dd3John McCall Base::prot++; 287db083f218f6dca7312610367675183343c91dd3John McCall Base::sprot++; 297db083f218f6dca7312610367675183343c91dd3John McCall Base::priv++; // expected-error {{private member}} 307db083f218f6dca7312610367675183343c91dd3John McCall Base::spriv++; // expected-error {{private member}} 317db083f218f6dca7312610367675183343c91dd3John McCall } 327db083f218f6dca7312610367675183343c91dd3John McCall }; 337db083f218f6dca7312610367675183343c91dd3John McCall 347db083f218f6dca7312610367675183343c91dd3John McCall void test(Test *t) { 357db083f218f6dca7312610367675183343c91dd3John McCall t->pub++; 367db083f218f6dca7312610367675183343c91dd3John McCall t->spub++; 377db083f218f6dca7312610367675183343c91dd3John McCall t->prot++; // expected-error {{protected member}} 387db083f218f6dca7312610367675183343c91dd3John McCall t->sprot++; // expected-error {{protected member}} 397db083f218f6dca7312610367675183343c91dd3John McCall t->priv++; // expected-error {{private member}} 407db083f218f6dca7312610367675183343c91dd3John McCall t->spriv++; // expected-error {{private member}} 417db083f218f6dca7312610367675183343c91dd3John McCall 427db083f218f6dca7312610367675183343c91dd3John McCall t->Base::pub++; 437db083f218f6dca7312610367675183343c91dd3John McCall t->Base::spub++; 447db083f218f6dca7312610367675183343c91dd3John McCall t->Base::prot++; // expected-error {{protected member}} 457db083f218f6dca7312610367675183343c91dd3John McCall t->Base::sprot++; // expected-error {{protected member}} 467db083f218f6dca7312610367675183343c91dd3John McCall t->Base::priv++; // expected-error {{private member}} 477db083f218f6dca7312610367675183343c91dd3John McCall t->Base::spriv++; // expected-error {{private member}} 487db083f218f6dca7312610367675183343c91dd3John McCall } 497db083f218f6dca7312610367675183343c91dd3John McCall} 507db083f218f6dca7312610367675183343c91dd3John McCall 517db083f218f6dca7312610367675183343c91dd3John McCall// C++0x [class.access.base]p1(b): 527db083f218f6dca7312610367675183343c91dd3John McCall// If a class is declared to be a base class for another class using 537db083f218f6dca7312610367675183343c91dd3John McCall// the protected access specifier, the public and protected members 547db083f218f6dca7312610367675183343c91dd3John McCall// of the base class are accessible as protected members of the 557db083f218f6dca7312610367675183343c91dd3John McCall// derived class. 567db083f218f6dca7312610367675183343c91dd3John McCallnamespace test1 { 57fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor class Base { // expected-note 6{{member is declared here}} 58fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor public: 59fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor int pub; // expected-note{{member is declared here}} 60fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor static int spub; // expected-note{{member is declared here}} 617db083f218f6dca7312610367675183343c91dd3John McCall protected: int prot; static int sprot; // expected-note 4 {{declared protected here}} 627db083f218f6dca7312610367675183343c91dd3John McCall private: int priv; static int spriv; // expected-note 8 {{declared private here}} 637db083f218f6dca7312610367675183343c91dd3John McCall }; 647db083f218f6dca7312610367675183343c91dd3John McCall 65c5d12db8396180a6a84e82caac1e11709f29da03John McCall class Test : protected Base { // expected-note 6 {{declared protected here}} expected-note 8 {{constrained by protected inheritance here}} 667db083f218f6dca7312610367675183343c91dd3John McCall void test() { 677db083f218f6dca7312610367675183343c91dd3John McCall pub++; 687db083f218f6dca7312610367675183343c91dd3John McCall spub++; 697db083f218f6dca7312610367675183343c91dd3John McCall prot++; 707db083f218f6dca7312610367675183343c91dd3John McCall sprot++; 717db083f218f6dca7312610367675183343c91dd3John McCall priv++; // expected-error {{private member}} 727db083f218f6dca7312610367675183343c91dd3John McCall spriv++; // expected-error {{private member}} 737db083f218f6dca7312610367675183343c91dd3John McCall 747db083f218f6dca7312610367675183343c91dd3John McCall Base::pub++; 757db083f218f6dca7312610367675183343c91dd3John McCall Base::spub++; 767db083f218f6dca7312610367675183343c91dd3John McCall Base::prot++; 777db083f218f6dca7312610367675183343c91dd3John McCall Base::sprot++; 787db083f218f6dca7312610367675183343c91dd3John McCall Base::priv++; // expected-error {{private member}} 797db083f218f6dca7312610367675183343c91dd3John McCall Base::spriv++; // expected-error {{private member}} 807db083f218f6dca7312610367675183343c91dd3John McCall } 817db083f218f6dca7312610367675183343c91dd3John McCall }; 827db083f218f6dca7312610367675183343c91dd3John McCall 837db083f218f6dca7312610367675183343c91dd3John McCall void test(Test *t) { 84c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->pub++; // expected-error {{protected member}} expected-error {{protected base class}} 857db083f218f6dca7312610367675183343c91dd3John McCall t->spub++; // expected-error {{protected member}} 86c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->prot++; // expected-error {{protected member}} expected-error {{protected base class}} 877db083f218f6dca7312610367675183343c91dd3John McCall t->sprot++; // expected-error {{protected member}} 88c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->priv++; // expected-error {{private member}} expected-error {{protected base class}} 897db083f218f6dca7312610367675183343c91dd3John McCall t->spriv++; // expected-error {{private member}} 907db083f218f6dca7312610367675183343c91dd3John McCall 917db083f218f6dca7312610367675183343c91dd3John McCall // Two possible errors here: one for Base, one for the member 92c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->Base::pub++; // expected-error {{protected member}} expected-error {{protected base class}} 937db083f218f6dca7312610367675183343c91dd3John McCall t->Base::spub++; // expected-error {{protected member}} 94c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->Base::prot++; // expected-error 2 {{protected member}} expected-error {{protected base class}} 957db083f218f6dca7312610367675183343c91dd3John McCall t->Base::sprot++; // expected-error 2 {{protected member}} 96c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->Base::priv++; // expected-error {{protected member}} expected-error {{private member}} expected-error {{protected base class}} 977db083f218f6dca7312610367675183343c91dd3John McCall t->Base::spriv++; // expected-error {{protected member}} expected-error {{private member}} 987db083f218f6dca7312610367675183343c91dd3John McCall } 997db083f218f6dca7312610367675183343c91dd3John McCall} 1007db083f218f6dca7312610367675183343c91dd3John McCall 1017db083f218f6dca7312610367675183343c91dd3John McCall// C++0x [class.access.base]p1(b): 1027db083f218f6dca7312610367675183343c91dd3John McCall// If a class is declared to be a base class for another class using 1037db083f218f6dca7312610367675183343c91dd3John McCall// the private access specifier, the public and protected members of 1047db083f218f6dca7312610367675183343c91dd3John McCall// the base class are accessible as private members of the derived 1057db083f218f6dca7312610367675183343c91dd3John McCall// class. 1067db083f218f6dca7312610367675183343c91dd3John McCallnamespace test2 { 107fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor class Base { // expected-note 6{{member is declared here}} 1087db083f218f6dca7312610367675183343c91dd3John McCall public: 109fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor int pub; // expected-note{{member is declared here}} 110fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor static int spub; // expected-note{{member is declared here}} 1117db083f218f6dca7312610367675183343c91dd3John McCall protected: 112fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor int prot; // expected-note {{declared protected here}} \ 113fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor // expected-note{{member is declared here}} 114fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor static int sprot; // expected-note {{declared protected here}} \ 115fa8dbea559e1eb3df1807fd417032e36c10894edDouglas Gregor // expected-note{{member is declared here}} 1167db083f218f6dca7312610367675183343c91dd3John McCall private: 1177db083f218f6dca7312610367675183343c91dd3John McCall int priv; // expected-note 4 {{declared private here}} 1187db083f218f6dca7312610367675183343c91dd3John McCall static int spriv; // expected-note 4 {{declared private here}} 1197db083f218f6dca7312610367675183343c91dd3John McCall }; 1207db083f218f6dca7312610367675183343c91dd3John McCall 121c5d12db8396180a6a84e82caac1e11709f29da03John McCall class Test : private Base { // expected-note 6 {{declared private here}} \ 122c5d12db8396180a6a84e82caac1e11709f29da03John McCall // expected-note 10 {{constrained by private inheritance here}} 1237db083f218f6dca7312610367675183343c91dd3John McCall void test() { 1247db083f218f6dca7312610367675183343c91dd3John McCall pub++; 1257db083f218f6dca7312610367675183343c91dd3John McCall spub++; 1267db083f218f6dca7312610367675183343c91dd3John McCall prot++; 1277db083f218f6dca7312610367675183343c91dd3John McCall sprot++; 1287db083f218f6dca7312610367675183343c91dd3John McCall priv++; // expected-error {{private member}} 1297db083f218f6dca7312610367675183343c91dd3John McCall spriv++; // expected-error {{private member}} 1307db083f218f6dca7312610367675183343c91dd3John McCall 1317db083f218f6dca7312610367675183343c91dd3John McCall Base::pub++; 1327db083f218f6dca7312610367675183343c91dd3John McCall Base::spub++; 1337db083f218f6dca7312610367675183343c91dd3John McCall Base::prot++; 1347db083f218f6dca7312610367675183343c91dd3John McCall Base::sprot++; 1357db083f218f6dca7312610367675183343c91dd3John McCall Base::priv++; // expected-error {{private member}} 1367db083f218f6dca7312610367675183343c91dd3John McCall Base::spriv++; // expected-error {{private member}} 1377db083f218f6dca7312610367675183343c91dd3John McCall } 1387db083f218f6dca7312610367675183343c91dd3John McCall }; 1397db083f218f6dca7312610367675183343c91dd3John McCall 1407db083f218f6dca7312610367675183343c91dd3John McCall void test(Test *t) { 141c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->pub++; // expected-error {{private member}} expected-error {{private base class}} 1427db083f218f6dca7312610367675183343c91dd3John McCall t->spub++; // expected-error {{private member}} 143c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->prot++; // expected-error {{private member}} expected-error {{private base class}} 1447db083f218f6dca7312610367675183343c91dd3John McCall t->sprot++; // expected-error {{private member}} 145c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->priv++; // expected-error {{private member}} expected-error {{private base class}} 1467db083f218f6dca7312610367675183343c91dd3John McCall t->spriv++; // expected-error {{private member}} 1477db083f218f6dca7312610367675183343c91dd3John McCall 148c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->Base::pub++; // expected-error {{private member}} expected-error {{private base class}} 1497db083f218f6dca7312610367675183343c91dd3John McCall t->Base::spub++; // expected-error {{private member}} 150c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->Base::prot++; // expected-error {{protected member}} expected-error {{private member}} expected-error {{private base class}} 1517db083f218f6dca7312610367675183343c91dd3John McCall t->Base::sprot++; // expected-error {{protected member}} expected-error {{private member}} 152c5d12db8396180a6a84e82caac1e11709f29da03John McCall t->Base::priv++; // expected-error 2 {{private member}} expected-error {{private base class}} 1537db083f218f6dca7312610367675183343c91dd3John McCall t->Base::spriv++; // expected-error 2 {{private member}} 1547db083f218f6dca7312610367675183343c91dd3John McCall } 1557db083f218f6dca7312610367675183343c91dd3John McCall} 156651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 157651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesnamespace PR12788 { 158651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines class A { 159651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines protected: 160651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines struct { 161651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines int x; 162651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines }; 163651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines }; 164651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines class B : A { 165651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void f() { 166651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines ++x; 167651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines A::x++; 168651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 169651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines }; 170651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines} 171