From patchwork Thu Jun 30 16:53:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 55611 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C059538418AD for ; Thu, 30 Jun 2022 16:53:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C059538418AD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1656608038; bh=GJ82ZGev9sPRPDC2I4ZIqrx4pwyCU8iUks/TKSRNRCA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=KYRH44zF7C7VQiaybs1K7j/RYC6cJJCIbpNBDQICiBmTwYeyZ2nLfpm9IWmOx6atZ kRLienRyw5JcgHWJZPTRNssrcKiLa7VV9kw+YxncVT45yEtnC+pWsl9eD1RFIuEMO8 5BEI3bmlm7eoncTUf5TzcvmvC5hWOTado8PrbnUI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 62D373857367 for ; Thu, 30 Jun 2022 16:53:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 62D373857367 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-526-g6XzMOvKOhekUi0MC0hYow-1; Thu, 30 Jun 2022 12:53:28 -0400 X-MC-Unique: g6XzMOvKOhekUi0MC0hYow-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 631AF3C025D2 for ; Thu, 30 Jun 2022 16:53:27 +0000 (UTC) Received: from localhost (unknown [10.33.36.212]) by smtp.corp.redhat.com (Postfix) with ESMTP id 293D540E6A41; Thu, 30 Jun 2022 16:53:27 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Refer to internal linkage for -Wsubobject-linkage [PR86491] Date: Thu, 30 Jun 2022 17:53:26 +0100 Message-Id: <20220630165326.1253733-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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. 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; };