From patchwork Wed Sep 9 15:09:48 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 515920 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 8404F14028F for ; Thu, 10 Sep 2015 01:10:06 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=ogG+4MrT; 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:from:message-id:date:mime-version :in-reply-to:content-type; q=dns; s=default; b=cLWrYlX+Cczt3KeGr zJQCO27NocqulAxqSXpa7SBSV2B0kc6yrbBa+iFZJugiwx/2GYgTvr9PnmcbAW7y yHd1ORuSXawL+gf1Sijxsintm4x98vSZ7oyr36R6G2WVWqfDBYCrpreKGfpPVhB4 v7c8PqEj/qdydF8OZ9Hl3rFpaQ= 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:from:message-id:date:mime-version :in-reply-to:content-type; s=default; bh=S0XE6BnoxJ7OArFW4Ja4Kcs Uanw=; b=ogG+4MrTNAW0rEGkZ+bngrp776fCG4h27RY7NHTBRgnWnzzpdW3QZMt L/veYku0YrFHjPR/xsHBxMD7jYW9oEzg2Z7bQ8QX7wbDklGqd2gOCAJBPybwXnr5 cXBVmwY2sGubYR1sVenXasvFlCZc1ZwLseYYlNZw1FSXZcTqgm1I= Received: (qmail 56708 invoked by alias); 9 Sep 2015 15:09:59 -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 56691 invoked by uid 89); 9 Sep 2015 15:09:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.7 required=5.0 tests=AWL, BAYES_00, KAM_ASCII_DIVIDERS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: aserp1040.oracle.com Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Wed, 09 Sep 2015 15:09:57 +0000 Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id t89F9s4x023992 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 9 Sep 2015 15:09:55 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserv0022.oracle.com (8.13.8/8.13.8) with ESMTP id t89F9sVl017584 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Wed, 9 Sep 2015 15:09:54 GMT Received: from abhmp0010.oracle.com (abhmp0010.oracle.com [141.146.116.16]) by userv0121.oracle.com (8.13.8/8.13.8) with ESMTP id t89F9qv0028253; Wed, 9 Sep 2015 15:09:52 GMT Received: from [192.168.1.4] (/87.10.208.171) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 09 Sep 2015 08:09:51 -0700 Subject: Re: [C++ Patch] PR 53184 ("Unnecessary anonymous namespace warnings") To: Jason Merrill , "gcc-patches@gcc.gnu.org" References: <55DB143D.1010209@oracle.com> <55F02D5B.70901@redhat.com> From: Paolo Carlini Message-ID: <55F04BBC.4020809@oracle.com> Date: Wed, 9 Sep 2015 17:09:48 +0200 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: <55F02D5B.70901@redhat.com> X-IsSubscribed: yes Hi, On 09/09/2015 03:00 PM, Jason Merrill wrote: > On 08/24/2015 08:55 AM, Paolo Carlini wrote: >> + if (no_linkage_check (ftype, /*relaxed_p=*/false)) > How about using the return value of no_linkage_check in the warning? Agreed, more informative. The below, which I'm finishing testing, also uses same_type_p to avoid printing redundant information. Thanks, Paolo. ///////////////////////////// Index: c-family/c.opt =================================================================== --- c-family/c.opt (revision 227586) +++ c-family/c.opt (working copy) @@ -944,6 +944,11 @@ Wuseless-cast C++ ObjC++ Var(warn_useless_cast) Warning Warn about useless casts +Wsubobject-linkage +C++ ObjC++ Var(warn_subobject_linkage) Warning Init(1) +Warn if a class type has a base or a field whose type uses the anonymous +namespace or depends on a type with no linkage + ansi C ObjC C++ ObjC++ A synonym for -std=c89 (for C) or -std=c++98 (for C++) Index: cp/decl2.c =================================================================== --- cp/decl2.c (revision 227586) +++ cp/decl2.c (working copy) @@ -2564,10 +2564,25 @@ constrain_class_visibility (tree type) if (subvis == VISIBILITY_ANON) { - if (!in_main_input_context ()) - warning (0, "\ + if (!in_main_input_context()) + { + tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false); + if (nlt) + { + if (same_type_p (TREE_TYPE (t), nlt)) + warning (OPT_Wsubobject_linkage, "\ +%qT has a field %qD whose type has no linkage", + type, t); + else + warning (OPT_Wsubobject_linkage, "\ +%qT has a field %qD whose type depends on the type %qT which has no linkage", + type, t, nlt); + } + else + warning (OPT_Wsubobject_linkage, "\ %qT has a field %qD whose type uses the anonymous namespace", - type, t); + type, t); + } } else if (MAYBE_CLASS_TYPE_P (ftype) && vis < VISIBILITY_HIDDEN @@ -2585,9 +2600,24 @@ constrain_class_visibility (tree type) if (subvis == VISIBILITY_ANON) { if (!in_main_input_context()) - warning (0, "\ + { + tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false); + if (nlt) + { + if (same_type_p (TREE_TYPE (t), nlt)) + warning (OPT_Wsubobject_linkage, "\ +%qT has a base %qT whose type has no linkage", + type, TREE_TYPE (t)); + else + warning (OPT_Wsubobject_linkage, "\ +%qT has a base %qT whose type depends on the type %qT which has no linkage", + type, TREE_TYPE (t), nlt); + } + else + warning (OPT_Wsubobject_linkage, "\ %qT has a base %qT whose type uses the anonymous namespace", - type, TREE_TYPE (t)); + type, TREE_TYPE (t)); + } } else if (vis < VISIBILITY_HIDDEN && subvis >= VISIBILITY_HIDDEN) Index: doc/invoke.texi =================================================================== --- doc/invoke.texi (revision 227586) +++ doc/invoke.texi (working copy) @@ -282,7 +282,7 @@ Objective-C and Objective-C++ Dialects}. -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 -Wsuggest-override @gol --Wmissing-format-attribute @gol +-Wmissing-format-attribute -Wsubobject-linkage @gol -Wswitch -Wswitch-default -Wswitch-enum -Wswitch-bool -Wsync-nand @gol -Wsystem-headers -Wtautological-compare -Wtrampolines -Wtrigraphs @gol -Wtype-limits -Wundef @gol @@ -4923,6 +4923,13 @@ types. @option{-Wconversion-null} is enabled by de Warn when a literal '0' is used as null pointer constant. This can be useful to facilitate the conversion to @code{nullptr} in C++11. +@item -Wsubobject-linkage @r{(C++ and Objective-C++ only)} +@opindex Wsubobject-linkage +@opindex Wno-subobject-linkage +Warn if a class type has a base or a field whose type uses the anonymous +namespace or depends on a type with no linkage. This warning is +enabled by default. + @item -Wdate-time @opindex Wdate-time @opindex Wno-date-time Index: testsuite/g++.dg/warn/Wsubobject-linkage-1.C =================================================================== --- testsuite/g++.dg/warn/Wsubobject-linkage-1.C (revision 0) +++ testsuite/g++.dg/warn/Wsubobject-linkage-1.C (working copy) @@ -0,0 +1,9 @@ +// PR c++/53184 + +typedef volatile struct { } Foo; + +#line 6 "foo.C" +struct Bar { Foo foo; }; // { dg-warning "no linkage" } +// { dg-bogus "anonymous namespace" "" { target *-*-* } 6 } +struct Bar2 : Foo { }; // { dg-warning "no linkage" } +// { dg-bogus "anonymous namespace" "" { target *-*-* } 8 } Index: testsuite/g++.dg/warn/Wsubobject-linkage-2.C =================================================================== --- testsuite/g++.dg/warn/Wsubobject-linkage-2.C (revision 0) +++ testsuite/g++.dg/warn/Wsubobject-linkage-2.C (working copy) @@ -0,0 +1,8 @@ +// PR c++/53184 +// { dg-options "-Wno-subobject-linkage" } + +typedef volatile struct { } Foo; + +#line 7 "foo.C" +struct Bar { Foo foo; }; +struct Bar2 : Foo { }; Index: testsuite/g++.dg/warn/Wsubobject-linkage-3.C =================================================================== --- testsuite/g++.dg/warn/Wsubobject-linkage-3.C (revision 0) +++ testsuite/g++.dg/warn/Wsubobject-linkage-3.C (working copy) @@ -0,0 +1,9 @@ +// PR c++/53184 + +namespace { struct Foo { }; } + +#line 6 "foo.C" +struct Bar { Foo foo; }; // { dg-warning "anonymous namespace" } +// { dg-bogus "no linkage" "" { target *-*-* } 6 } +struct Bar2 : Foo { }; // { dg-warning "anonymous namespace" } +// { dg-bogus "no linkage" "" { target *-*-* } 8 } Index: testsuite/g++.dg/warn/Wsubobject-linkage-4.C =================================================================== --- testsuite/g++.dg/warn/Wsubobject-linkage-4.C (revision 0) +++ testsuite/g++.dg/warn/Wsubobject-linkage-4.C (working copy) @@ -0,0 +1,8 @@ +// PR c++/53184 +// { dg-options "-Wno-subobject-linkage" } + +namespace { struct Foo { }; } + +#line 7 "foo.C" +struct Bar { Foo foo; }; +struct Bar2 : Foo { };