[RFA][fortran] Fix # <line> <file> handling in the Fortran front-end

Jeff Law law@redhat.com
Fri May 4 19:32:00 GMT 2018


The Fortran front end has its own code to parse # <line> <file>
directives.  We've run into a case where it does not function correctly.
 In particular when the directive changes the current file, subsequent
diagnostics still refer to the original filename.

Concretely take this code and compile it with -Wall:



# 12345 "foo-f"
SUBROUTINE s(dummy)
  INTEGER, INTENT(in) :: dummy
END SUBROUTINE


The "dummy" argument is unused and you'll get a diagnostic like:

-bash-4.3$ gfortran j.f90 -Wall
j.f90:12345:18:

Warning: Unused dummy argument ‘dummy’ at (1) [-Wunused-dummy-argument]

Note how we got the right line #, but the wrong file in the diagnostic.
It should look something like this:

[law@torsion gcc]$ ./gfortran -Wall -B./ j.f90
foo-f:12345:18:

Warning: Unused dummy argument ‘dummy’ at (1) [-Wunused-dummy-argument]

--


AFAICT the Fortran front-end has failed to notify the linemap interface
that the current filename has changed.

This patch (and testcase) fixes the problem by adding the missing call
to linemap_add.

Bootstrapped and regression tested on x86_64-linux-gnu.  OK for the trunk?

Jeff
-------------- next part --------------
diff --git a/gcc/fortran/scanner.c b/gcc/fortran/scanner.c
index aab5379..55d6daf 100644
--- a/gcc/fortran/scanner.c
+++ b/gcc/fortran/scanner.c
@@ -2107,6 +2107,10 @@ preprocessor_line (gfc_char_t *c)
           in the linemap.  Alternative could be using GC or updating linemap to
           point to the new name, but there is no API for that currently.  */
       current_file->filename = xstrdup (filename);
+
+      /* We need to tell the linemap API that the filename changed.  Just
+	 changing current_file is insufficient.  */
+      linemap_add (line_table, LC_RENAME, false, current_file->filename, line);
     }
 
   /* Set new line number.  */
diff --git a/gcc/testsuite/gfortran.dg/linefile.f90 b/gcc/testsuite/gfortran.dg/linefile.f90
new file mode 100644
index 0000000..7f1465a
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/linefile.f90
@@ -0,0 +1,12 @@
+! { dg-do compile }
+! { dg-options "-Wall" }
+
+# 4 "foo-f"
+SUBROUTINE s(dummy)
+  INTEGER, INTENT(in) :: dummy
+END SUBROUTINE
+! We want to check that the # directive changes the filename in the
+! diagnostic.  Nothing else really matters here.  dg-regexp allows us
+! to see the entire diagnostic.  We just have to make sure to consume
+! the entire message.
+! { dg-regexp "foo-f\[^\n]*" }


More information about the Gcc-patches mailing list