aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Palka <ppalka@redhat.com>2024-03-07 16:23:22 -0500
committerPatrick Palka <ppalka@redhat.com>2024-03-07 16:23:22 -0500
commit0552560f6d2eaa1ae6df5c80660b489de1d5c772 (patch)
treeede33f7b2df70be01d730a4870daa3a96f24c79d
parente162b2ff52c5e20f6624ff6b66845fe573cef183 (diff)
downloadgcc-0552560f6d2eaa1ae6df5c80660b489de1d5c772.zip
gcc-0552560f6d2eaa1ae6df5c80660b489de1d5c772.tar.gz
gcc-0552560f6d2eaa1ae6df5c80660b489de1d5c772.tar.bz2
c++/modules: inline namespace abi_tag streaming [PR110730]
The unreduced testcase from PR110730 crashes at runtime ultimately because we don't stream the abi_tag attribute on inline namespaces and so the filesystem::current_path() call resolves to the non-C++11 ABI version even though the C++11 ABI is active, leading to a crash when destroying the path temporary (which contains an std::string member). Similar story for the PR105512 testcase. While we do stream the DECL_ATTRIBUTES of all decls that go through the generic tree streaming routines, it seems namespaces are streamed separately from other decls and we don't use the generic routines for them. So this patch makes us stream the abi_tag manually for (inline) namespaces. PR c++/110730 PR c++/105512 gcc/cp/ChangeLog: * module.cc (module_state::write_namespaces): Stream the abi_tag attribute of an inline namespace. (module_state::read_namespaces): Likewise. gcc/testsuite/ChangeLog: * g++.dg/modules/hello-2_a.C: New test. * g++.dg/modules/hello-2_b.C: New test. * g++.dg/modules/namespace-6_a.H: New test. * g++.dg/modules/namespace-6_b.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
-rw-r--r--gcc/cp/module.cc28
-rw-r--r--gcc/testsuite/g++.dg/modules/hello-2_a.C11
-rw-r--r--gcc/testsuite/g++.dg/modules/hello-2_b.C10
-rw-r--r--gcc/testsuite/g++.dg/modules/namespace-6_a.H10
-rw-r--r--gcc/testsuite/g++.dg/modules/namespace-6_b.C7
5 files changed, 66 insertions, 0 deletions
diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index f7e8b35..d52db76 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -15276,6 +15276,19 @@ module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
sec.u (flags);
write_location (sec, DECL_SOURCE_LOCATION (ns));
+
+ if (DECL_NAMESPACE_INLINE_P (ns))
+ {
+ if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
+ {
+ tree tags = TREE_VALUE (attr);
+ sec.u (list_length (tags));
+ for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
+ sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
+ }
+ else
+ sec.u (0);
+ }
}
sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
@@ -15306,11 +15319,22 @@ module_state::read_namespaces (unsigned num)
/* See comment in write_namespace about why not bits. */
unsigned flags = sec.u ();
location_t src_loc = read_location (sec);
+ unsigned tags_count = (flags & 2) ? sec.u () : 0;
if (entity_index >= entity_num
|| !parent
|| (flags & 0xc) == 0x8)
sec.set_overrun ();
+
+ tree tags = NULL_TREE;
+ while (tags_count--)
+ {
+ size_t len;
+ const char *str = sec.str (&len);
+ tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
+ tags = nreverse (tags);
+ }
+
if (sec.get_overrun ())
break;
@@ -15342,6 +15366,10 @@ module_state::read_namespaces (unsigned num)
DECL_MODULE_EXPORT_P (inner) = true;
}
+ if (tags)
+ DECL_ATTRIBUTES (inner)
+ = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
+
/* Install the namespace. */
(*entity_ary)[entity_lwm + entity_index] = inner;
if (DECL_MODULE_IMPORT_P (inner))
diff --git a/gcc/testsuite/g++.dg/modules/hello-2_a.C b/gcc/testsuite/g++.dg/modules/hello-2_a.C
new file mode 100644
index 0000000..a8f8b81
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/hello-2_a.C
@@ -0,0 +1,11 @@
+// PR c++/105512
+// { dg-additional-options -fmodules-ts }
+// { dg-module-cmi Hello2 }
+
+module;
+#include <string>
+export module Hello2;
+
+export std::string tester() {
+ return "hello world\n";
+}
diff --git a/gcc/testsuite/g++.dg/modules/hello-2_b.C b/gcc/testsuite/g++.dg/modules/hello-2_b.C
new file mode 100644
index 0000000..dafd3c2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/hello-2_b.C
@@ -0,0 +1,10 @@
+// PR c++/105512
+// { dg-additional-options -fmodules-ts }
+// { dg-module-do run }
+
+#include <iostream>
+import Hello2;
+
+int main() {
+ std::cout << tester();
+}
diff --git a/gcc/testsuite/g++.dg/modules/namespace-6_a.H b/gcc/testsuite/g++.dg/modules/namespace-6_a.H
new file mode 100644
index 0000000..b412cbe
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/namespace-6_a.H
@@ -0,0 +1,10 @@
+// PR c++/110730
+// { dg-additional-options -fmodule-header }
+// { dg-module-cmi {} }
+
+namespace std::filesystem {
+ inline namespace __cxx11 __attribute__((__abi_tag__("cxx11", "foo"))) {
+ struct path { };
+ }
+ inline path current_path() { return {}; };
+}
diff --git a/gcc/testsuite/g++.dg/modules/namespace-6_b.C b/gcc/testsuite/g++.dg/modules/namespace-6_b.C
new file mode 100644
index 0000000..2ce41c7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/namespace-6_b.C
@@ -0,0 +1,7 @@
+// PR c++/110730
+// { dg-additional-options -fmodules-ts }
+
+import "namespace-6_a.H";
+
+int main() { std::filesystem::current_path(); }
+// { dg-final { scan-assembler _ZNSt10filesystem12current_pathB5cxx11B3fooEv } }