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 <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
This commit is contained in:
Nathaniel Shead
2026-03-13 00:47:36 +11:00
parent 2d1bf27b49
commit 0661c5480c
3 changed files with 29 additions and 4 deletions

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -0,0 +1,8 @@
// PR c++/124459
// { dg-additional-options "-fmodules -Wunused-but-set-variable" }
import M;
void test() {
foo(123);
}