Commit 7506ab1d by Ville Voutilainen Committed by Jason Merrill

Fixes for override/final.

	* class.c (check_for_override): Diagnose final on a nonvirtual
	member function, diagnose override for a virtual with no matching
	override. Don't fiddle around with DECL_VINDEX.

From-SVN: r173626
parent d26e5986
2011-05-10 Ville Voutilainen <ville.voutilainen@gmail.com>
Fixes for override/final.
* class.c (check_for_override): Diagnose final on a nonvirtual
member function, diagnose override for a virtual with no matching
override. Don't fiddle around with DECL_VINDEX.
2011-05-10 Nathan Froyd <froydnj@codesourcery.com> 2011-05-10 Nathan Froyd <froydnj@codesourcery.com>
* cp-tree.def (EXPR_PACK_EXPANSION): Add an operand. * cp-tree.def (EXPR_PACK_EXPANSION): Add an operand.
......
...@@ -2453,6 +2453,7 @@ get_basefndecls (tree name, tree t) ...@@ -2453,6 +2453,7 @@ get_basefndecls (tree name, tree t)
void void
check_for_override (tree decl, tree ctype) check_for_override (tree decl, tree ctype)
{ {
bool overrides_found = false;
if (TREE_CODE (decl) == TEMPLATE_DECL) if (TREE_CODE (decl) == TEMPLATE_DECL)
/* In [temp.mem] we have: /* In [temp.mem] we have:
...@@ -2467,7 +2468,10 @@ check_for_override (tree decl, tree ctype) ...@@ -2467,7 +2468,10 @@ check_for_override (tree decl, tree ctype)
/* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
the error_mark_node so that we know it is an overriding the error_mark_node so that we know it is an overriding
function. */ function. */
DECL_VINDEX (decl) = decl; {
DECL_VINDEX (decl) = decl;
overrides_found = true;
}
if (DECL_VIRTUAL_P (decl)) if (DECL_VIRTUAL_P (decl))
{ {
...@@ -2477,11 +2481,10 @@ check_for_override (tree decl, tree ctype) ...@@ -2477,11 +2481,10 @@ check_for_override (tree decl, tree ctype)
if (DECL_DESTRUCTOR_P (decl)) if (DECL_DESTRUCTOR_P (decl))
TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true; TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
} }
else if (DECL_OVERRIDE_P (decl)) else if (DECL_FINAL_P (decl))
{ error ("%q+#D marked final, but is not virtual", decl);
DECL_VINDEX (decl) = error_mark_node; if (DECL_OVERRIDE_P (decl) && !overrides_found)
error ("%q+#D marked override, but does not override", decl); error ("%q+#D marked override, but does not override", decl);
}
} }
/* Warn about hidden virtual functions that are not overridden in t. /* Warn about hidden virtual functions that are not overridden in t.
......
2011-05-10 Ville Voutilainen <ville.voutilainen@gmail.com>
* g++.dg/inherit/virtual9.C: Extend.
2011-05-10 Michael Meissner <meissner@linux.vnet.ibm.com> 2011-05-10 Michael Meissner <meissner@linux.vnet.ibm.com>
PR target/48857 PR target/48857
......
...@@ -3,6 +3,7 @@ struct B ...@@ -3,6 +3,7 @@ struct B
{ {
virtual void f() final {} virtual void f() final {}
virtual void g() {} virtual void g() {}
virtual void x() const {}
}; };
struct B2 struct B2
...@@ -20,7 +21,12 @@ template <class T> struct D2 : T ...@@ -20,7 +21,12 @@ template <class T> struct D2 : T
void h() override {} // { dg-error "marked override, but does not override" } void h() override {} // { dg-error "marked override, but does not override" }
}; };
struct D3 : D template <class T> struct D3 : T
{
void h() override {}
};
struct D4 : D
{ {
void g() {} // { dg-error "virtual function" } void g() {} // { dg-error "virtual function" }
}; };
...@@ -30,10 +36,25 @@ struct B3 ...@@ -30,10 +36,25 @@ struct B3
virtual void f() final final {} // { dg-error "duplicate virt-specifier" } virtual void f() final final {} // { dg-error "duplicate virt-specifier" }
}; };
void g() override {} // { dg-error "virt-specifiers" } struct B4
{
void f() final {} // { dg-error "marked final, but is not virtual" }
};
struct D5 : B
{
void ff() override {} // { dg-error "marked override, but does not override" }
virtual void fff() override {} // { dg-error "marked override, but does not override" }
virtual void x() override {} // { dg-error "marked override, but does not override" }
void g() override;
};
void D5::g() override {} // { dg-error "not allowed outside a class definition" }
void g() override {} // { dg-error "not allowed outside a class definition" }
int main() int main()
{ {
D2<B> d2; D2<B> d;
D2<B2> d3; D2<B2> d2;
D3<B2> d3;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment