public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-2627] c++: Refer to internal linkage for -Wsubobject-linkage [PR86491]
Date: Mon, 12 Sep 2022 16:09:56 +0000 (GMT)	[thread overview]
Message-ID: <20220912160956.82D2C38515C3@sourceware.org> (raw)

https://gcc.gnu.org/g:8ef5fa4c56c82dfbd6e8fc5e4e08c4be843abc3e

commit r13-2627-g8ef5fa4c56c82dfbd6e8fc5e4e08c4be843abc3e
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Jun 30 17:53:26 2022 +0100

    c++: Refer to internal linkage for -Wsubobject-linkage [PR86491]
    
    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, if the type isn't actually a member of the anonymous namespace.
    Making that distinction involved renaming the current decl_anon_ns_mem_p to
    something that better expresses its semantics.
    
    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 for cases where anonymous
            namespaces aren't used.
            * tree.cc (decl_anon_ns_mem_p): Now only true for actual anonymous
            namespace members, rename old semantics to...
            (decl_internal_context_p): ...this.
            * cp-tree.h, name-lookup.cc, pt.cc: Adjust.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/warn/anonymous-namespace-3.C: Use separate dg-warning
            directives for C++98 and everything else.
            * g++.dg/warn/Wsubobject-linkage-5.C: New test.

Diff:
---
 gcc/cp/cp-tree.h                                  |  3 +-
 gcc/cp/decl2.cc                                   | 39 +++++++++++++++--------
 gcc/cp/name-lookup.cc                             |  2 +-
 gcc/cp/pt.cc                                      |  2 +-
 gcc/cp/tree.cc                                    | 12 +++++--
 gcc/testsuite/g++.dg/warn/Wsubobject-linkage-5.C  |  7 ++++
 gcc/testsuite/g++.dg/warn/anonymous-namespace-3.C |  3 +-
 7 files changed, 48 insertions(+), 20 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 7b28405c3ac..e73d04f21d8 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7874,7 +7874,8 @@ extern tree replace_placeholders		(tree, tree, bool * = NULL);
 extern bool find_placeholders			(tree);
 extern tree get_type_decl			(tree);
 extern tree decl_namespace_context		(tree);
-extern bool decl_anon_ns_mem_p			(const_tree);
+extern bool decl_anon_ns_mem_p			(tree);
+extern bool decl_internal_context_p		(const_tree);
 extern tree lvalue_type				(tree);
 extern tree error_type				(tree);
 extern int varargs_function_p			(const_tree);
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index cd188813bee..684a2d06dde 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -2851,7 +2851,7 @@ determine_visibility (tree decl)
   if (class_type)
     determine_visibility_from_class (decl, class_type);
 
-  if (decl_anon_ns_mem_p (decl))
+  if (decl_internal_context_p (decl))
     /* Names in an anonymous namespace get internal linkage.  */
     constrain_visibility (decl, VISIBILITY_ANON, false);
   else if (TREE_CODE (decl) != TYPE_DECL)
@@ -2965,16 +2965,21 @@ constrain_class_visibility (tree type)
 		  {
 		    if (same_type_p (TREE_TYPE (t), nlt))
 		      warning (OPT_Wsubobject_linkage, "\
-%qT has a field %qD whose type has no linkage",
+%qT has a field %q#D 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
+		else if (cxx_dialect > cxx98
+			 && !decl_anon_ns_mem_p (ftype))
 		  warning (OPT_Wsubobject_linkage, "\
-%qT has a field %qD whose type uses the anonymous namespace",
+%qT has a field %q#D 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 %q#D whose type uses the anonymous namespace",
 			   type, t);
 	      }
 	  }
