From patchwork Thu Sep 17 14:43:20 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 518893 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 30F8114132A for ; Fri, 18 Sep 2015 00:43:33 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=dgxEr0Ad; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type; q=dns; s=default; b=VYGiGWBZAZImjbBlv N0Y6cD4Gjoh1U5Ubqg0gsb2+kuhPgPziBMEXjSVB1+KPCkMJajeM301VR/hPU4F4 a8VPcffgO+qrrl4TY31DdSIwx7cZ+0dEWW2+JeqIb27u4SSQsQieGp4IUrAdSW/h iTZ5WoNzQbuF++XqwvRjk/rylM= 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 :subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type; s=default; bh=sIBv2kRGZEIBHg2m5l6znTt L7h8=; b=dgxEr0AdC/7i68YwLHarvVrmyr1dmFwaedgaPpNmg1M30dZV1DQtkoh kWWBKFM86ffAUdXsSYWdxoW+4WBOVj1wE+eBu2u6IUoNT2D5Iwp9r2LRadb8OiTg jEubkFrTfRtO+ycG4EdmjBDedNFNDZMpNlJtAxk2Y1rHwpK/ZD2I= Received: (qmail 59092 invoked by alias); 17 Sep 2015 14:43:26 -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 59078 invoked by uid 89); 17 Sep 2015 14:43:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.9 required=5.0 tests=BAYES_50, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 X-HELO: mail-qk0-f179.google.com Received: from mail-qk0-f179.google.com (HELO mail-qk0-f179.google.com) (209.85.220.179) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Thu, 17 Sep 2015 14:43:23 +0000 Received: by qkfq186 with SMTP id q186so7273119qkf.1 for ; Thu, 17 Sep 2015 07:43:21 -0700 (PDT) X-Received: by 10.55.16.135 with SMTP id 7mr51750763qkq.81.1442501001422; Thu, 17 Sep 2015 07:43:21 -0700 (PDT) Received: from ?IPv6:2601:181:c000:c497:a2a8:cdff:fe3e:b48? ([2601:181:c000:c497:a2a8:cdff:fe3e:b48]) by smtp.googlemail.com with ESMTPSA id l8sm1393882qge.31.2015.09.17.07.43.20 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Sep 2015 07:43:21 -0700 (PDT) Subject: Re: [C++] Coding rule enforcement To: Jason Merrill , Richard Biener References: <55F816F2.5010209@acm.org> <55F81E34.3020903@redhat.com> <55F95A54.7050809@acm.org> <55F97B68.9060500@redhat.com> Cc: GCC Patches From: Nathan Sidwell Message-ID: <55FAD188.6080604@acm.org> Date: Thu, 17 Sep 2015 10:43:20 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <55F97B68.9060500@redhat.com> On 09/16/15 10:23, Jason Merrill wrote: > On 09/16/2015 08:02 AM, Nathan Sidwell wrote: >> + else if (warn_multiple_inheritance) >> + warning (OPT_Wmultiple_inheritance, >> + "%qT defined with multiple direct bases", ref); > > You don't need to guard the warning with a check of the warning flag; warning > will only give the warning if the option is enabled. the spelling mistake on the option I used to experiment on didn't help, but I discovered one must have the Var clause in the options file too, which I didn't expect. Anyway, fixed now. ok? nathan 2015-09-17 Nathan Sidwell c-family/ * c.opt (Wmultiple-inheritance, Wvirtual-inheritance, Wtemplates, Wnamespaces): New C++ warnings. cp/ * decl.c (xref_basetypes): Check virtual and/or multiple inheritance warning.. * parser.c (cp_parser_namespace_definition): Check namespaces warning. * pt.c (push_template_decl_real): Check templates warning. * doc/invoke.texi (-Wmultiple-inheritance, -Wvirtual-inheritance, -Wtemplates, -Wnamespaces): Document. testsuite/ * g++.dg/diagostic/disable.C: New. Index: gcc/c-family/c.opt =================================================================== --- gcc/c-family/c.opt (revision 227761) +++ gcc/c-family/c.opt (working copy) @@ -573,6 +573,14 @@ Wmissing-field-initializers C ObjC C++ ObjC++ Var(warn_missing_field_initializers) Warning EnabledBy(Wextra) Warn about missing fields in struct initializers +Wmultiple-inheritance +C++ ObjC++ Var(warn_multiple_inheritance) Warning +Warn on direct multiple inheritance + +Wnamespaces +C++ ObjC++ Var(warn_namespaces) Warning +Warn on namespace definition + Wsized-deallocation C++ ObjC++ Var(warn_sized_deallocation) Warning EnabledBy(Wextra) Warn about missing sized deallocation functions @@ -610,6 +618,10 @@ Wswitch-bool C ObjC C++ ObjC++ Var(warn_switch_bool) Warning Init(1) Warn about switches with boolean controlling expression +Wtemplates +C++ ObjC++ Var(warn_templates) Warning +Warn on primary template declaration + Wmissing-format-attribute C ObjC C++ ObjC++ Alias(Wsuggest-attribute=format) ; @@ -936,6 +948,10 @@ Wvolatile-register-var C ObjC C++ ObjC++ Var(warn_volatile_register_var) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) Warn when a register variable is declared volatile +Wvirtual-inheritance +C++ ObjC++ Var(warn_virtual_inheritance) Warning +Warn on direct virtual inheritance + Wvirtual-move-assign C++ ObjC++ Var(warn_virtual_move_assign) Warning Init(1) Warn if a virtual base has a non-trivial move assignment operator Index: gcc/cp/decl.c =================================================================== --- gcc/cp/decl.c (revision 227761) +++ gcc/cp/decl.c (working copy) @@ -12729,6 +12729,7 @@ xref_basetypes (tree ref, tree base_list tree binfo, base_binfo; unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */ unsigned max_bases = 0; /* Maximum direct bases. */ + unsigned max_dvbases = 0; /* Maximum direct virtual bases. */ int i; tree default_access; tree igo_prev; /* Track Inheritance Graph Order. */ @@ -12766,12 +12767,13 @@ xref_basetypes (tree ref, tree base_list { max_bases++; if (TREE_TYPE (*basep)) - max_vbases++; + max_dvbases++; if (CLASS_TYPE_P (basetype)) max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype)); basep = &TREE_CHAIN (*basep); } } + max_vbases += max_dvbases; TYPE_MARKED_P (ref) = 1; @@ -12814,6 +12816,9 @@ xref_basetypes (tree ref, tree base_list error ("Java class %qT cannot have multiple bases", ref); return false; } + else + warning (OPT_Wmultiple_inheritance, + "%qT defined with multiple direct bases", ref); } if (max_vbases) @@ -12825,6 +12830,9 @@ xref_basetypes (tree ref, tree base_list error ("Java class %qT cannot have virtual bases", ref); return false; } + else if (max_dvbases) + warning (OPT_Wvirtual_inheritance, + "%qT defined with direct virtual base", ref); } for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list)) Index: gcc/cp/parser.c =================================================================== --- gcc/cp/parser.c (revision 227761) +++ gcc/cp/parser.c (working copy) @@ -16798,6 +16798,8 @@ cp_parser_namespace_definition (cp_parse has_visibility = handle_namespace_attrs (current_namespace, attribs); + warning (OPT_Wnamespaces, "namepace %qD entered", current_namespace); + /* Parse the body of the namespace. */ cp_parser_namespace_body (parser); Index: gcc/cp/pt.c =================================================================== --- gcc/cp/pt.c (revision 227761) +++ gcc/cp/pt.c (working copy) @@ -5088,6 +5088,8 @@ push_template_decl_real (tree decl, bool if (is_primary) { + warning (OPT_Wtemplates, "template %qD declared", decl); + if (DECL_CLASS_SCOPE_P (decl)) member_template_p = true; if (TREE_CODE (decl) == TYPE_DECL Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 227761) +++ gcc/doc/invoke.texi (working copy) @@ -198,12 +198,13 @@ in the following sections. -fvisibility-ms-compat @gol -fext-numeric-literals @gol -Wabi=@var{n} -Wabi-tag -Wconversion-null -Wctor-dtor-privacy @gol --Wdelete-non-virtual-dtor -Wliteral-suffix -Wnarrowing @gol +-Wdelete-non-virtual-dtor -Wliteral-suffix -Wmultiple-inheritance @gol +-Wnamespaces -Wnarrowing @gol -Wnoexcept -Wnon-virtual-dtor -Wreorder @gol --Weffc++ -Wstrict-null-sentinel @gol +-Weffc++ -Wstrict-null-sentinel -Wtemplates @gol -Wno-non-template-friend -Wold-style-cast @gol -Woverloaded-virtual -Wno-pmf-conversions @gol --Wsign-promo} +-Wsign-promo -Wvirtual-inheritance} @item Objective-C and Objective-C++ Language Options @xref{Objective-C and Objective-C++ Dialect Options,,Options Controlling @@ -2903,6 +2904,37 @@ enumerated type to a signed type, over a the same size. Previous versions of G++ tried to preserve unsignedness, but the standard mandates the current behavior. +@item -Wtemplates @r{(C++ and Objective-C++ only)} +@opindex Wtemplates +Warn when a primary template declaration is encountered. Some coding +rules disallow templates, and this may be used to enforce that rule. +The warning is inactive inside a system header file, such as the STL, so +one can still use the STL. One may also instantiate or specialize +templates. + +@item -Wmultiple-inheritance @r{(C++ and Objective-C++ only)} +@opindex Wmultiple-inheritance +Warn when a class is defined with multiple direct base classes. Some +coding rules disallow multiple inheritance, and this may be used to +enforce that rule. The warning is inactive inside a system header file, +such as the STL, so one can still use the STL. One may also define +classes that indirectly use multiple inheritance. + +@item -Wvirtual-inheritance +@opindex Wvirtual-inheritance +Warn when a class is defined with a virtual direct base classe. Some +coding rules disallow multiple inheritance, and this may be used to +enforce that rule. The warning is inactive inside a system header file, +such as the STL, so one can still use the STL. One may also define +classes that indirectly use virtual inheritance. + +@item -Wnamespaces +@opindex Wnamespaces +Warn when a namespace definition is opened. Some coding rules disallow +namespaces, and this may be used to enforce that rule. The warning is +inactive inside a system header file, such as the STL, so one can still +use the STL. One may also use using directives and qualified names. + @item -Wno-terminate @r{(C++ and Objective-C++ only)} @opindex Wterminate @opindex Wno-terminate Index: gcc/testsuite/g++.dg/diagnostic/disable.C =================================================================== --- gcc/testsuite/g++.dg/diagnostic/disable.C (revision 0) +++ gcc/testsuite/g++.dg/diagnostic/disable.C (working copy) @@ -0,0 +1,27 @@ +// { dg-options "-Wtemplates -Wmultiple-inheritance -Wvirtual-inheritance -Wnamespaces" } + +#include +#include + +namespace foo { } // { dg-warning "namepace" } + +template X Foo (); // { dg-warning "template" } + +struct B1 {}; +struct B2 {}; +struct V {}; + +struct D : B1, B2 {}; // { dg-warning "multiple" } + +struct E : virtual V {}; // { dg-warning "virtual" } + +struct F1 : E {}; + +struct F2 : D {}; + +void Baz (int a, int b) +{ + std::swap (a, b); +} + +