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

OK.

-- >8 --

The [[maybe_unused]] attribute works by marking relevant entities as
TREE_USED, and DECL_READ_P for vars and parms.  But modules streaming
typically clears this flag unless reading a definition.  This patch sets
it back again when we see this attribute applied to a parameter.

        PR c++/124483

gcc/cp/ChangeLog:

        * module.cc (trees_in::fn_parms_init): Set TREE_USED and
        DECL_READ_P for parameters deliberately marked unused.

gcc/testsuite/ChangeLog:

        * g++.dg/modules/attrib-4_a.C: New test.
        * g++.dg/modules/attrib-4_b.C: New test.
---
  gcc/cp/module.cc                          | 13 +++++++++++++
  gcc/testsuite/g++.dg/modules/attrib-4_a.C |  8 ++++++++
  gcc/testsuite/g++.dg/modules/attrib-4_b.C |  8 ++++++++
  3 files changed, 29 insertions(+)
  create mode 100644 gcc/testsuite/g++.dg/modules/attrib-4_a.C
  create mode 100644 gcc/testsuite/g++.dg/modules/attrib-4_b.C

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 83d24a3267d..566563272cd 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -11358,6 +11358,19 @@ trees_in::fn_parms_init (tree fn)
                 base_tag - ix, ix, parm, fn);
        if (!tree_node_vals (parm))
        return 0;
+
+      /* Apply relevant attributes.
+        FIXME should probably use cplus_decl_attributes for this,
+        but it's not yet ready for modules.  */
+
+      /* TREE_USED is deliberately not streamed for most declarations,
+        but needs to be set if we have the [[maybe_unused]] attribute.  */
+      if (lookup_attribute ("unused", DECL_ATTRIBUTES (parm))
+         || lookup_attribute ("maybe_unused", DECL_ATTRIBUTES (parm)))
+       {
+         TREE_USED (parm) = true;
+         DECL_READ_P (parm) = true;
+       }
      }
/* Reload references to contract functions, if any. */
diff --git a/gcc/testsuite/g++.dg/modules/attrib-4_a.C 
b/gcc/testsuite/g++.dg/modules/attrib-4_a.C
new file mode 100644
index 00000000000..a77e9af5d9d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/attrib-4_a.C
@@ -0,0 +1,8 @@
+// PR c++/124483
+// { dg-additional-options "-fmodules -Wunused-parameter" }
+// { dg-module-cmi B }
+
+export module B;
+
+export template <typename T = void>
+void foo([[maybe_unused]] int x) {}
diff --git a/gcc/testsuite/g++.dg/modules/attrib-4_b.C 
b/gcc/testsuite/g++.dg/modules/attrib-4_b.C
new file mode 100644
index 00000000000..a812559edf3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/attrib-4_b.C
@@ -0,0 +1,8 @@
+// PR c++/124483
+// { dg-additional-options "-fmodules -Wunused-parameter" }
+
+import B;
+
+int main() {
+  foo(123);
+}

Reply via email to