]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: modules and function attributes
authorJason Merrill <jason@redhat.com>
Fri, 10 Jan 2025 23:00:20 +0000 (18:00 -0500)
committerJason Merrill <jason@redhat.com>
Sat, 11 Jan 2025 06:27:11 +0000 (01:27 -0500)
30_threads/stop_token/stop_source/109339.cc was failing because we weren't
representing attribute access on the METHOD_TYPE for _Stop_state_ref.

The modules code expected attributes to appear on tt_variant_type and not
on tt_derived_type, but that's backwards since build_type_attribute_variant
gives a type with attributes its own TYPE_MAIN_VARIANT.

gcc/cp/ChangeLog:

* module.cc (trees_out::type_node): Write attributes for
tt_derived_type, not tt_variant_type.
(trees_in::tree_node): Likewise for reading.

gcc/testsuite/ChangeLog:

* g++.dg/modules/attrib-2_a.C: New test.
* g++.dg/modules/attrib-2_b.C: New test.

gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/attrib-2_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/attrib-2_b.C [new file with mode: 0644]

index 321d4164a6a78d43cbad3e753433e75d74c59cb6..c932c4d0a90daa00a1dc4ac606ec1b6b4fad7b0c 100644 (file)
@@ -9189,7 +9189,10 @@ trees_out::type_node (tree type)
          tree_node (raises);
        }
 
-      tree_node (TYPE_ATTRIBUTES (type));
+      /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
+        variants should all have the same set of attributes.  */
+      gcc_checking_assert (TYPE_ATTRIBUTES (type)
+                          == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
 
       if (streaming_p ())
        {
@@ -9406,6 +9409,8 @@ trees_out::type_node (tree type)
       break;
     }
 
+  tree_node (TYPE_ATTRIBUTES (type));
+
   /* We may have met the type during emitting the above.  */
   if (ref_node (type) != WK_none)
     {
@@ -10090,6 +10095,13 @@ trees_in::tree_node (bool is_use)
            break;
          }
 
+       /* In the exporting TU, a derived type with attributes was built by
+          build_type_attribute_variant as a distinct copy, with itself as
+          TYPE_MAIN_VARIANT.  We repeat that on import to get the version
+          without attributes as TYPE_CANONICAL.  */
+       if (tree attribs = tree_node ())
+         res = cp_build_type_attribute_variant (res, attribs);
+
        int tag = i ();
        if (!tag)
          {
@@ -10133,9 +10145,6 @@ trees_in::tree_node (bool is_use)
            TYPE_USER_ALIGN (res) = true;
          }
 
-       if (tree attribs = tree_node ())
-         res = cp_build_type_attribute_variant (res, attribs);
-
        int quals = i ();
        if (quals >= 0 && !get_overrun ())
          res = cp_build_qualified_type (res, quals);
diff --git a/gcc/testsuite/g++.dg/modules/attrib-2_a.C b/gcc/testsuite/g++.dg/modules/attrib-2_a.C
new file mode 100644 (file)
index 0000000..96f667c
--- /dev/null
@@ -0,0 +1,12 @@
+// { dg-additional-options "-fmodules -Wno-global-module" }
+// { dg-module-cmi M }
+
+export module M;
+
+export
+{
+  struct A { int i; };
+
+  __attribute ((access (none, 1)))
+  void f(const A&);
+}
diff --git a/gcc/testsuite/g++.dg/modules/attrib-2_b.C b/gcc/testsuite/g++.dg/modules/attrib-2_b.C
new file mode 100644 (file)
index 0000000..c12ad11
--- /dev/null
@@ -0,0 +1,9 @@
+// { dg-additional-options "-fmodules -Wmaybe-uninitialized" }
+
+import M;
+
+int main()
+{
+  A a;
+  f(a);
+}