public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Jonathan Wakely <jwakely@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: Refer to internal linkage for -Wsubobject-linkage [PR86491]
Date: Fri, 22 Jul 2022 17:30:13 -0400	[thread overview]
Message-ID: <2d5773f2-4d92-37bb-93d0-f744e03571b6@redhat.com> (raw)
In-Reply-To: <20220630165326.1253733-1-jwakely@redhat.com>

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<A> 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<A> p;
>   };


  parent reply	other threads:[~2022-07-22 21:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-30 16:53 Jonathan Wakely
2022-07-22 11:20 ` Jonathan Wakely
2022-07-22 21:30 ` Jason Merrill [this message]
     [not found] <c1215d18-73b0-95aa-2c02-485536696a48@redhat.com>
2022-09-12 16:10 ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2d5773f2-4d92-37bb-93d0-f744e03571b6@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).