]> gcc.gnu.org Git - gcc.git/commitdiff
c++: Refer to internal linkage for -Wsubobject-linkage [PR86491]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 30 Jun 2022 16:53:26 +0000 (17:53 +0100)
committerJason Merrill <jason@redhat.com>
Mon, 12 Sep 2022 16:09:44 +0000 (12:09 -0400)
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.

gcc/cp/cp-tree.h
gcc/cp/decl2.cc
gcc/cp/name-lookup.cc
gcc/cp/pt.cc
gcc/cp/tree.cc
gcc/testsuite/g++.dg/warn/Wsubobject-linkage-5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/anonymous-namespace-3.C

index 7b28405c3acae3385a887ebd0e74d8c24a8cc4d7..e73d04f21d8fa4ccc68111803fb9342c9443843b 100644 (file)
@@ -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);
index cd188813beeb58f0558e695a2b75d175fe0a5443..684a2d06ddecb51fe942dbac9303c4e8c22ccb9e 100644 (file)
@@ -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 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 whose type uses the anonymous namespace",
-                        type, TREE_TYPE (t));
+%qT has a base %qT which uses the anonymous namespace",
+                        type, btype);
            }
        }
       else if (vis < VISIBILITY_HIDDEN
index f89a1dc411182af8b1a809ed2d76d53e4111aa48..69d555ddf1f31b28ead988fb0662757539942d67 100644 (file)
@@ -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);
 }
index c5fc0f1eab8db1b2e641477b99084dd48e83eaff..ad9c2f9b18082eed0e46afac464722a73387bfaf 100644 (file)
@@ -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;
index c678e3b9c4c220a5542fc94ddc00807ffe19279b..6de208a909b15df8579725db8bf2744308c17959 100644 (file)
@@ -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 (file)
index 0000000..e2c2fd9
--- /dev/null
@@ -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" }
index 8b72abdf5d1dfaecf179ddddd5d9885e12eb074b..ce5745b25f0ccf1579feca16ae663ae503fe741a 100644 (file)
@@ -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;
 };
This page took 0.082857 seconds and 5 git commands to generate.