]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Set DECL_CONTEXT for __cxa_thread_atexit [PR99187]
authorNathaniel Shead <nathanieloshead@gmail.com>
Thu, 16 Nov 2023 21:39:53 +0000 (08:39 +1100)
committerNathan Sidwell <nathan@acm.org>
Sun, 19 Nov 2023 21:43:01 +0000 (16:43 -0500)
Modules streaming requires DECL_CONTEXT to be set on declarations that
are streamed. This ensures that __cxa_thread_atexit is given translation
unit context much like is already done with many other support
functions.

PR c++/99187

gcc/cp/ChangeLog:

* cp-tree.h (enum cp_tree_index): Add CPTI_THREAD_ATEXIT.
(thread_atexit_node): New.
* decl.cc (get_thread_atexit_node): Cache in thread_atexit_node.

gcc/testsuite/ChangeLog:

* g++.dg/modules/pr99187.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Signed-off-by: Nathan Sidwell <nathan@acm.org>
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/testsuite/g++.dg/modules/pr99187.C [new file with mode: 0644]

index 1fa710d71543d226bc37efe0a21eb4e5568192ba..c7a1cf610c815096a9bf41df30db6c4c1105af47 100644 (file)
@@ -231,6 +231,7 @@ enum cp_tree_index
     CPTI_RETHROW_FN,
     CPTI_ATEXIT_FN_PTR_TYPE,
     CPTI_ATEXIT,
+    CPTI_THREAD_ATEXIT,
     CPTI_DSO_HANDLE,
     CPTI_DCAST,
 
@@ -375,6 +376,9 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
 /* A pointer to `std::atexit'.  */
 #define atexit_node                    cp_global_trees[CPTI_ATEXIT]
 
+/* A pointer to `__cxa_thread_atexit'.  */
+#define thread_atexit_node             cp_global_trees[CPTI_THREAD_ATEXIT]
+
 /* A pointer to `__dso_handle'.  */
 #define dso_handle_node                        cp_global_trees[CPTI_DSO_HANDLE]
 
index b8e1098d482250b4e11c863d0cb39b5807e37e44..e6f75d771e09aec5b922b35a728f9134c209a156 100644 (file)
@@ -9593,6 +9593,9 @@ get_atexit_node (void)
 static tree
 get_thread_atexit_node (void)
 {
+  if (thread_atexit_node)
+    return thread_atexit_node;
+
   /* The declaration for `__cxa_thread_atexit' is:
 
      int __cxa_thread_atexit (void (*)(void *), void *, void *) */
@@ -9601,10 +9604,18 @@ get_thread_atexit_node (void)
                                           ptr_type_node, ptr_type_node,
                                           NULL_TREE);
 
-  /* Now, build the function declaration.  */
+  /* Now, build the function declaration, as with __cxa_atexit.  */
+  unsigned flags = push_abi_namespace ();
   tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
                                             ECF_LEAF | ECF_NOTHROW);
-  return decay_conversion (atexit_fndecl, tf_warning_or_error);
+  DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
+  DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
+  atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
+  pop_abi_namespace (flags);
+  mark_used (atexit_fndecl);
+  thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
+
+  return thread_atexit_node;
 }
 
 /* Returns the __dso_handle VAR_DECL.  */
diff --git a/gcc/testsuite/g++.dg/modules/pr99187.C b/gcc/testsuite/g++.dg/modules/pr99187.C
new file mode 100644 (file)
index 0000000..7f707e0
--- /dev/null
@@ -0,0 +1,10 @@
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi pr99187 }
+
+export module pr99187;
+
+export struct A { ~A() {} };
+
+export inline void f() {
+  static thread_local A a;
+}