https://gcc.gnu.org/g:0661c5480c80bc40d9bc1cb15c3264d67c2efe9c

commit r16-8045-g0661c5480c80bc40d9bc1cb15c3264d67c2efe9c
Author: Nathaniel Shead <[email protected]>
Date:   Fri Mar 13 00:47:36 2026 +1100

    c++/modules: Support diagnostic classification changes in macros [PR124459]
    
    Diagnostic classification changes via _Pragma can occur in macros, but
    weren't taking effect in imported modules.  The issue is that we were
    reading the diagnostic classification map before we processed the macro
    maps, which meant that all locations in macros were just falling back
    to the abstract location of the module itself.  Fixed by moving the
    processing after we've read the macro maps.
    
            PR c++/124459
    
    gcc/cp/ChangeLog:
    
            * module.cc (module_state::read_initial): Move
            read_diagnostic_classification after read_macro_maps.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/modules/warn-spec-4_a.C: New test.
            * g++.dg/modules/warn-spec-4_b.C: New test.
    
    Signed-off-by: Nathaniel Shead <[email protected]>
    Reviewed-by: Jason Merrill <[email protected]>

Diff:
---
 gcc/cp/module.cc                             | 10 ++++++----
 gcc/testsuite/g++.dg/modules/warn-spec-4_a.C | 15 +++++++++++++++
 gcc/testsuite/g++.dg/modules/warn-spec-4_b.C |  8 ++++++++
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 4dcee160956d..628dec9c1b23 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -21217,10 +21217,6 @@ module_state::read_initial (cpp_reader *reader)
   else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
     ok = false;
 
-  if (ok && have_locs && config.ordinary_locs
-      && !read_diagnostic_classification (global_dc))
-    ok = false;
-
   /* Allocate the REMAP vector.  */
   slurp->alloc_remap (config.num_imports);
 
@@ -21281,6 +21277,12 @@ module_state::read_initial (cpp_reader *reader)
   else if (!read_macro_maps (config.macro_locs))
     ok = false;
 
+  /* Diagnostic classification streaming needs to come after reading
+     macro maps to handle _Pragmas in macros.  */
+  if (ok && have_locs && config.ordinary_locs
+      && !read_diagnostic_classification (global_dc))
+    ok = false;
+
   /* Note whether there's an active initializer.  */
   active_init_p = !is_header () && bool (config.active_init);
 
diff --git a/gcc/testsuite/g++.dg/modules/warn-spec-4_a.C 
b/gcc/testsuite/g++.dg/modules/warn-spec-4_a.C
new file mode 100644
index 000000000000..2eddc1b4c567
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/warn-spec-4_a.C
@@ -0,0 +1,15 @@
+// PR c++/124459
+// { dg-additional-options "-fmodules -Wunused-but-set-variable" }
+// Test diagnostic pragmas in macro maps.
+
+export module M;
+
+#define MACRO(t) \
+  _Pragma("GCC diagnostic push") \
+  _Pragma("GCC diagnostic ignored \"-Wunused-but-set-variable\"") \
+  int x = t; \
+  _Pragma("GCC diagnostic pop")
+
+export template <typename T> void foo(T t) {
+  MACRO(t);
+}
diff --git a/gcc/testsuite/g++.dg/modules/warn-spec-4_b.C 
b/gcc/testsuite/g++.dg/modules/warn-spec-4_b.C
new file mode 100644
index 000000000000..fce9b849308c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/warn-spec-4_b.C
@@ -0,0 +1,8 @@
+// PR c++/124459
+// { dg-additional-options "-fmodules -Wunused-but-set-variable" }
+
+import M;
+
+void test() {
+  foo(123);
+}

Reply via email to