From 0661c5480c80bc40d9bc1cb15c3264d67c2efe9c Mon Sep 17 00:00:00 2001 From: Nathaniel Shead Date: Fri, 13 Mar 2026 00:47:36 +1100 Subject: [PATCH] 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 Reviewed-by: Jason Merrill --- 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(-) create mode 100644 gcc/testsuite/g++.dg/modules/warn-spec-4_a.C create mode 100644 gcc/testsuite/g++.dg/modules/warn-spec-4_b.C diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 4dcee160956..628dec9c1b2 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 00000000000..2eddc1b4c56 --- /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 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 00000000000..fce9b849308 --- /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); +}