Hi!

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;

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

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.

--- libcpp/files.cc.jj  2026-03-02 07:43:12.612783616 +0100
+++ libcpp/files.cc     2026-03-12 17:50:42.012876133 +0100
@@ -972,7 +972,6 @@ _cpp_stack_file (cpp_reader *pfile, _cpp
   /* 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);
--- libcpp/mkdeps.cc.jj 2026-01-02 09:56:10.418332275 +0100
+++ libcpp/mkdeps.cc    2026-03-12 17:57:24.748114816 +0100
@@ -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, cons
 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
@@ -438,7 +446,7 @@ make_write (const cpp_reader *pfile, FIL
       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]));
     }
 

        Jakub

Reply via email to