https://gcc.gnu.org/g:33426febd82ce80816bdd0d1c9938bfd3b6842ce

commit r14-12424-g33426febd82ce80816bdd0d1c9938bfd3b6842ce
Author: Jakub Jelinek <[email protected]>
Date:   Fri Mar 13 21:58:46 2026 +0100

    libcpp: Fix up -MP for - input [PR105412]
    
    For -MP we used to emit something like below in GCC 9 and earlier:
    touch a.h; echo '#include "a.h"' | ./cc1 -quiet -E -M -MP -
    -: /usr/include/stdc-predef.h a.h
    /usr/include/stdc-predef.h:
    a.h:
    but since r10-205 we emit instead just
    touch a.h; echo '#include "a.h"' | ./cc1 -quiet -E -M -MP -
    -: /usr/include/stdc-predef.h a.h
    a.h:
    i.e. the rule for /usr/include/stdc-predef.h is removed.  Similarly
    with -nostdinc:
    touch a.h; echo '#include "a.h"' | ./cc1 -quiet -E -nostdinc -M -MP -
    -: a.h
    a.h:
    r10-205 changed that to:
    touch a.h; echo '#include "a.h"' | ./cc1 -quiet -E -nostdinc -M -MP -
    -: a.h
    a.h:
    The rationale for the change was
    https://gcc.gnu.org/legacy-ml/gcc-patches/2019-05/msg00323.html
    where after the mkdeps.cc reimplementation it started ICEing on
    - input (file->path[0] is "" in that case).
    The problem is that by leaving out the "" dependency (which we then
    in some cases indeed ignore) the -MP printing
          if (CPP_OPTION (pfile, deps.phony_targets))
           for (unsigned i = 1; i < d->deps.size (); i++)
             fprintf (fp, "%s:\n", munge (d->deps[i]));
    starts at d->deps[1] unconditionally and so ignores the first dependency
    even when it is not the main file.
    So, either we could just revert the r10-205 change and ensure we don't
    ICE on it and keep ignoring it when it should be ignored, or the
    following patch fixes it by not adding the "" dep, but remembering that
    in that case we should start iterating from i = 0; and not from i = 1;
    
    2026-03-13  Jakub Jelinek  <[email protected]>
    
            PR preprocessor/105412
            * files.cc (_cpp_stack_file): Call deps_add_dep even on
            empty file path.
            * mkdeps.cc (class mkdeps): Add first_phony_dep member.
            (mkdeps::mkdeps ()): Initialize it to 1.
            (deps_add_dep): When called first with "" argument, decrease
            d->first_phony_dep to 0.
            (make_write): For -MP iterate from d->first_phony_dep
            rather than 1.
    
    Reviewed-by: Joseph Myers <[email protected]>
    (cherry picked from commit f25788bfb856a83f7d12d850d62a940536b70688)

Diff:
---
 libcpp/files.cc  |  1 -
 libcpp/mkdeps.cc | 22 +++++++++++++++-------
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/libcpp/files.cc b/libcpp/files.cc
index 12de69e5c2d4..7613d15e7cbc 100644
--- a/libcpp/files.cc
+++ b/libcpp/files.cc
@@ -972,7 +972,6 @@ _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, 
include_type type,
       /* Add the file to the dependencies on its first inclusion.  */
       if (CPP_OPTION (pfile, deps.style) > (sysp != 0)
          && !file->stack_count
-         && file->path[0]
          && !(pfile->main_file == file
               && CPP_OPTION (pfile, deps.ignore_main_file)))
        deps_add_dep (pfile->deps, file->path);
diff --git a/libcpp/mkdeps.cc b/libcpp/mkdeps.cc
index 4cf0cf09178c..5902e9bdabc9 100644
--- a/libcpp/mkdeps.cc
+++ b/libcpp/mkdeps.cc
@@ -81,8 +81,9 @@ public:
   };
 
   mkdeps ()
-    : primary_output (NULL), module_name (NULL), cmi_name (NULL)
-    , is_header_unit (false), is_exported (false), quote_lwm (0)
+    : primary_output (NULL), module_name (NULL), cmi_name (NULL),
+      is_header_unit (false), is_exported (false), quote_lwm (0),
+      first_phony_dep (1)
   {
   }
   ~mkdeps ()
@@ -118,6 +119,7 @@ public:
   bool is_header_unit;
   bool is_exported;
   unsigned short quote_lwm;
+  unsigned int first_phony_dep;
 };
 
 /* Apply Make quoting to STR, TRAIL.  Note that it's not possible to
@@ -318,11 +320,17 @@ fdeps_add_target (struct mkdeps *d, const char *o, bool 
is_primary)
 void
 deps_add_dep (class mkdeps *d, const char *t)
 {
-  gcc_assert (*t);
-
-  t = apply_vpath (d, t);
+  if (*t)
+    {
+      t = apply_vpath (d, t);
 
-  d->deps.push (xstrdup (t));
+      d->deps.push (xstrdup (t));
+    }
+  /* When called first with "", remember we should
+     emit for -MP all dependencies rather than just
+     second and following dependency.  See PR105412.  */
+  else if (d->deps.size () == 0)
+    d->first_phony_dep = 0;
 }
 
 void
@@ -439,7 +447,7 @@ make_write (const cpp_reader *pfile, FILE *fp, unsigned int 
colmax)
       make_write_vec (d->deps, fp, column, colmax);
       fputs ("\n", fp);
       if (CPP_OPTION (pfile, deps.phony_targets))
-       for (unsigned i = 1; i < d->deps.size (); i++)
+       for (unsigned i = d->first_phony_dep; i < d->deps.size (); i++)
          fprintf (fp, "%s:\n", munge (d->deps[i]));
     }

Reply via email to