]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/modules: Fix handling of -fdeclone-ctor-dtor with explicit instantiations [PR120125]
authorNathaniel Shead <nathanieloshead@gmail.com>
Thu, 8 May 2025 13:06:13 +0000 (23:06 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Wed, 14 May 2025 09:26:24 +0000 (19:26 +1000)
The attached testcase ICEs in maybe_thunk_body because we haven't
created a node in the cgraph for an imported explicit instantiation yet.

We in fact really shouldn't be emitting calls at all, since an imported
explicit instantiation always exists in the TU we imported it from.  So
this patch adjusts DECL_NOT_REALLY_EXTERN handling to account for this.

PR c++/120125

gcc/cp/ChangeLog:

* module.cc (trees_out::write_function_def): Only set
DECL_NOT_REALLY_EXTERN if the importer might need to emit it.
* optimize.cc (maybe_thunk_body): Don't assume 'fn' has a cgraph
node created.

gcc/testsuite/ChangeLog:

* g++.dg/modules/clone-4_a.C: New test.
* g++.dg/modules/clone-4_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/module.cc
gcc/cp/optimize.cc
gcc/testsuite/g++.dg/modules/clone-4_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/clone-4_b.C [new file with mode: 0644]

index f562bf8cd9158498a83b70035b8261047e882a44..e7782627a492e692d0745f30169eea9bbc5c0d62 100644 (file)
@@ -12638,7 +12638,11 @@ trees_out::write_function_def (tree decl)
     {
       unsigned flags = 0;
 
-      flags |= 1 * DECL_NOT_REALLY_EXTERN (decl);
+      /* Whether the importer should emit this definition, if used.  */
+      flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
+                   && (get_importer_interface (decl)
+                       != importer_interface::always_import));
+
       if (f)
        {
          flags |= 2;
index 6f9a77f407a33c79eceef9d84fa3b5f2df457cfb..fc4d6c2e3514d3d956b9d77947035c90380e060b 100644 (file)
@@ -309,8 +309,8 @@ maybe_thunk_body (tree fn, bool force)
       defer_mangling_aliases = save_defer_mangling_aliases;
       cgraph_node::get_create (fns[0])->set_comdat_group (comdat_group);
       cgraph_node::get_create (fns[1])->add_to_same_comdat_group
-       (cgraph_node::get_create (fns[0]));
-      symtab_node::get (fn)->add_to_same_comdat_group
+       (cgraph_node::get (fns[0]));
+      symtab_node::get_create (fn)->add_to_same_comdat_group
        (symtab_node::get (fns[0]));
       if (fns[2])
        /* If *[CD][12]* dtors go into the *[CD]5* comdat group and dtor is
diff --git a/gcc/testsuite/g++.dg/modules/clone-4_a.C b/gcc/testsuite/g++.dg/modules/clone-4_a.C
new file mode 100644 (file)
index 0000000..3ee6109
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/120125
+// { dg-additional-options "-fmodules -fdeclone-ctor-dtor" }
+// { dg-module-cmi M }
+
+export module M;
+
+void foo();
+export template <typename _Tp> struct __shared_ptr {
+  inline __shared_ptr() { foo(); }
+};
+
+template class __shared_ptr<int>;
diff --git a/gcc/testsuite/g++.dg/modules/clone-4_b.C b/gcc/testsuite/g++.dg/modules/clone-4_b.C
new file mode 100644 (file)
index 0000000..1b36cb4
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/120125
+// { dg-additional-options "-fmodules -fdeclone-ctor-dtor" }
+
+import M;
+
+int main() {
+  __shared_ptr<int> s1;
+  __shared_ptr<double> s2;
+}
+
+// { dg-final { scan-assembler-not {_ZNW1M12__shared_ptrIiEC[1-4]Ev:} } }
+// { dg-final { scan-assembler {_ZNW1M12__shared_ptrIdEC2Ev:} } }