From patchwork Wed Dec 24 03:14:24 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tbsaunde+gcc@tbsaunde.org X-Patchwork-Id: 423844 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id E143514007D for ; Wed, 24 Dec 2014 14:16:43 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; q=dns; s= default; b=TD69S9qjgUQe+twEJ+LaG6JxDnCWcaCxc6Uxyfq1AQdhgmQHC357i QG6v2TEs+DiIYvvlz6vXbOndDR8jPMq978kz6Mlzrvc8/My+hV3yfWmlOOZLCk+r jdmRyg7IxqPT3ydufv8dWqQvE8BHqTBr7J03XQ0x2Offf4L8iKmA18= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; s= default; bh=ok4AJUcUEEWHSwCMo3/k17JacCM=; b=Rr9toJVcZqXOuKlxaXW+ Fwthx1pGxRuaiBNRcwK0VImlt+ku2x9STgjMyo+GswILkIC2Aj6xfzAEMnB8QP/o slAxhRW7zbRaQM5eIJSgavdVz5qZJetWMjxcyShg/Sc34C7/t5uRati9Xj6VogC0 SGQeuKbzvoHEN7HbeRY+Dr4= Received: (qmail 20275 invoked by alias); 24 Dec 2014 03:16:36 -0000 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 Received: (qmail 20243 invoked by uid 89); 24 Dec 2014 03:16:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: paperclip.tbsaunde.org Received: from tbsaunde.org (HELO paperclip.tbsaunde.org) (66.228.47.254) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 24 Dec 2014 03:16:34 +0000 Received: from iceball.myhome.westell.com (pool-72-73-221-82.cmdnnj.east.verizon.net [72.73.221.82]) by paperclip.tbsaunde.org (Postfix) with ESMTPSA id 6B548C0BD; Wed, 24 Dec 2014 03:16:30 +0000 (UTC) From: tbsaunde+gcc@tbsaunde.org To: jason@redhat.com Cc: gcc-patches@gcc.gnu.org, Trevor Saunders Subject: [PATCH] pr31397 - implement -Wsuggest-override Date: Tue, 23 Dec 2014 22:14:24 -0500 Message-Id: <1419390864-16128-1-git-send-email-tbsaunde+gcc@tbsaunde.org> In-Reply-To: <5491F3DB.2000102@redhat.com> References: <5491F3DB.2000102@redhat.com> X-IsSubscribed: yes From: Trevor Saunders Hi, comments fixed. bootstrapped on x86_64-linux, new test passes and regtest pending, ok? Trev c-family/ * c.opt (Wsuggest-override): New option. cp/ * class.c (check_for_override): Warn when a virtual function is an override not marked override. gcc/ * doc/invoke.texi: Document -Wsuggest-override. --- gcc/c-family/c.opt | 5 +++++ gcc/cp/class.c | 4 ++++ gcc/doc/invoke.texi | 6 +++++- gcc/testsuite/g++.dg/warn/Wsuggest-override.C | 23 +++++++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wsuggest-override.C diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index f86718b..1f6f793 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -578,6 +578,11 @@ Wsuggest-attribute=format C ObjC C++ ObjC++ Var(warn_suggest_attribute_format) Warning Warn about functions which might be candidates for format attributes +Wsuggest-override +C++ ObjC++ Var(warn_override) Warning +Suggest that the override keyword be used when the declaration of a virtual +function overrides another. + Wswitch C ObjC C++ ObjC++ Var(warn_switch) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) Warn about enumerated switches, with no default, missing a case diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 0ac2124..d2cf4a0 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -2811,6 +2811,10 @@ check_for_override (tree decl, tree ctype) { DECL_VINDEX (decl) = decl; overrides_found = true; + if (warn_override && !DECL_OVERRIDE_P (decl) + && !DECL_DESTRUCTOR_P (decl)) + warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override, + "%q+D can be marked override", decl); } if (DECL_VIRTUAL_P (decl)) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 9f56f42..d0ee093 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -277,7 +277,7 @@ Objective-C and Objective-C++ Dialects}. -Wstack-protector -Wstack-usage=@var{len} -Wstrict-aliasing @gol -Wstrict-aliasing=n @gol -Wstrict-overflow -Wstrict-overflow=@var{n} @gol -Wsuggest-attribute=@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r{]} @gol --Wsuggest-final-types @gol -Wsuggest-final-methods @gol +-Wsuggest-final-types @gol -Wsuggest-final-methods @gol -Wsuggest-override @gol -Wmissing-format-attribute @gol -Wswitch -Wswitch-default -Wswitch-enum -Wswitch-bool -Wsync-nand @gol -Wsystem-headers -Wtrampolines -Wtrigraphs -Wtype-limits -Wundef @gol @@ -4276,6 +4276,10 @@ class hierarchy graph is more complete. It is recommended to first consider suggestions of @option{-Wsuggest-final-types} and then rebuild with new annotations. +@item -Wsuggest-override +Warn about overriding virtual functions that are not marked with the override +keyword. + @item -Warray-bounds @opindex Wno-array-bounds @opindex Warray-bounds diff --git a/gcc/testsuite/g++.dg/warn/Wsuggest-override.C b/gcc/testsuite/g++.dg/warn/Wsuggest-override.C new file mode 100644 index 0000000..f820f4b --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wsuggest-override.C @@ -0,0 +1,23 @@ +// { dg-do compile } +// { dg-options "-std=c++11 -Wsuggest-override" } +struct A +{ + A(); + virtual ~A(); + virtual void f(); + virtual int bar(); + int c(); + operator int(); + virtual operator float(); +}; + +struct B : A +{ + B(); + virtual ~B(); + virtual void f(); // { dg-warning "can be marked override" } +virtual int bar() override; +int c(); +operator int(); +virtual operator float(); // { dg-warning "can be marked override" } +};