https://gcc.gnu.org/g:fcb78dea6b1b854b80ab623d81402bd0d0c39697

commit r16-7948-gfcb78dea6b1b854b80ab623d81402bd0d0c39697
Author: Jason Merrill <[email protected]>
Date:   Thu Mar 5 17:54:30 2026 -0500

    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: <[email protected]>

Diff:
---
 gcc/cp/module.cc                     | 8 +++++++-
 gcc/testsuite/g++.dg/modules/dep-6.C | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index ccbf124876d8..ec6357b33733 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -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);
diff --git a/gcc/testsuite/g++.dg/modules/dep-6.C 
b/gcc/testsuite/g++.dg/modules/dep-6.C
new file mode 100644
index 000000000000..8a7960585a11
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/dep-6.C
@@ -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} } }

Reply via email to