Fortran: Improve file-reading error diagnostic [PR55534]
authorTobias Burnus <tobias@codesourcery.com>
Fri, 24 Sep 2021 07:30:51 +0000 (09:30 +0200)
committerTobias Burnus <tobias@codesourcery.com>
Fri, 24 Sep 2021 07:30:51 +0000 (09:30 +0200)
PR fortran/55534

gcc/fortran/ChangeLog:

* scanner.c (load_file): Return void, call (gfc_)fatal_error for
all errors.
(include_line, include_stmt, gfc_new_file): Remove exit call
for failed load_file run.

gcc/testsuite/ChangeLog:

* gfortran.dg/include_9.f90: Add dg-prune-output.
* gfortran.dg/include_23.f90: New test.
* gfortran.dg/include_24.f90: New test.

gcc/fortran/scanner.c
gcc/testsuite/gfortran.dg/include_23.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/include_24.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/include_9.f90

index 52124bd..5a45069 100644 (file)
@@ -47,6 +47,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "toplev.h"    /* For set_src_pwd.  */
 #include "debug.h"
 #include "options.h"
+#include "diagnostic-core.h"  /* For fatal_error. */
 #include "cpp.h"
 #include "scanner.h"
 
@@ -2230,7 +2231,7 @@ preprocessor_line (gfc_char_t *c)
 }
 
 
-static bool load_file (const char *, const char *, bool);
+static void load_file (const char *, const char *, bool);
 
 /* include_line()-- Checks a line buffer to see if it is an include
    line.  If so, we call load_file() recursively to load the included
@@ -2396,9 +2397,7 @@ include_line (gfc_char_t *line)
                   read by anything else.  */
 
   filename = gfc_widechar_to_char (begin, -1);
-  if (!load_file (filename, NULL, false))
-    exit (FATAL_EXIT_CODE);
-
+  load_file (filename, NULL, false);
   free (filename);
   return 1;
 }
@@ -2505,9 +2504,7 @@ include_stmt (gfc_linebuf *b)
       filename[i] = (unsigned char) c;
     }
   filename[length] = '\0';
-  if (!load_file (filename, NULL, false))
-    exit (FATAL_EXIT_CODE);
-
+  load_file (filename, NULL, false);
   free (filename);
 
 do_ret:
@@ -2525,9 +2522,11 @@ do_ret:
   return ret;
 }
 
+
+
 /* Load a file into memory by calling load_line until the file ends.  */
 
-static bool
+static void
 load_file (const char *realfilename, const char *displayedname, bool initial)
 {
   gfc_char_t *line;
@@ -2549,13 +2548,8 @@ load_file (const char *realfilename, const char *displayedname, bool initial)
 
   for (f = current_file; f; f = f->up)
     if (filename_cmp (filename, f->filename) == 0)
-      {
-       fprintf (stderr, "%s:%d: Error: File '%s' is being included "
-                "recursively\n", current_file->filename, current_file->line,
-                filename);
-       return false;
-      }
-
+      fatal_error (linemap_line_start (line_table, current_file->line, 0),
+                  "File %qs is being included recursively", filename);
   if (initial)
     {
       if (gfc_src_file)
@@ -2567,10 +2561,7 @@ load_file (const char *realfilename, const char *displayedname, bool initial)
        input = gfc_open_file (realfilename);
 
       if (input == NULL)
-       {
-         gfc_error_now ("Cannot open file %qs", filename);
-         return false;
-       }
+       gfc_fatal_error ("Cannot open file %qs", filename);
     }
   else
     {
@@ -2579,22 +2570,20 @@ load_file (const char *realfilename, const char *displayedname, bool initial)
        {
          /* For -fpre-include file, current_file is NULL.  */
          if (current_file)
-           fprintf (stderr, "%s:%d: Error: Can't open included file '%s'\n",
-                    current_file->filename, current_file->line, filename);
+           fatal_error (linemap_line_start (line_table, current_file->line, 0),
+                        "Cannot open included file %qs", filename);
          else
-           fprintf (stderr, "Error: Can't open pre-included file '%s'\n",
-                    filename);
-
-         return false;
+           gfc_fatal_error ("Cannot open pre-included file %qs", filename);
        }
       stat_result = stat (realfilename, &st);
-      if (stat_result == 0 && !S_ISREG(st.st_mode))
+      if (stat_result == 0 && !S_ISREG (st.st_mode))
        {
-         fprintf (stderr, "%s:%d: Error: Included path '%s'"
-                  " is not a regular file\n",
-                  current_file->filename, current_file->line, filename);
          fclose (input);
-         return false;
+         if (current_file)
+           fatal_error (linemap_line_start (line_table, current_file->line, 0),
+                        "Included file %qs is not a regular file", filename);
+         else
+           gfc_fatal_error ("Included file %qs is not a regular file", filename);
        }
     }
 
@@ -2768,7 +2757,6 @@ load_file (const char *realfilename, const char *displayedname, bool initial)
     add_file_change (NULL, current_file->inclusion_line + 1);
   current_file = current_file->up;
   linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
-  return true;
 }
 
 
@@ -2780,23 +2768,17 @@ load_file (const char *realfilename, const char *displayedname, bool initial)
 void
 gfc_new_file (void)
 {
-  bool result;
-
-  if (flag_pre_include != NULL
-      && !load_file (flag_pre_include, NULL, false))
-    exit (FATAL_EXIT_CODE);
+  if (flag_pre_include != NULL)
+    load_file (flag_pre_include, NULL, false);
 
   if (gfc_cpp_enabled ())
     {
-      result = gfc_cpp_preprocess (gfc_source_file);
+      gfc_cpp_preprocess (gfc_source_file);
       if (!gfc_cpp_preprocess_only ())
-        result = load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
+       load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
     }
   else
-    result = load_file (gfc_source_file, NULL, true);
-
-  if (!result)
-    exit (FATAL_EXIT_CODE);
+    load_file (gfc_source_file, NULL, true);
 
   gfc_current_locus.lb = line_head;
   gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
diff --git a/gcc/testsuite/gfortran.dg/include_23.f90 b/gcc/testsuite/gfortran.dg/include_23.f90
new file mode 100644 (file)
index 0000000..421ddda
--- /dev/null
@@ -0,0 +1,4 @@
+implicit none
+include "nonexisting/file.f90"  ! { dg-error "Cannot open included file 'nonexisting/file.f90'" }
+end
+! { dg-prune-output "compilation terminated." }
diff --git a/gcc/testsuite/gfortran.dg/include_24.f90 b/gcc/testsuite/gfortran.dg/include_24.f90
new file mode 100644 (file)
index 0000000..1fe9eb5
--- /dev/null
@@ -0,0 +1,4 @@
+implicit none
+include "."  ! { dg-error "Included file '.' is not a regular file" }
+end
+! { dg-prune-output "compilation terminated." }
index c4ef50f..6b0648b 100644 (file)
@@ -4,3 +4,4 @@
       program main
       end program
 ! { dg-error "is not a regular file" " " { target *-*-* } 3 }
+! { dg-prune-output "compilation terminated." }