Hans Wennborg | c9bd88e | 2014-01-14 19:35:09 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -triple %itanium_abi_triple -verify %s -std=c++11 |
| 2 | // RUN: %clang_cc1 -fsyntax-only -triple %ms_abi_triple -verify %s -std=c++11 |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 3 | namespace T1 { |
| 4 | |
| 5 | class A { |
| 6 | virtual int f(); // expected-note{{overridden virtual function is here}} |
| 7 | }; |
| 8 | |
| 9 | class B : A { |
| 10 | virtual void f(); // expected-error{{virtual function 'f' has a different return type ('void') than the function it overrides (which has return type 'int')}} |
| 11 | }; |
| 12 | |
| 13 | } |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 14 | |
| 15 | namespace T2 { |
| 16 | |
| 17 | struct a { }; |
| 18 | struct b { }; |
| 19 | |
| 20 | class A { |
| 21 | virtual a* f(); // expected-note{{overridden virtual function is here}} |
| 22 | }; |
| 23 | |
| 24 | class B : A { |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 25 | virtual b* f(); // expected-error{{return type of virtual function 'f' is not covariant with the return type of the function it overrides ('T2::b *' is not derived from 'T2::a *')}} |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | } |
| 29 | |
| 30 | namespace T3 { |
| 31 | |
| 32 | struct a { }; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 33 | struct b : private a { }; // expected-note{{declared private here}} |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 34 | |
| 35 | class A { |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 36 | virtual a* f(); // FIXME: desired-note{{overridden virtual function is here}} |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | class B : A { |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 40 | virtual b* f(); // expected-error{{invalid covariant return for virtual function: 'T3::a' is a private base class of 'T3::b'}} |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | } |
| 44 | |
| 45 | namespace T4 { |
| 46 | |
| 47 | struct a { }; |
| 48 | struct a1 : a { }; |
Nathan Sidwell | 44b2174 | 2015-01-19 01:44:02 +0000 | [diff] [blame] | 49 | struct b : a, a1 { }; // expected-warning{{direct base 'T4::a' is inaccessible due to ambiguity:\n struct T4::b -> struct T4::a\n struct T4::b -> struct T4::a1 -> struct T4::a}} |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 50 | |
| 51 | class A { |
| 52 | virtual a* f(); // expected-note{{overridden virtual function is here}} |
| 53 | }; |
| 54 | |
| 55 | class B : A { |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 56 | virtual b* f(); // expected-error{{return type of virtual function 'f' is not covariant with the return type of the function it overrides (ambiguous conversion from derived class 'T4::b' to base class 'T4::a':\n\ |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 57 | struct T4::b -> struct T4::a\n\ |
| 58 | struct T4::b -> struct T4::a1 -> struct T4::a)}} |
| 59 | }; |
| 60 | |
| 61 | } |
| 62 | |
| 63 | namespace T5 { |
| 64 | |
| 65 | struct a { }; |
| 66 | |
| 67 | class A { |
| 68 | virtual a* const f(); |
| 69 | virtual a* const g(); // expected-note{{overridden virtual function is here}} |
| 70 | }; |
| 71 | |
| 72 | class B : A { |
| 73 | virtual a* const f(); |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 74 | virtual a* g(); // expected-error{{return type of virtual function 'g' is not covariant with the return type of the function it overrides ('T5::a *' has different qualifiers than 'T5::a *const')}} |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } |
| 78 | |
| 79 | namespace T6 { |
| 80 | |
| 81 | struct a { }; |
| 82 | |
| 83 | class A { |
| 84 | virtual const a* f(); |
| 85 | virtual a* g(); // expected-note{{overridden virtual function is here}} |
| 86 | }; |
| 87 | |
| 88 | class B : A { |
| 89 | virtual a* f(); |
Chris Lattner | 24b8946 | 2010-09-05 00:17:29 +0000 | [diff] [blame] | 90 | virtual const a* g(); // expected-error{{return type of virtual function 'g' is not covariant with the return type of the function it overrides (class type 'const T6::a *' is more qualified than class type 'T6::a *'}} |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | } |
Anders Carlsson | 0a7c01f | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 94 | |
| 95 | namespace T7 { |
| 96 | struct a { }; |
| 97 | struct b { }; |
| 98 | |
| 99 | class A { |
| 100 | a* f(); |
| 101 | }; |
| 102 | |
| 103 | class B : A { |
| 104 | virtual b* f(); |
| 105 | }; |
| 106 | } |
Douglas Gregor | f107aa6 | 2009-12-01 16:18:00 +0000 | [diff] [blame] | 107 | |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 108 | namespace T8 { |
| 109 | struct a { }; |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 110 | struct b; // expected-note {{forward declaration of 'T8::b'}} |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 111 | |
| 112 | class A { |
| 113 | virtual a *f(); |
| 114 | }; |
| 115 | |
| 116 | class B : A { |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 117 | b* f(); // expected-error {{return type of virtual function 'f' is not covariant with the return type of the function it overrides ('T8::b' is incomplete)}} |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 118 | }; |
| 119 | } |
| 120 | |
| 121 | namespace T9 { |
| 122 | struct a { }; |
| 123 | |
| 124 | template<typename T> struct b : a { |
Chandler Carruth | a92409c | 2011-01-04 04:44:35 +0000 | [diff] [blame] | 125 | int a[sizeof(T) ? -1 : -1]; // expected-error {{array with a negative size}} |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | class A { |
| 129 | virtual a *f(); |
| 130 | }; |
| 131 | |
| 132 | class B : A { |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 133 | virtual b<int> *f(); // expected-note {{in instantiation of template class 'T9::b<int>' requested here}} |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 134 | }; |
| 135 | } |
| 136 | |
Douglas Gregor | f107aa6 | 2009-12-01 16:18:00 +0000 | [diff] [blame] | 137 | // PR5656 |
| 138 | class X0 { |
| 139 | virtual void f0(); |
| 140 | }; |
| 141 | class X1 : public X0 { |
| 142 | void f0() = 0; |
| 143 | }; |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 144 | |
| 145 | template <typename Base> |
| 146 | struct Foo : Base { |
Douglas Gregor | 6be3de3 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 147 | void f(int) = 0; // expected-error{{not virtual and cannot be declared pure}} |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Douglas Gregor | 6be3de3 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 150 | struct Base1 { virtual void f(int); }; |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 151 | struct Base2 { }; |
| 152 | |
| 153 | void test() { |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 154 | (void)sizeof(Foo<Base1>); |
| 155 | (void)sizeof(Foo<Base2>); // expected-note{{instantiation}} |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 156 | } |
Douglas Gregor | 6be3de3 | 2009-12-01 17:35:23 +0000 | [diff] [blame] | 157 | |
| 158 | template<typename Base> |
| 159 | struct Foo2 : Base { |
| 160 | template<typename T> int f(T); |
| 161 | }; |
| 162 | |
| 163 | void test2() { |
| 164 | Foo2<Base1> f1; |
| 165 | Foo2<Base2> f2; |
| 166 | f1.f(17); |
| 167 | f2.f(17); |
| 168 | }; |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 169 | |
| 170 | struct Foo3 { |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 171 | virtual void f(int) = 0; // expected-note{{unimplemented pure virtual method}} |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | template<typename T> |
| 175 | struct Bar3 : Foo3 { |
| 176 | void f(T); |
| 177 | }; |
| 178 | |
| 179 | void test3() { |
| 180 | Bar3<int> b3i; // okay |
| 181 | Bar3<float> b3f; // expected-error{{is an abstract class}} |
| 182 | } |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 183 | |
| 184 | // 5920 |
| 185 | namespace PR5920 { |
| 186 | class Base {}; |
| 187 | |
| 188 | template <typename T> |
| 189 | class Derived : public Base {}; |
| 190 | |
| 191 | class Foo { |
| 192 | public: |
| 193 | virtual Base* Method(); |
| 194 | }; |
| 195 | |
| 196 | class Bar : public Foo { |
| 197 | public: |
| 198 | virtual Derived<int>* Method(); |
| 199 | }; |
| 200 | } |
Chandler Carruth | df7fd5f | 2010-01-22 13:07:41 +0000 | [diff] [blame] | 201 | |
| 202 | // Look through template types and typedefs to see whether return types are |
| 203 | // pointers or references. |
| 204 | namespace PR6110 { |
| 205 | class Base {}; |
| 206 | class Derived : public Base {}; |
| 207 | |
| 208 | typedef Base* BaseP; |
| 209 | typedef Derived* DerivedP; |
| 210 | |
| 211 | class X { virtual BaseP f(); }; |
| 212 | class X1 : public X { virtual DerivedP f(); }; |
| 213 | |
| 214 | template <typename T> class Y { virtual T f(); }; |
| 215 | template <typename T1, typename T> class Y1 : public Y<T> { virtual T1 f(); }; |
| 216 | Y1<Derived*, Base*> y; |
| 217 | } |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 218 | |
Chandler Carruth | 284bb2e | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 219 | // Defer checking for covariance if either return type is dependent. |
| 220 | namespace type_dependent_covariance { |
| 221 | struct B {}; |
| 222 | template <int N> struct TD : public B {}; |
| 223 | template <> struct TD<1> {}; |
| 224 | |
| 225 | template <int N> struct TB {}; |
| 226 | struct D : public TB<0> {}; |
| 227 | |
| 228 | template <int N> struct X { |
| 229 | virtual B* f1(); // expected-note{{overridden virtual function is here}} |
| 230 | virtual TB<N>* f2(); // expected-note{{overridden virtual function is here}} |
| 231 | }; |
| 232 | template <int N, int M> struct X1 : X<N> { |
| 233 | virtual TD<M>* f1(); // expected-error{{return type of virtual function 'f1' is not covariant with the return type of the function it overrides ('TD<1> *'}} |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 234 | virtual D* f2(); // expected-error{{return type of virtual function 'f2' is not covariant with the return type of the function it overrides ('type_dependent_covariance::D *' is not derived from 'TB<1> *')}} |
Chandler Carruth | 284bb2e | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | X1<0, 0> good; |
| 238 | X1<0, 1> bad_derived; // expected-note{{instantiation}} |
| 239 | X1<1, 0> bad_base; // expected-note{{instantiation}} |
| 240 | } |
| 241 | |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 242 | namespace T10 { |
| 243 | struct A { }; |
| 244 | struct B : A { }; |
| 245 | |
| 246 | struct C { |
| 247 | virtual A&& f(); |
| 248 | }; |
| 249 | |
| 250 | struct D : C { |
| 251 | virtual B&& f(); |
| 252 | }; |
| 253 | }; |
| 254 | |
| 255 | namespace T11 { |
| 256 | struct A { }; |
| 257 | struct B : A { }; |
| 258 | |
| 259 | struct C { |
| 260 | virtual A& f(); // expected-note {{overridden virtual function is here}} |
| 261 | }; |
| 262 | |
| 263 | struct D : C { |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 264 | virtual B&& f(); // expected-error {{virtual function 'f' has a different return type ('T11::B &&') than the function it overrides (which has return type 'T11::A &')}} |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 265 | }; |
| 266 | }; |
| 267 | |
| 268 | namespace T12 { |
| 269 | struct A { }; |
| 270 | struct B : A { }; |
| 271 | |
| 272 | struct C { |
| 273 | virtual A&& f(); // expected-note {{overridden virtual function is here}} |
| 274 | }; |
| 275 | |
| 276 | struct D : C { |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 277 | virtual B& f(); // expected-error {{virtual function 'f' has a different return type ('T12::B &') than the function it overrides (which has return type 'T12::A &&')}} |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 278 | }; |
| 279 | }; |
Douglas Gregor | 5a2bb5b | 2010-10-13 22:55:32 +0000 | [diff] [blame] | 280 | |
| 281 | namespace PR8168 { |
| 282 | class A { |
| 283 | public: |
| 284 | virtual void foo() {} // expected-note{{overridden virtual function is here}} |
| 285 | }; |
| 286 | |
| 287 | class B : public A { |
| 288 | public: |
| 289 | static void foo() {} // expected-error{{'static' member function 'foo' overrides a virtual function}} |
| 290 | }; |
| 291 | } |