@@ -2989,28 +2994,34 @@ constrain_class_visibility (tree type)
   binfo = TYPE_BINFO (type);
   for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
     {
-      int subvis = type_visibility (TREE_TYPE (t));
+      tree btype = BINFO_TYPE (t);
+      int subvis = type_visibility (btype);
 
       if (subvis == VISIBILITY_ANON)
         {
 	  if (!in_main_input_context())
 	    {
-	      tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
+	      tree nlt = no_linkage_check (btype, /*relaxed_p=*/false);
 	      if (nlt)
 		{
-		  if (same_type_p (TREE_TYPE (t), nlt))
+		  if (same_type_p (btype, nlt))
 		    warning (OPT_Wsubobject_linkage, "\
-%qT has a base %qT whose type has no linkage",
-			     type, TREE_TYPE (t));
+%qT has a base %qT which has no linkage",
+			     type, btype);
 		  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);
+%qT has a base %qT which depends on the type %qT which has no linkage",
+			     type, btype, nlt);
 		}
-	      else
+	      else if (cxx_dialect > cxx98
+		       && !decl_anon_ns_mem_p (btype))
 		warning (OPT_Wsubobject_linkage, "\
-%qT has a base %qT whose type uses the anonymous namespace",
-			 type, TREE_TYPE (t));
+%qT has a base %qT which has internal linkage",
+			 type, btype);
+	      else // In C++98 this can only happen with unnamed namespaces.
+		warning (OPT_Wsubobject_linkage, "\
+%qT has a base %qT which uses the anonymous namespace",
+			 type, btype);
 	    }
 	}
       else if (vis < VISIBILITY_HIDDEN
diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index f89a1dc4111..69d555ddf1f 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -402,7 +402,7 @@ add_decl_to_level (cp_binding_level *b, tree decl)
       && ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
 	  || (TREE_CODE (decl) == FUNCTION_DECL
 	      && (!TREE_PUBLIC (decl)
-		  || decl_anon_ns_mem_p (decl)
+		  || decl_internal_context_p (decl)
 		  || DECL_DECLARED_INLINE_P (decl)))))
     vec_safe_push (static_decls, decl);
 }
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index c5fc0f1eab8..ad9c2f9b180 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -25025,7 +25025,7 @@ mark_decl_instantiated (tree result, int extern_p)
     return;
 
   /* For anonymous namespace we don't need to do anything.  */
-  if (decl_anon_ns_mem_p (result))
+  if (decl_internal_context_p (result))
     {
       gcc_assert (!TREE_PUBLIC (result));
       return;
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index c678e3b9c4c..6de208a909b 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -2968,7 +2968,7 @@ verify_stmt_tree (tree t)
 /* Check if the type T depends on a type with no linkage and if so,
    return it.  If RELAXED_P then do not consider a class type declared
    within a vague-linkage function to have no linkage.  Remember:
-   no-linkage is not the same as internal-linkage*/
+   no-linkage is not the same as internal-linkage.  */
 
 tree
 no_linkage_check (tree t, bool relaxed_p)
@@ -3817,7 +3817,15 @@ decl_namespace_context (tree decl)
    nested, or false otherwise.  */
 
 bool
-decl_anon_ns_mem_p (const_tree decl)
+decl_anon_ns_mem_p (tree decl)
+{
+  return !TREE_PUBLIC (decl_namespace_context (decl));
+}
+
+/* Returns true if the enclosing scope of DECL has internal or no linkage.  */
+
+bool
+decl_internal_context_p (const_tree decl)
 {
   while (TREE_CODE (decl) != NAMESPACE_DECL)
     {
diff --git a/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-5.C b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-5.C
new file mode 100644
index 00000000000..e2c2fd91688
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-5.C
@@ -0,0 +1,7 @@
+// PR c++/86491
+// { dg-do compile { target c++11 } }
+
+template <int *> struct NT{};
+#line 6 "tM.C"
+static int d;
+struct D : NT<&d> {};		// { dg-warning "internal linkage" }
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;
 };

                 reply	other threads:[~2022-09-12 16:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220912160956.82D2C38515C3@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).