summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@mac.com>2009-12-31 18:34:24 +0000
committerAnders Carlsson <andersca@mac.com>2009-12-31 18:34:24 +0000
commite60365b7b5c79536084e99fbea19c91170a41349 (patch)
treee5c2c1aa8b98009b16ed19ce4a45035b699c988b
parentac61550504de1dd67525b39422db50ba3bec84c9 (diff)
downloadbcm5719-llvm-e60365b7b5c79536084e99fbea19c91170a41349.tar.gz
bcm5719-llvm-e60365b7b5c79536084e99fbea19c91170a41349.zip
Make sure that an overriding return type is complete before checking if it's covariant. Fixes PR5920.
llvm-svn: 92365
-rw-r--r--clang/include/clang/Basic/DiagnosticSemaKinds.td3
-rw-r--r--clang/lib/Sema/SemaDeclCXX.cpp9
-rw-r--r--clang/test/SemaCXX/virtual-override.cpp47
3 files changed, 59 insertions, 0 deletions
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index a1c6f470679..41f8c4c3568 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -497,6 +497,9 @@ def err_covariant_return_ambiguous_derived_to_base_conv : Error<
def err_covariant_return_not_derived : Error<
"return type of virtual function %0 is not covariant with the return type of "
"the function it overrides (%1 is not derived from %2)">;
+def err_covariant_return_incomplete : Error<
+ "return type of virtual function %0 is not covariant with the return type of "
+ "the function it overrides (%1 is incomplete)">;
def err_covariant_return_type_different_qualifications : Error<
"return type of virtual function %0 is not covariant with the return type of "
"the function it overrides (%1 has different qualifiers than %2)">;
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f8321132ff0..d6aa240df4c 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -5528,6 +5528,15 @@ bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New,
return true;
}
+ // C++ [class.virtual]p6:
+ // If the return type of D::f differs from the return type of B::f, the
+ // class type in the return type of D::f shall be complete at the point of
+ // declaration of D::f or shall be the class type D.
+ if (RequireCompleteType(New->getLocation(), NewClassTy,
+ PDiag(diag::err_covariant_return_incomplete)
+ << New->getDeclName()))
+ return true;
+
if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) {
// Check if the new class derives from the old class.
if (!IsDerivedFrom(NewClassTy, OldClassTy)) {
diff --git a/clang/test/SemaCXX/virtual-override.cpp b/clang/test/SemaCXX/virtual-override.cpp
index 503245dcfd8..4fdac85040b 100644
--- a/clang/test/SemaCXX/virtual-override.cpp
+++ b/clang/test/SemaCXX/virtual-override.cpp
@@ -104,6 +104,35 @@ namespace T7 {
};
}
+namespace T8 {
+ struct a { };
+ struct b; // expected-note {{forward declaration of 'struct T8::b'}}
+
+ class A {
+ virtual a *f();
+ };
+
+ class B : A {
+ b* f(); // expected-error {{return type of virtual function 'f' is not covariant with the return type of the function it overrides ('struct T8::b' is incomplete)}}
+ };
+}
+
+namespace T9 {
+ struct a { };
+
+ template<typename T> struct b : a {
+ int a[sizeof(T) ? -1 : -1]; // expected-error {{array size is negative}}
+ };
+
+ class A {
+ virtual a *f();
+ };
+
+ class B : A {
+ virtual b<int> *f(); // expected-note {{in instantiation of template class 'struct T9::b<int>' requested here}}
+ };
+}
+
// PR5656
class X0 {
virtual void f0();
@@ -150,3 +179,21 @@ void test3() {
Bar3<int> b3i; // okay
Bar3<float> b3f; // expected-error{{is an abstract class}}
}
+
+// 5920
+namespace PR5920 {
+ class Base {};
+
+ template <typename T>
+ class Derived : public Base {};
+
+ class Foo {
+ public:
+ virtual Base* Method();
+ };
+
+ class Bar : public Foo {
+ public:
+ virtual Derived<int>* Method();
+ };
+}
OpenPOWER on IntegriCloud