From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 7A86C3858405 for ; Fri, 22 Jul 2022 21:30:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7A86C3858405 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-25-wiuQuRW1PduTLizRR2eO3A-1; Fri, 22 Jul 2022 17:30:17 -0400 X-MC-Unique: wiuQuRW1PduTLizRR2eO3A-1 Received: by mail-qk1-f200.google.com with SMTP id s9-20020a05620a254900b006b54dd4d6deso4590777qko.3 for ; Fri, 22 Jul 2022 14:30:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=NamndklbRl/f+Gx0e5lDCuLtdJkJnB8DFmDNJDUdnqs=; b=eCDQHlLrU9VQnQXQ4liokdRvVGxdqdXNfWkRufzM1lazPY1oHAGq7qTsyCYeezHysm Kh1+4GDF/ADfRb3TmfzmzgPV3SgkjWQLz1kXhgS2Hs3wEGP/5JkkihkJpAo5MP+6XJNf /n+J3V0ChE21E3CkTlmXgljGA6M10qudg/gD7qGbcxCp8+DoEzuT1pAl1SQVpX3mg+Kw Il+kpocZiSQHNRKF47Ri3Iq3cabNDX3UcojiC3arL8qpWQYDYsLbgkBTgMw0ZHzPqt1M CVS8JyOZjrkegSqfxJkNMdu0NppcXykDr77wOtKPpzD9OGZ0KPkO+wiuvWb4MFkYLyZf eIsw== X-Gm-Message-State: AJIora+NalTjupEbjjYODhqEisYvvrA/lC0ROV/6qee2xtaqCZ6+usoq 2f7Lv752/bGcJeKIIQnXQo4A+fm/iMU36yxEOAkCaid/9gUTS5wPj0ptiq9bHqpB+I9ICrfGBGI m4Vape5qMLwy0bsQmzQ== X-Received: by 2002:ad4:596a:0:b0:474:99d:e65f with SMTP id eq10-20020ad4596a000000b00474099de65fmr1784415qvb.63.1658525415921; Fri, 22 Jul 2022 14:30:15 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tsegWMz8xRF/igZdQnB8kpXtYH1DrhOIox26O4sX4kL0lsrfiH9JD6KbVOvDATgOeV1mo1dg== X-Received: by 2002:ad4:596a:0:b0:474:99d:e65f with SMTP id eq10-20020ad4596a000000b00474099de65fmr1784395qvb.63.1658525415528; Fri, 22 Jul 2022 14:30:15 -0700 (PDT) Received: from [192.168.1.100] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id bm5-20020a05620a198500b006a67d257499sm3982524qkb.56.2022.07.22.14.30.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 22 Jul 2022 14:30:14 -0700 (PDT) Message-ID: <2d5773f2-4d92-37bb-93d0-f744e03571b6@redhat.com> Date: Fri, 22 Jul 2022 17:30:13 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH] c++: Refer to internal linkage for -Wsubobject-linkage [PR86491] To: Jonathan Wakely , gcc-patches@gcc.gnu.org References: <20220630165326.1253733-1-jwakely@redhat.com> From: Jason Merrill In-Reply-To: <20220630165326.1253733-1-jwakely@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 22 Jul 2022 21:30:20 -0000 On 6/30/22 12:53, Jonathan Wakely via Gcc-patches wrote: > Tested powerpc64le-linux, OK for trunk? > > -- >8 -- > > Since C++11 relaxed the requirement for template arguments to have > external linkage, it's possible to get -Wsubobject-linkage warnings > without using any anonymous namespaces. This confuses users when they > get diagnostics that refer to an anonymous namespace that doesn't exist > in their code. > > This changes the diagnostic to say "has internal linkage" for C++11 and > later, which is accurate whether internal linkage is due to the 'static' > specifier, or due to the use of anonymous namespaces. > > For C++98 template arguments declared with 'static' are ill-formed > anyway, so the only way this warning can arise is via anonymous > namespaces. That means the existing wording is accurate for C++98 and so > we can keep it. I'd prefer to keep the existing wording for types that are actually in an anonymous namespace. Checking decl_anon_ns_mem_p seems like the way to do that, though it would probably need to remove the existing type shortcut and instead just do decl = TYPE_MAIN_DECL (decl). > PR c++/86491 > > gcc/cp/ChangeLog: > > * decl2.cc (constrain_class_visibility): Adjust wording of > -Wsubobject-linkage to account for cases where anonymous > namespaces aren't used. > > gcc/testsuite/ChangeLog: > > * g++.dg/warn/Wsubobject-linkage-3.C: Adjust for new warning. > * g++.dg/warn/anonymous-namespace-1.C: Use separate dg-warning > directives for C++98 and everything else. > * g++.dg/warn/anonymous-namespace-2.C: Likewise. > * g++.dg/warn/anonymous-namespace-3.C: Likewise. > --- > gcc/cp/decl2.cc | 12 ++++++++++-- > gcc/testsuite/g++.dg/warn/Wsubobject-linkage-3.C | 4 ++-- > gcc/testsuite/g++.dg/warn/anonymous-namespace-1.C | 8 ++++++-- > gcc/testsuite/g++.dg/warn/anonymous-namespace-2.C | 9 ++++++--- > gcc/testsuite/g++.dg/warn/anonymous-namespace-3.C | 3 ++- > 5 files changed, 26 insertions(+), 10 deletions(-) > > diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc > index 3737e5f010c..de53678715e 100644 > --- a/gcc/cp/decl2.cc > +++ b/gcc/cp/decl2.cc > @@ -3027,7 +3027,11 @@ constrain_class_visibility (tree type) > %qT has a field %qD whose type depends on the type %qT which has no linkage", > type, t, nlt); > } > - else > + else if (cxx_dialect > cxx98) > + warning (OPT_Wsubobject_linkage, "\ > +%qT has a field %qD whose type has internal linkage", > + type, t); > + else // In C++98 this can only happen with unnamed namespaces. > warning (OPT_Wsubobject_linkage, "\ > %qT has a field %qD whose type uses the anonymous namespace", > type, t); > @@ -3062,7 +3066,11 @@ constrain_class_visibility (tree type) > %qT has a base %qT whose type depends on the type %qT which has no linkage", > type, TREE_TYPE (t), nlt); > } > - else > + else if (cxx_dialect > cxx98) > + warning (OPT_Wsubobject_linkage, "\ > +%qT has a base %qT whose type has internal linkage", > + type, TREE_TYPE (t)); > + else // In C++98 this can only happen with unnamed namespaces. > warning (OPT_Wsubobject_linkage, "\ > %qT has a base %qT whose type uses the anonymous namespace", > type, TREE_TYPE (t)); > diff --git a/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-3.C b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-3.C > index 95a04501441..b116fbbb186 100644 > --- a/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-3.C > +++ b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-3.C > @@ -3,7 +3,7 @@ > namespace { struct Foo { }; } > > #line 6 "foo.C" > -struct Bar { Foo foo; }; // { dg-warning "anonymous namespace" } > +struct Bar { Foo foo; }; // { dg-warning "anonymous namespace|internal linkage" } > // { dg-bogus "no linkage" "" { target *-*-* } .-1 } > -struct Bar2 : Foo { }; // { dg-warning "anonymous namespace" } > +struct Bar2 : Foo { }; // { dg-warning "anonymous namespace|internal linkage" } > // { dg-bogus "no linkage" "" { target *-*-* } .-1 } > diff --git a/gcc/testsuite/g++.dg/warn/anonymous-namespace-1.C b/gcc/testsuite/g++.dg/warn/anonymous-namespace-1.C > index cf193e0cba5..eed3818c5cf 100644 > --- a/gcc/testsuite/g++.dg/warn/anonymous-namespace-1.C > +++ b/gcc/testsuite/g++.dg/warn/anonymous-namespace-1.C > @@ -14,5 +14,9 @@ class foobar1 > }; > > #line 17 "foo.C" > -class foobar : public bad { }; // { dg-warning "uses the anonymous namespace" } > -class foobar2 { bad b; }; // { dg-warning "uses the anonymous namespace" } > +class foobar : public bad { }; > +// { dg-warning "has internal linkage" "" { target c++11 } .-1 } > +// { dg-warning "uses the anonymous namespace" "" { target c++98_only } .-2 } > +class foobar2 { bad b; }; > +// { dg-warning "has internal linkage" "" { target c++11 } .-1 } > +// { dg-warning "uses the anonymous namespace" "" { target c++98_only } .-2 } > diff --git a/gcc/testsuite/g++.dg/warn/anonymous-namespace-2.C b/gcc/testsuite/g++.dg/warn/anonymous-namespace-2.C > index 4048f3959df..f2ca5915278 100644 > --- a/gcc/testsuite/g++.dg/warn/anonymous-namespace-2.C > +++ b/gcc/testsuite/g++.dg/warn/anonymous-namespace-2.C > @@ -18,12 +18,15 @@ struct g3 { > }; > > #line 21 "foo.C" > -struct b1 { // { dg-warning "uses the anonymous namespace" } > +struct b1 { // { dg-warning "has internal linkage" "" { target c++11 } } > +// { dg-warning "uses the anonymous namespace" "" { target c++98_only } .-1 } > bad * B; > }; > -struct b2 { // { dg-warning "uses the anonymous namespace" } > +struct b2 { // { dg-warning "has internal linkage" "" { target c++11 } } > +// { dg-warning "uses the anonymous namespace" "" { target c++98_only } .-1 } > bad * B[1]; > }; > -struct b3 { // { dg-warning "uses the anonymous namespace" } > +struct b3 { // { dg-warning "has internal linkage" "" { target c++11 } } > +// { dg-warning "uses the anonymous namespace" "" { target c++98_only } .-1 } > bad (*B)[1]; > }; > diff --git a/gcc/testsuite/g++.dg/warn/anonymous-namespace-3.C b/gcc/testsuite/g++.dg/warn/anonymous-namespace-3.C > index 8b72abdf5d1..ce5745b25f0 100644 > --- a/gcc/testsuite/g++.dg/warn/anonymous-namespace-3.C > +++ b/gcc/testsuite/g++.dg/warn/anonymous-namespace-3.C > @@ -7,7 +7,8 @@ > struct B { std::auto_ptr p; }; > > #line 10 "foo.C" > -struct C // { dg-warning "uses the anonymous namespace" } > +struct C // { dg-warning "has internal linkage" "" { target c++11 } } > +// { dg-warning "uses the anonymous namespace" "" { target c++98_only } .-1 } > { > std::auto_ptr p; > };