public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Refer to internal linkage for -Wsubobject-linkage [PR86491]
@ 2022-06-30 16:53 Jonathan Wakely
  2022-07-22 11:20 ` Jonathan Wakely
  2022-07-22 21:30 ` Jason Merrill
  0 siblings, 2 replies; 4+ messages in thread
From: Jonathan Wakely @ 2022-06-30 16:53 UTC (permalink / raw)
  To: 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<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;
 };
-- 
2.36.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-09-12 16:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <c1215d18-73b0-95aa-2c02-485536696a48@redhat.com>
2022-09-12 16:10 ` [PATCH] c++: Refer to internal linkage for -Wsubobject-linkage [PR86491] Jason Merrill
2022-06-30 16:53 Jonathan Wakely
2022-07-22 11:20 ` Jonathan Wakely
2022-07-22 21:30 ` Jason Merrill

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).