From patchwork Tue Nov 25 16:38:22 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Trevor Saunders X-Patchwork-Id: 414770 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 2E1C214010B for ; Wed, 26 Nov 2014 03:39:18 +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; q=dns; s=default; b=IGRg8qZLbltg X4m+1agJQWs1HXKNRUPE14zzBHrCd6kR9/0RH/xuQKk/F00VmrUhkISJCsDqJyw/ WJAi+u3Z7kxdUl4/v4cQAhzpLWrphEu8iektQoM1BxhhqxpmIuIceECWVzmBLUgI xZUsgvqfsH4VUJZndcZIqTX5sO5p0yo= 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; s=default; bh=VpPJvlKsAeXrZuYHfO JCp5Fe/JI=; b=RS+TxwsKUCA1yWs7rx2NXP0HNVZ0NV/Zjm5f09vZEDNMcjf3g3 uc5M7TaDl49gGupXJ2sOlt4a5uH0UucbsDm9xhVSHeNjHeHzUAAwCeelKbSOQugw tlV1Jg8kLF8z5lYNkPGY6nq/DXZq2hWLL3CQpWuuLLCo6ZWbkEm1/YNLw= Received: (qmail 17766 invoked by alias); 25 Nov 2014 16:38:54 -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 17700 invoked by uid 89); 25 Nov 2014 16:38:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL, BAYES_00, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: smtp.mozilla.org Received: from mx2.corp.phx1.mozilla.com (HELO smtp.mozilla.org) (63.245.216.70) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Tue, 25 Nov 2014 16:38:52 +0000 Received: from iceball.myhome.westell.com (pool-72-73-218-237.cmdnnj.east.verizon.net [72.73.218.237]) (Authenticated sender: tsaunders@mozilla.com) by mx2.mail.corp.phx1.mozilla.com (Postfix) with ESMTPSA id 31577F232C; Tue, 25 Nov 2014 08:38:50 -0800 (PST) From: tsaunders@mozilla.com To: gcc-patches@gcc.gnu.org Cc: Trevor Saunders Subject: [PATCH] pr31397 - implement -Wsuggest-override Date: Tue, 25 Nov 2014 11:38:22 -0500 Message-Id: <1416933502-12588-1-git-send-email-tsaunders@mozilla.com> X-IsSubscribed: yes From: Trevor Saunders Hi, this is a new warning to find places where virtual functions are over ridden, but not marked override. included test passes, I expect comments so regtest is pending, and ChangeLog is omitted. Trev --- gcc/c-family/c.opt | 5 +++++ gcc/cp/class.c | 4 ++++ gcc/doc/invoke.texi | 6 +++++- gcc/testsuite/g++.dg/warn/Wsuggest-override.C | 21 +++++++++++++++++++++ 4 files changed, 35 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 85dcb98..259b520 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -574,6 +574,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 16279df..515f33f 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -2777,6 +2777,10 @@ check_for_override (tree decl, tree ctype) { DECL_VINDEX (decl) = decl; overrides_found = true; + if (warn_override && DECL_VIRTUAL_P (decl) && !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 89edddb..8741e8e 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -275,7 +275,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 @@ -4255,6 +4255,10 @@ effective with link time optimization, where the information about the class hiearchy graph is more complete. It is recommended to first consider suggestins 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..929d365 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wsuggest-override.C @@ -0,0 +1,21 @@ +// { dg-do compile } +// { dg-options "-std=c++11 -Wsuggest-override" } +struct A +{ + A(); + virtual ~A(); + virtual void f(); + virtual int bar(); + operator int(); + virtual operator float(); +}; + +struct B : A +{ + B(); + virtual ~B(); + virtual void f(); // { dg-warning "can be marked override" } +virtual int bar() override; +operator int(); +virtual operator float(); // { dg-warning "can be marked override" } +};