aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp
diff options
context:
space:
mode:
authorNathaniel Shead <nathanieloshead@gmail.com>2024-03-16 22:00:29 +1100
committerNathaniel Shead <nathanieloshead@gmail.com>2024-03-19 12:21:41 +1100
commitc4845edfeaf44756ad9672e8d143f1c8f5c4c0f6 (patch)
treea59c6a93508e213e51ae5f804f94578409dbb104 /gcc/cp
parent23409731ca4464471d4ef456d6aee50084d00c85 (diff)
downloadgcc-c4845edfeaf44756ad9672e8d143f1c8f5c4c0f6.zip
gcc-c4845edfeaf44756ad9672e8d143f1c8f5c4c0f6.tar.gz
gcc-c4845edfeaf44756ad9672e8d143f1c8f5c4c0f6.tar.bz2
c++: Fix handling of no-linkage decls for modules
When testing the changes for PR c++/112631 we discovered that currently we don't emit definitions of block-scope function declarations if they're not used in the module interface TU, which causes issues if they are used by importers. This patch fixes the handling of no-linkage declarations for C++20. In particular, a type declared in a function with vague linkage or declared in a module CMI could potentially be accessible outside its defining TU, and as such we can't assume that function declarations using that type can never be defined in another TU. A complication with handling this is that we're only strictly interested in declarations with a module CMI, but when parsing the global module fragment we don't yet know whether or not this module will have a CMI until we reach the "export module" line (or not). Since this case is IFNDR anyway (by [basic.def.odr] p11) we just tentatively assume while parsing the GMF that this module will have a CMI; once we see (or don't see) an 'export module' declaration we can commit to that knowledge for future declarations. gcc/cp/ChangeLog: * cp-tree.h (module_maybe_has_cmi_p): New function. * decl.cc (grokfndecl): Mark block-scope functions as public if they could be visible in other TUs. * decl2.cc (no_linkage_error): Don't error for declarations that could be defined in other TUs since C++20. Suppress duplicate errors from 'check_global_declaration'. * tree.cc (no_linkage_check): In relaxed mode, don't consider types in a module CMI to have no linkage. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/linkage-1.C: New test. * g++.dg/modules/block-decl-3.h: New test. * g++.dg/modules/block-decl-3_a.C: New test. * g++.dg/modules/block-decl-3_b.C: New test. * g++.dg/modules/block-decl-3_c.C: New test. * g++.dg/modules/linkage-1_a.C: New test. * g++.dg/modules/linkage-1_b.C: New test. * g++.dg/modules/linkage-1_c.C: New test. * g++.dg/modules/linkage-2.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Jason Merrill <jason@redhat.com>
Diffstat (limited to 'gcc/cp')
-rw-r--r--gcc/cp/cp-tree.h6
-rw-r--r--gcc/cp/decl.cc10
-rw-r--r--gcc/cp/decl2.cc39
-rw-r--r--gcc/cp/tree.cc21
4 files changed, 64 insertions, 12 deletions
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 0591386..52d5358 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7384,6 +7384,12 @@ inline bool named_module_purview_p ()
inline bool named_module_attach_p ()
{ return named_module_p () && module_attach_p (); }
+/* We don't know if this TU will have a CMI while parsing the GMF,
+ so tentatively assume that it might, for the purpose of determining
+ whether no-linkage decls could be used by an importer. */
+inline bool module_maybe_has_cmi_p ()
+{ return module_has_cmi_p () || (named_module_p () && !module_purview_p ()); }
+
/* We're currently exporting declarations. */
inline bool module_exporting_p ()
{ return module_kind & MK_EXPORTING; }
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 7a97b86..65ab648 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -10756,9 +10756,15 @@ grokfndecl (tree ctype,
/* Members of anonymous types and local classes have no linkage; make
them internal. If a typedef is made later, this will be changed. */
- if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
- || decl_function_context (TYPE_MAIN_DECL (ctype))))
+ if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
publicp = 0;
+ else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
+ /* But members of local classes in a module CMI should have their
+ definitions exported, in case they are (directly or indirectly)
+ used by an importer. We don't just use module_has_cmi_p here
+ because for entities in the GMF we don't yet know whether this
+ module will have a CMI, so we'll conservatively assume it might. */
+ publicp = module_maybe_has_cmi_p ();
if (publicp && cxx_dialect == cxx98)
{
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index 6c9fd41..2562d8a 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -4696,8 +4696,19 @@ no_linkage_error (tree decl)
bool d = false;
auto_diagnostic_group grp;
if (cxx_dialect >= cxx11)
- d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
- "unnamed type, is used but never defined", decl);
+ {
+ /* If t is declared in a module CMI, then decl could actually
+ be defined in a different TU, so don't warn since C++20. */
+ tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
+ if (relaxed != NULL_TREE)
+ d = permerror (DECL_SOURCE_LOCATION (decl),
+ "%q#D, declared using an unnamed type, "
+ "is used but never defined", decl);
+ else if (cxx_dialect < cxx20)
+ d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
+ "%q#D, declared using an unnamed type, "
+ "is used but not defined", decl);
+ }
else if (DECL_EXTERN_C_P (decl))
/* Allow this; it's pretty common in C. */;
else if (VAR_P (decl))
@@ -4716,13 +4727,31 @@ no_linkage_error (tree decl)
inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
"to the unqualified type, so it is not used for linkage",
TYPE_NAME (t));
+ /* Suppress warning from check_global_declaration if needed. */
+ if (d)
+ suppress_warning (decl, OPT_Wunused);
}
else if (cxx_dialect >= cxx11)
{
if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
- permerror (DECL_SOURCE_LOCATION (decl),
- "%q#D, declared using local type "
- "%qT, is used but never defined", decl, t);
+ {
+ /* Similarly for local types in a function with vague linkage or
+ defined in a module CMI, then decl could actually be defined
+ in a different TU, so don't warn since C++20. */
+ bool d = false;
+ tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
+ if (relaxed != NULL_TREE)
+ d = permerror (DECL_SOURCE_LOCATION (decl),
+ "%q#D, declared using local type "
+ "%qT, is used but never defined", decl, t);
+ else if (cxx_dialect < cxx20)
+ d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
+ "%q#D, declared using local type "
+ "%qT, is used but not defined here", decl, t);
+ /* Suppress warning from check_global_declaration if needed. */
+ if (d)
+ suppress_warning (decl, OPT_Wunused);
+ }
}
else if (VAR_P (decl))
warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index e75be9a..f1a23ff 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -2971,7 +2971,8 @@ 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:
+ within a vague-linkage function or in a module CMI to have no linkage,
+ since it can still be accessed within a different TU. Remember:
no-linkage is not the same as internal-linkage. */
tree
@@ -3012,7 +3013,15 @@ no_linkage_check (tree t, bool relaxed_p)
/* Only treat unnamed types as having no linkage if they're at
namespace scope. This is core issue 966. */
if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
- return t;
+ {
+ if (relaxed_p
+ && TREE_PUBLIC (CP_TYPE_CONTEXT (t))
+ && module_maybe_has_cmi_p ())
+ /* This type could possibly be accessed outside this TU. */
+ return NULL_TREE;
+ else
+ return t;
+ }
for (r = CP_TYPE_CONTEXT (t); ; )
{
@@ -3023,10 +3032,12 @@ no_linkage_check (tree t, bool relaxed_p)
return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
else if (TREE_CODE (r) == FUNCTION_DECL)
{
- if (!relaxed_p || !vague_linkage_p (r))
- return t;
- else
+ if (relaxed_p
+ && (vague_linkage_p (r)
+ || (TREE_PUBLIC (r) && module_maybe_has_cmi_p ())))
r = CP_DECL_CONTEXT (r);
+ else
+ return t;
}
else
break;