The -MT and -MQ options should replace the default target in the
generated dependency file. deps_add_target needs to be called before
cpp_read_main_file, otherwise the original object name is added.

gcc/fortran/
        PR fortran/47485
        * cpp.cc: fix -MT/-MQ adding additional target instead of
          replacing the default

gcc/testsuite/
        PR fortran/47485
        * gfortran.dg/dependency_generation_1.f90: New test

Signed-off-by: Vincent Vanlaer <vincen...@volkihar.be>
---
 gcc/fortran/cpp.cc                             | 18 ++++++++++++------
 .../gfortran.dg/dependency_generation_1.f90    | 15 +++++++++++++++
 2 files changed, 27 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/dependency_generation_1.f90

diff --git a/gcc/fortran/cpp.cc b/gcc/fortran/cpp.cc
index 7c5f00cfd69..3b93d17b90f 100644
--- a/gcc/fortran/cpp.cc
+++ b/gcc/fortran/cpp.cc
@@ -96,6 +96,8 @@ struct gfc_cpp_option_data
   int deps_skip_system;                 /* -MM */
   const char *deps_filename;            /* -M[M]D */
   const char *deps_filename_user;       /* -MF <arg> */
+  const char *deps_target_filename;     /* -MT / -MQ <arg> */
+  bool quote_deps_target_filename;      /* -MQ */
   int deps_missing_are_generated;       /* -MG */
   int deps_phony;                       /* -MP */
   int warn_date_time;                   /* -Wdate-time */
@@ -287,6 +289,8 @@ gfc_cpp_init_options (unsigned int decoded_options_count,
   gfc_cpp_option.deps_missing_are_generated = 0;
   gfc_cpp_option.deps_filename = NULL;
   gfc_cpp_option.deps_filename_user = NULL;
+  gfc_cpp_option.deps_target_filename = NULL;
+  gfc_cpp_option.quote_deps_target_filename = false;
 
   gfc_cpp_option.multilib = NULL;
   gfc_cpp_option.prefix = NULL;
@@ -439,9 +443,8 @@ gfc_cpp_handle_option (size_t scode, const char *arg, int 
value ATTRIBUTE_UNUSED
 
     case OPT_MQ:
     case OPT_MT:
-      gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = 
code;
-      gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
-      gfc_cpp_option.deferred_opt_count++;
+      gfc_cpp_option.quote_deps_target_filename = (code == OPT_MQ);
+      gfc_cpp_option.deps_target_filename = arg;
       break;
 
     case OPT_P:
@@ -593,6 +596,12 @@ gfc_cpp_init_0 (void)
     }
 
   gcc_assert(cpp_in);
+
+  if (gfc_cpp_option.deps_target_filename)
+    if (mkdeps *deps = cpp_get_deps (cpp_in))
+      deps_add_target (deps, gfc_cpp_option.deps_target_filename,
+                      gfc_cpp_option.quote_deps_target_filename);
+
   if (!cpp_read_main_file (cpp_in, gfc_source_file))
     errorcount++;
 }
@@ -635,9 +644,6 @@ gfc_cpp_init (void)
          else
            cpp_assert (cpp_in, opt->arg);
        }
-      else if (opt->code == OPT_MT || opt->code == OPT_MQ)
-       if (mkdeps *deps = cpp_get_deps (cpp_in))
-         deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
     }
 
   /* Pre-defined macros for non-required INTEGER kind types.  */
diff --git a/gcc/testsuite/gfortran.dg/dependency_generation_1.f90 
b/gcc/testsuite/gfortran.dg/dependency_generation_1.f90
new file mode 100644
index 00000000000..d42a257f83a
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/dependency_generation_1.f90
@@ -0,0 +1,15 @@
+! This test case ensures that the -MT flag is correctly replacing the object 
name in the dependency file.
+! See PR 47485
+!
+! Contributed by Vincent Vanlaer <vincen...@volkihar.be>
+!
+! { dg-do preprocess }
+! { dg-additional-options "-cpp" }
+! { dg-additional-options "-M" }
+! { dg-additional-options "-MF deps" }
+! { dg-additional-options "-MT obj.o" }
+
+module test
+end module
+
+! { dg-final { scan-file "deps" "obj.o:.*" } }
-- 
2.47.0

Reply via email to