On 3/19/26 8:02 AM, Nathaniel Shead wrote:
Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk?

OK.

-- >8 --

This is a minimal fix to not just silently drop the section attribute on
stream-in.  Ideally we should be using cplus_decl_attributes for this,
but it's not yet ready for modules (many attributes behave weirdly when
applied not during parsing).  We should also be checking for
incompatible sections on stream-in in is_matching_decl, but that would
also be better handled more generally so I'm leaving that out of this
patch.

Instead this minimally fixes the issue by just re-applying the section
name from the attributes we've streamed in.

        PR c++/122786

gcc/cp/ChangeLog:

        * module.cc (trees_in::decl_value): Set section name from
        attribute.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Nathaniel Shead <[email protected]>
---
  gcc/cp/module.cc                          | 11 +++++++++++
  gcc/testsuite/g++.dg/modules/attrib-5_a.C | 12 ++++++++++++
  gcc/testsuite/g++.dg/modules/attrib-5_b.C | 13 +++++++++++++
  3 files changed, 36 insertions(+)
  create mode 100644 gcc/testsuite/g++.dg/modules/attrib-5_a.C
  create mode 100644 gcc/testsuite/g++.dg/modules/attrib-5_b.C

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 566563272cd..78f9f3f4d41 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -9031,6 +9031,17 @@ trees_in::decl_value ()
          && decl_tls_wrapper_p (decl))
        note_vague_linkage_fn (decl);
+ /* Apply relevant attributes.
+        FIXME should probably use cplus_decl_attributes for this,
+        but it's not yet ready for modules.  */
+
+      if (VAR_OR_FUNCTION_DECL_P (inner))
+       if (tree attr = lookup_attribute ("section", DECL_ATTRIBUTES (inner)))
+         {
+           tree section_name = TREE_VALUE (TREE_VALUE (attr));
+           set_decl_section_name (inner, TREE_STRING_POINTER (section_name));
+         }
+
        /* Setup aliases for the declaration.  */
        if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
        {
diff --git a/gcc/testsuite/g++.dg/modules/attrib-5_a.C 
b/gcc/testsuite/g++.dg/modules/attrib-5_a.C
new file mode 100644
index 00000000000..5c0cac4f51d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/attrib-5_a.C
@@ -0,0 +1,12 @@
+// PR c++/122786
+// { dg-additional-options "-fmodules" }
+// { dg-require-effective-target named_sections }
+// { dg-module-cmi M }
+
+export module M;
+
+export inline void a() __attribute__((section("barbar")));
+inline void a() {}
+
+export [[gnu::section("quxqux")]] inline void b();
+inline void b() {}
diff --git a/gcc/testsuite/g++.dg/modules/attrib-5_b.C 
b/gcc/testsuite/g++.dg/modules/attrib-5_b.C
new file mode 100644
index 00000000000..31ece58f9f3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/attrib-5_b.C
@@ -0,0 +1,13 @@
+// PR c++/122786
+// { dg-additional-options "-fmodules" }
+// { dg-require-effective-target named_sections }
+
+import M;
+
+int main() {
+  a();
+  b();
+}
+
+// { dg-final { scan-assembler {barbar} } }
+// { dg-final { scan-assembler {quxqux} } }

Reply via email to