c++/modules: fix -MG for header units [PR123622]

With -MG we should allow a nonexistent header unit, as we do with a
nonexistent #include.  But still import it if available.

	PR c++/123622

gcc/cp/ChangeLog:

	* module.cc (preprocess_module): Check deps.missing_files.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/dep-6.C: New test.

Co-authored-by: <mtxn@duck.com>
This commit is contained in:
Jason Merrill
2026-03-05 17:54:30 -05:00
parent c600bb7aef
commit fcb78dea6b
2 changed files with 13 additions and 1 deletions

View File

@@ -23319,7 +23319,13 @@ preprocess_module (module_state *module, location_t from_loc,
if (!(import->is_module ()
&& (import->is_partition () || import->is_exported ()))
&& import->loadedness == ML_NONE
&& (import->is_header () || !flag_preprocess_only))
&& (!flag_preprocess_only
|| (import->is_header ()
/* Allow a missing/unimportable GCM with -MG.
FIXME We should also try falling back to #include
before giving up entirely. */
&& (!cpp_get_options (reader)->deps.missing_files
|| import->check_importable (reader)))))
{
unsigned n = dump.push (import);
import->do_import (reader, true);

View File

@@ -0,0 +1,6 @@
// PR c++/123622
// { dg-additional-options "-fmodules -M -MG -MF dep-6.d" }
import "dep-6.h"; // Must not exist!
// { dg-final { scan-file dep-6.d {\nCXX_IMPORTS \+= \./dep-6\.h\.c\+\+-module} } }