Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -Wabstract-vbase-init |
Anders Carlsson | 7cbd8fb | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 2 | |
| 3 | #ifndef __GXX_EXPERIMENTAL_CXX0X__ |
| 4 | #define __CONCAT(__X, __Y) __CONCAT1(__X, __Y) |
| 5 | #define __CONCAT1(__X, __Y) __X ## __Y |
| 6 | |
| 7 | #define static_assert(__b, __m) \ |
| 8 | typedef int __CONCAT(__sa, __LINE__)[__b ? 1 : -1] |
| 9 | #endif |
| 10 | |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 11 | union IncompleteUnion; |
| 12 | |
| 13 | static_assert(!__is_abstract(IncompleteUnion), "unions are never abstract"); |
| 14 | |
Anders Carlsson | 7cbd8fb | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 15 | class C { |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 16 | virtual void f() = 0; // expected-note {{unimplemented pure virtual method 'f'}} |
Anders Carlsson | 7cbd8fb | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | static_assert(__is_abstract(C), "C has a pure virtual function"); |
| 20 | |
| 21 | class D : C { |
| 22 | }; |
| 23 | |
| 24 | static_assert(__is_abstract(D), "D inherits from an abstract class"); |
| 25 | |
| 26 | class E : D { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 27 | virtual void f(); |
Anders Carlsson | 7cbd8fb | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | static_assert(!__is_abstract(E), "E inherits from an abstract class but implements f"); |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 31 | |
Chandler Carruth | fc1ad1f | 2011-02-19 00:12:23 +0000 | [diff] [blame] | 32 | C *d = new C; // expected-error {{allocating an object of abstract class type 'C'}} |
Anders Carlsson | 0d5ca29 | 2009-03-23 17:49:10 +0000 | [diff] [blame] | 33 | |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 34 | C c; // expected-error {{variable type 'C' is an abstract class}} |
| 35 | void t1(C c); // expected-error {{parameter type 'C' is an abstract class}} |
| 36 | void t2(C); // expected-error {{parameter type 'C' is an abstract class}} |
| 37 | |
| 38 | struct S { |
| 39 | C c; // expected-error {{field type 'C' is an abstract class}} |
| 40 | }; |
Anders Carlsson | 0d5ca29 | 2009-03-23 17:49:10 +0000 | [diff] [blame] | 41 | |
Anders Carlsson | eb0c532 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 42 | void t3(const C&); |
| 43 | |
| 44 | void f() { |
Chandler Carruth | fc1ad1f | 2011-02-19 00:12:23 +0000 | [diff] [blame] | 45 | C(); // expected-error {{allocating an object of abstract class type 'C'}} |
| 46 | t3(C()); // expected-error {{allocating an object of abstract class type 'C'}} |
Anders Carlsson | eb0c532 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Douglas Gregor | 7d8072e | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 49 | C e1[2]; // expected-error {{array of abstract class type 'C'}} |
| 50 | C (*e2)[2]; // expected-error {{array of abstract class type 'C'}} |
| 51 | C (**e3)[2]; // expected-error {{array of abstract class type 'C'}} |
Anders Carlsson | eb0c532 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | 7d8072e | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 53 | void t4(C c[2]); // expected-error {{array of abstract class type 'C'}} |
Anders Carlsson | eb0c532 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 54 | |
| 55 | void t5(void (*)(C)); // expected-error {{parameter type 'C' is an abstract class}} |
| 56 | |
| 57 | typedef void (*Func)(C); // expected-error {{parameter type 'C' is an abstract class}} |
| 58 | void t6(Func); |
| 59 | |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 60 | class F { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | F a() { while (1) {} } // expected-error {{return type 'F' is an abstract class}} |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 62 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | class D { |
| 64 | void f(F c); // expected-error {{parameter type 'F' is an abstract class}} |
| 65 | }; |
Anders Carlsson | eb0c532 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 66 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | union U { |
| 68 | void u(F c); // expected-error {{parameter type 'F' is an abstract class}} |
| 69 | }; |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 70 | |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 71 | virtual void f() = 0; // expected-note {{unimplemented pure virtual method 'f'}} |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 72 | }; |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 73 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 74 | // Diagnosing in these cases is prohibitively expensive. We still |
| 75 | // diagnose at the function definition, of course. |
| 76 | |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 77 | class Abstract; |
| 78 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 79 | void t7(Abstract a); |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 80 | |
| 81 | void t8() { |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 82 | void h(Abstract a); |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | namespace N { |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 86 | void h(Abstract a); |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | class Abstract { |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 90 | virtual void f() = 0; |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 91 | }; |
Anders Carlsson | 3c01271 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 92 | |
| 93 | // <rdar://problem/6854087> |
| 94 | class foo { |
| 95 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | virtual foo *getFoo() = 0; |
Anders Carlsson | 3c01271 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | class bar : public foo { |
| 100 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | virtual bar *getFoo(); |
Anders Carlsson | 3c01271 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | bar x; |
Anders Carlsson | 65d5820 | 2009-05-30 00:52:53 +0000 | [diff] [blame] | 105 | |
| 106 | // <rdar://problem/6902298> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | class A { |
Anders Carlsson | 65d5820 | 2009-05-30 00:52:53 +0000 | [diff] [blame] | 108 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | virtual void release() = 0; |
| 110 | virtual void release(int count) = 0; |
| 111 | virtual void retain() = 0; |
Anders Carlsson | 65d5820 | 2009-05-30 00:52:53 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | class B : public A { |
Anders Carlsson | 65d5820 | 2009-05-30 00:52:53 +0000 | [diff] [blame] | 115 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | virtual void release(); |
| 117 | virtual void release(int count); |
| 118 | virtual void retain(); |
Anders Carlsson | 65d5820 | 2009-05-30 00:52:53 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | void foo(void) { |
| 122 | B b; |
Anders Carlsson | 65d5820 | 2009-05-30 00:52:53 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Anders Carlsson | 36b75a4 | 2009-05-30 17:26:39 +0000 | [diff] [blame] | 125 | struct K { |
| 126 | int f; |
| 127 | virtual ~K(); |
| 128 | }; |
| 129 | |
| 130 | struct L : public K { |
| 131 | void f(); |
| 132 | }; |
Anders Carlsson | 70017943 | 2009-10-18 19:34:08 +0000 | [diff] [blame] | 133 | |
| 134 | // PR5222 |
| 135 | namespace PR5222 { |
| 136 | struct A { |
| 137 | virtual A *clone() = 0; |
| 138 | }; |
| 139 | struct B : public A { |
| 140 | virtual B *clone() = 0; |
| 141 | }; |
| 142 | struct C : public B { |
| 143 | virtual C *clone(); |
| 144 | }; |
| 145 | |
| 146 | C c; |
| 147 | } |
Sebastian Redl | d5b2453 | 2009-11-18 21:51:29 +0000 | [diff] [blame] | 148 | |
| 149 | // PR5550 - instantiating template didn't track overridden methods |
| 150 | namespace PR5550 { |
| 151 | struct A { |
| 152 | virtual void a() = 0; |
| 153 | virtual void b() = 0; |
| 154 | }; |
| 155 | template<typename T> struct B : public A { |
| 156 | virtual void b(); |
| 157 | virtual void c() = 0; |
| 158 | }; |
| 159 | struct C : public B<int> { |
| 160 | virtual void a(); |
| 161 | virtual void c(); |
| 162 | }; |
| 163 | C x; |
| 164 | } |
Eli Friedman | 5dd02a0f | 2009-12-16 20:00:27 +0000 | [diff] [blame] | 165 | |
| 166 | namespace PureImplicit { |
| 167 | // A pure virtual destructor should be implicitly overridden. |
| 168 | struct A { virtual ~A() = 0; }; |
| 169 | struct B : A {}; |
| 170 | B x; |
| 171 | |
| 172 | // A pure virtual assignment operator should be implicitly overridden. |
| 173 | struct D; |
| 174 | struct C { virtual D& operator=(const D&) = 0; }; |
| 175 | struct D : C {}; |
| 176 | D y; |
| 177 | } |
John McCall | 38e5f43 | 2010-06-16 09:33:39 +0000 | [diff] [blame] | 178 | |
| 179 | namespace test1 { |
| 180 | struct A { |
| 181 | virtual void foo() = 0; |
| 182 | }; |
| 183 | |
| 184 | struct B : A { |
| 185 | using A::foo; |
| 186 | }; |
| 187 | |
| 188 | struct C : B { |
| 189 | void foo(); |
| 190 | }; |
| 191 | |
| 192 | void test() { |
| 193 | C c; |
| 194 | } |
| 195 | } |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 196 | |
| 197 | // rdar://problem/8302168 |
| 198 | namespace test2 { |
| 199 | struct X1 { |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 200 | virtual void xfunc(void) = 0; // expected-note {{unimplemented pure virtual method}} |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 201 | void g(X1 parm7); // expected-error {{parameter type 'test2::X1' is an abstract class}} |
| 202 | void g(X1 parm8[2]); // expected-error {{array of abstract class type 'test2::X1'}} |
| 203 | }; |
| 204 | |
| 205 | template <int N> |
| 206 | struct X2 { |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 207 | virtual void xfunc(void) = 0; // expected-note {{unimplemented pure virtual method}} |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 208 | void g(X2 parm10); // expected-error {{parameter type 'X2<N>' is an abstract class}} |
| 209 | void g(X2 parm11[2]); // expected-error {{array of abstract class type 'X2<N>'}} |
| 210 | }; |
| 211 | } |
John McCall | 8f42893 | 2010-08-18 09:58:15 +0000 | [diff] [blame] | 212 | |
| 213 | namespace test3 { |
| 214 | struct A { // expected-note {{not complete until}} |
| 215 | A x; // expected-error {{field has incomplete type}} |
| 216 | virtual void abstract() = 0; |
| 217 | }; |
| 218 | |
| 219 | struct B { // expected-note {{not complete until}} |
| 220 | virtual void abstract() = 0; |
| 221 | B x; // expected-error {{field has incomplete type}} |
| 222 | }; |
| 223 | |
| 224 | struct C { |
| 225 | static C x; // expected-error {{abstract class}} |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 226 | virtual void abstract() = 0; // expected-note {{unimplemented pure virtual method}} |
John McCall | 8f42893 | 2010-08-18 09:58:15 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | struct D { |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 230 | virtual void abstract() = 0; // expected-note {{unimplemented pure virtual method}} |
John McCall | 8f42893 | 2010-08-18 09:58:15 +0000 | [diff] [blame] | 231 | static D x; // expected-error {{abstract class}} |
| 232 | }; |
| 233 | } |
| 234 | |
| 235 | namespace test4 { |
| 236 | template <class T> struct A { |
| 237 | A x; // expected-error {{abstract class}} |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 238 | virtual void abstract() = 0; // expected-note {{unimplemented pure virtual method}} |
John McCall | 8f42893 | 2010-08-18 09:58:15 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | template <class T> struct B { |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 242 | virtual void abstract() = 0; // expected-note {{unimplemented pure virtual method}} |
John McCall | 8f42893 | 2010-08-18 09:58:15 +0000 | [diff] [blame] | 243 | B x; // expected-error {{abstract class}} |
| 244 | }; |
| 245 | |
| 246 | template <class T> struct C { |
| 247 | static C x; // expected-error {{abstract class}} |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 248 | virtual void abstract() = 0; // expected-note {{unimplemented pure virtual method}} |
John McCall | 8f42893 | 2010-08-18 09:58:15 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | template <class T> struct D { |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 252 | virtual void abstract() = 0; // expected-note {{unimplemented pure virtual method}} |
John McCall | 8f42893 | 2010-08-18 09:58:15 +0000 | [diff] [blame] | 253 | static D x; // expected-error {{abstract class}} |
| 254 | }; |
| 255 | } |
Douglas Gregor | 385d3fd | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 256 | |
Richard Smith | 72d7405 | 2013-07-20 19:41:36 +0000 | [diff] [blame] | 257 | namespace test5 { |
| 258 | struct A { A(int); virtual ~A() = 0; }; // expected-note {{pure virtual method}} |
| 259 | const A &a = 0; // expected-error {{abstract class}} |
| 260 | void f(const A &a = 0); // expected-error {{abstract class}} |
| 261 | void g() { f(0); } // expected-error {{abstract class}} |
| 262 | } |
| 263 | |
Douglas Gregor | 385d3fd | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 264 | // PR9247: Crash on invalid in clang::Sema::ActOnFinishCXXMemberSpecification |
| 265 | namespace pr9247 { |
| 266 | struct A { |
| 267 | virtual void g(const A& input) = 0; |
| 268 | struct B { |
| 269 | C* f(int foo); |
| 270 | }; |
| 271 | }; |
| 272 | } |
Aaron Ballman | ea03214 | 2012-05-07 00:02:00 +0000 | [diff] [blame] | 273 | |
| 274 | namespace pr12658 { |
David Majnemer | b100410 | 2014-03-02 18:46:05 +0000 | [diff] [blame] | 275 | class C { |
| 276 | public: |
| 277 | C(int v){} |
| 278 | virtual void f() = 0; // expected-note {{unimplemented pure virtual method 'f' in 'C'}} |
| 279 | }; |
| 280 | |
| 281 | void foo( C& c ) {} |
| 282 | |
| 283 | void bar( void ) { |
| 284 | foo(C(99)); // expected-error {{allocating an object of abstract class type 'pr12658::C'}} |
Aaron Ballman | ea03214 | 2012-05-07 00:02:00 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 287 | |
| 288 | namespace pr16659 { |
| 289 | struct A { |
| 290 | A(int); |
| 291 | virtual void x() = 0; // expected-note {{unimplemented pure virtual method 'x' in 'RedundantInit'}} |
| 292 | }; |
| 293 | struct B : virtual A {}; |
| 294 | struct C : B { |
| 295 | C() : A(37) {} |
| 296 | void x() override {} |
| 297 | }; |
| 298 | |
| 299 | struct X { |
| 300 | friend class Z; |
| 301 | private: |
| 302 | X &operator=(const X&); |
| 303 | }; |
| 304 | struct Y : virtual X { // expected-note {{::X' has an inaccessible copy assignment}} |
| 305 | virtual ~Y() = 0; |
| 306 | }; |
| 307 | struct Z : Y {}; // expected-note {{::Y' has a deleted copy assignment}} |
| 308 | void f(Z &a, const Z &b) { a = b; } // expected-error {{copy assignment operator is implicitly deleted}} |
| 309 | |
| 310 | struct RedundantInit : virtual A { |
| 311 | RedundantInit() : A(0) {} // expected-warning {{initializer for virtual base class 'pr16659::A' of abstract class 'RedundantInit' will never be used}} |
| 312 | }; |
| 313 | } |