From patchwork Mon May 9 21:55:43 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ville Voutilainen X-Patchwork-Id: 94901 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 48EF3B6EEC for ; Tue, 10 May 2011 08:24:08 +1000 (EST) Received: (qmail 11490 invoked by alias); 9 May 2011 21:56:05 -0000 Received: (qmail 11481 invoked by uid 22791); 9 May 2011 21:56:04 -0000 X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RFC_ABUSE_POST, TW_FN, T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from mail-ew0-f47.google.com (HELO mail-ew0-f47.google.com) (209.85.215.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 09 May 2011 21:55:51 +0000 Received: by ewy5 with SMTP id 5so1859101ewy.20 for ; Mon, 09 May 2011 14:55:49 -0700 (PDT) Received: by 10.14.126.131 with SMTP id b3mr3191470eei.4.1304978149849; Mon, 09 May 2011 14:55:49 -0700 (PDT) Received: from ville-laptop.gmail.com ([88.193.47.74]) by mx.google.com with ESMTPS id z1sm3818762eeb.7.2011.05.09.14.55.49 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 09 May 2011 14:55:49 -0700 (PDT) Date: Tue, 10 May 2011 00:55:43 +0300 Message-ID: <87wrhzzggw.wl%ville@ville-laptop> From: Ville Voutilainen To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com Subject: [PATCH] C++0x, fixes for override/final User-Agent: Wanderlust/2.14.0 (Africa) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.7 Emacs/23.1 (i486-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Diagnose final on non-virtuals properly, diagnose override on virtuals that don't actually override properly. Amend these cases to the tests. Tested on Linux/X86-32. 2011-05-10 Ville Voutilainen 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. * virtual9.C: add tests that cover the aforementioned cases, add a test for a definition of a member function outside its class definition, add a test where cv-qualifiers don't match. diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 12db2bc..24184d2 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -2453,16 +2453,18 @@ get_basefndecls (tree name, tree t) void check_for_override (tree decl, tree ctype) { + int overrides_found = 0; if (TREE_CODE (decl) == TEMPLATE_DECL) /* In [temp.mem] we have: A specialization of a member function template does not override a virtual function from a base class. */ return; + overrides_found = look_for_overrides (ctype, decl); if ((DECL_DESTRUCTOR_P (decl) || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) || DECL_CONV_FN_P (decl)) - && look_for_overrides (ctype, decl) + && overrides_found && !DECL_STATIC_FUNCTION_P (decl)) /* 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 @@ -2477,11 +2479,11 @@ check_for_override (tree decl, tree ctype) if (DECL_DESTRUCTOR_P (decl)) TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true; } - else if (DECL_OVERRIDE_P (decl)) - { - DECL_VINDEX (decl) = error_mark_node; - error ("%q+#D marked override, but does not override", decl); - } + else if (DECL_FINAL_P (decl)) + error ("%q+#D marked final, but is not virtual", decl); + if (DECL_OVERRIDE_P (decl) + && !overrides_found) + error ("%q+#D marked override, but does not override", decl); } /* Warn about hidden virtual functions that are not overridden in t. diff --git a/gcc/testsuite/g++.dg/inherit/virtual9.C b/gcc/testsuite/g++.dg/inherit/virtual9.C index d3175e1..83e0479 100644 --- a/gcc/testsuite/g++.dg/inherit/virtual9.C +++ b/gcc/testsuite/g++.dg/inherit/virtual9.C @@ -3,6 +3,7 @@ struct B { virtual void f() final {} virtual void g() {} + virtual void x() const {} }; struct B2 @@ -20,7 +21,12 @@ template struct D2 : T void h() override {} // { dg-error "marked override, but does not override" } }; -struct D3 : D +template struct D3 : T +{ + void h() override {} +}; + +struct D4 : D { void g() {} // { dg-error "virtual function" } }; @@ -30,10 +36,25 @@ struct B3 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() { - D2 d2; - D2 d3; + D2 d; + D2 d2; + D3 d3; }