diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2022-06-30 17:53:26 +0100 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2022-09-12 12:09:44 -0400 |
commit | 8ef5fa4c56c82dfbd6e8fc5e4e08c4be843abc3e (patch) | |
tree | c1b43b01f3079e3a6f0667338a1152cfc80b0d4d /gcc | |
parent | 2e7bc76d84f0aa2f08bc01407ffcdca43c15f2ff (diff) | |
download | gcc-8ef5fa4c56c82dfbd6e8fc5e4e08c4be843abc3e.zip gcc-8ef5fa4c56c82dfbd6e8fc5e4e08c4be843abc3e.tar.gz gcc-8ef5fa4c56c82dfbd6e8fc5e4e08c4be843abc3e.tar.bz2 |
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/cp-tree.h | 3 | ||||
-rw-r--r-- | gcc/cp/decl2.cc | 39 | ||||
-rw-r--r-- | gcc/cp/name-lookup.cc | 2 | ||||
-rw-r--r-- | gcc/cp/pt.cc | 2 | ||||
-rw-r--r-- | gcc/cp/tree.cc | 12 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wsubobject-linkage-5.C | 7 | ||||
-rw-r--r-- | 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 7b28405..e73d04f 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 cd18881..684a2d0 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 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 diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index f89a1dc..69d555d 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 c5fc0f1..ad9c2f9 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 c678e3b..6de208a 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 0000000..e2c2fd9 --- /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 8b72abd..ce5745b 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; }; |