This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Fix uses of strerror in diagnostics


This patch fixes issues I noticed while planning the conversion of the
driver to use the common diagnostic infrastructure.  The common
diagnostics code supports %m to print strerror (errno); code using
that infrastructure should be using %m instead of calling strerror
itself.  Furthermore, libiberty provides an xstrerror wrapper and any
code that can't use %m should be using xstrerror rather than strerror
directly.  In the case of lto-coff.c and lto-macho.c, coff_errmsg and
mach_o_errmsg were only ever called with -1 as argument; they appear
to be useless abstractions at present and I removed them and changed
the callers to use %m.

Bootstrapped with no regressions on x86_64-unknown-linux-gnu.  OK to
commit?

2010-05-28  Joseph Myers  <joseph@codesourcery.com>

	* final.c (rest_of_clean_state): Use %m in errors instead of
	strerror (errno).
	* gengtype.c (read_input_list, close_output_files): Use xstrerror
	instead of strerror.
	* toplev.c (process_options): Use %m in errors instead of strerror
	(errno).
	* tree-dump.c (dump_begin): Use %m in errors instead of strerror
	(errno).

fortran:
2010-05-28  Joseph Myers  <joseph@codesourcery.com>

	* cpp.c (gfc_cpp_init_0): Use xstrerror instead of strerror.
	* module.c (write_char, gfc_dump_module, gfc_use_module): Use
	xstrerror instead of strerror.

lto:
2010-05-28  Joseph Myers  <joseph@codesourcery.com>

	* lto-coff.c (coff_errmsg): Remove.
	(lto_coff_begin_section_with_type, lto_obj_append_data): Use %m in
	errors instead of coff_errmsg (-1).
	* lto-macho.c (mach_o_errmsg): Remove.
	(lto_obj_begin_section, lto_obj_append_data): Use %m in errors
	instead of mach_o_errmsg (-1).
	* lto.c (read_cgraph_and_symbols): Use %m in errors instead of
	xstrerror (errno).

Index: tree-dump.c
===================================================================
--- tree-dump.c	(revision 159955)
+++ tree-dump.c	(working copy)
@@ -1,6 +1,6 @@
 /* Tree-dumping functionality for intermediate representation.
-   Copyright (C) 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
-   Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
+   2010 Free Software Foundation, Inc.
    Written by Mark Mitchell <mark@codesourcery.com>
 
 This file is part of GCC.
@@ -926,7 +926,7 @@ dump_begin (int phase, int *flag_ptr)
   dfi = get_dump_file_info (phase);
   stream = fopen (name, dfi->state < 0 ? "w" : "a");
   if (!stream)
-    error ("could not open dump file %qs: %s", name, strerror (errno));
+    error ("could not open dump file %qs: %m", name);
   else
     dfi->state = 1;
   free (name);
Index: gengtype.c
===================================================================
--- gengtype.c	(revision 159955)
+++ gengtype.c	(working copy)
@@ -1,5 +1,5 @@
 /* Process source files and output type information.
-   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
 
    This file is part of GCC.
@@ -392,7 +392,7 @@ read_input_list (const char *listname)
 {
   FILE *list = fopen (listname, "r");
   if (!list)
-    fatal ("cannot open %s: %s", listname, strerror (errno));
+    fatal ("cannot open %s: %s", listname, xstrerror (errno));
   else
     {
       struct fileloc epos;
@@ -508,7 +508,7 @@ read_input_list (const char *listname)
   }
 
   if (ferror (list))
-    fatal ("error reading %s: %s", listname, strerror (errno));
+    fatal ("error reading %s: %s", listname, xstrerror (errno));
 
   fclose (list);
 }
@@ -1838,11 +1838,11 @@ close_output_files (void)
       {
         FILE *newfile = fopen (of->name, "w");
         if (newfile == NULL)
-          fatal ("opening output file %s: %s", of->name, strerror (errno));
+          fatal ("opening output file %s: %s", of->name, xstrerror (errno));
         if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
-          fatal ("writing output file %s: %s", of->name, strerror (errno));
+          fatal ("writing output file %s: %s", of->name, xstrerror (errno));
         if (fclose (newfile) != 0)
-          fatal ("closing output file %s: %s", of->name, strerror (errno));
+          fatal ("closing output file %s: %s", of->name, xstrerror (errno));
       }
       free(of->buf);
       of->buf = NULL;
Index: final.c
===================================================================
--- final.c	(revision 159955)
+++ final.c	(working copy)
@@ -4363,8 +4363,8 @@ rest_of_clean_state (void)
       final_output = fopen (flag_dump_final_insns, "a");
       if (!final_output)
 	{
-	  error ("could not open final insn dump file %qs: %s",
-		 flag_dump_final_insns, strerror (errno));
+	  error ("could not open final insn dump file %qs: %m",
+		 flag_dump_final_insns);
 	  flag_dump_final_insns = NULL;
 	}
       else
@@ -4424,8 +4424,8 @@ rest_of_clean_state (void)
 
       if (fclose (final_output))
 	{
-	  error ("could not close final insn dump file %qs: %s",
-		 flag_dump_final_insns, strerror (errno));
+	  error ("could not close final insn dump file %qs: %m",
+		 flag_dump_final_insns);
 	  flag_dump_final_insns = NULL;
 	}
     }
Index: toplev.c
===================================================================
--- toplev.c	(revision 159955)
+++ toplev.c	(working copy)
@@ -1948,14 +1948,14 @@ process_options (void)
       FILE *final_output = fopen (flag_dump_final_insns, "w");
       if (!final_output)
 	{
-	  error ("could not open final insn dump file %qs: %s",
-		 flag_dump_final_insns, strerror (errno));
+	  error ("could not open final insn dump file %qs: %m",
+		 flag_dump_final_insns);
 	  flag_dump_final_insns = NULL;
 	}
       else if (fclose (final_output))
 	{
-	  error ("could not close zeroed insn dump file %qs: %s",
-		 flag_dump_final_insns, strerror (errno));
+	  error ("could not close zeroed insn dump file %qs: %m",
+		 flag_dump_final_insns);
 	  flag_dump_final_insns = NULL;
 	}
     }
Index: fortran/cpp.c
===================================================================
--- fortran/cpp.c	(revision 159955)
+++ fortran/cpp.c	(working copy)
@@ -523,7 +523,8 @@ gfc_cpp_init_0 (void)
 	  print.outf = fopen (gfc_cpp_option.output_filename, "w");
 	  if (print.outf == NULL)
 	    gfc_fatal_error ("opening output file %s: %s",
-			     gfc_cpp_option.output_filename, strerror(errno));
+			     gfc_cpp_option.output_filename,
+			     xstrerror (errno));
 	}
       else
 	print.outf = stdout;
@@ -533,7 +534,7 @@ gfc_cpp_init_0 (void)
       print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
       if (print.outf == NULL)
 	gfc_fatal_error ("opening output file %s: %s",
-			 gfc_cpp_option.temporary_filename, strerror(errno));
+			 gfc_cpp_option.temporary_filename, xstrerror (errno));
     }
 
   gcc_assert(cpp_in);
Index: fortran/module.c
===================================================================
--- fortran/module.c	(revision 159955)
+++ fortran/module.c	(working copy)
@@ -1299,7 +1299,7 @@ static void
 write_char (char out)
 {
   if (putc (out, module_fp) == EOF)
-    gfc_fatal_error ("Error writing modules file: %s", strerror (errno));
+    gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
 
   /* Add this to our MD5.  */
   md5_process_bytes (&out, sizeof (out), &ctx);
@@ -5124,7 +5124,7 @@ gfc_dump_module (const char *name, int d
   module_fp = fopen (filename_tmp, "w");
   if (module_fp == NULL)
     gfc_fatal_error ("Can't open module file '%s' for writing at %C: %s",
-		     filename_tmp, strerror (errno));
+		     filename_tmp, xstrerror (errno));
 
   /* Write the header, including space reserved for the MD5 sum.  */
   now = time (NULL);
@@ -5162,7 +5162,7 @@ gfc_dump_module (const char *name, int d
 
   if (fclose (module_fp))
     gfc_fatal_error ("Error writing module file '%s' for writing: %s",
-		     filename_tmp, strerror (errno));
+		     filename_tmp, xstrerror (errno));
 
   /* Read the MD5 from the header of the old module file and compare.  */
   if (read_md5_from_module_file (filename, md5_old) != 0
@@ -5171,16 +5171,16 @@ gfc_dump_module (const char *name, int d
       /* Module file have changed, replace the old one.  */
       if (unlink (filename) && errno != ENOENT)
 	gfc_fatal_error ("Can't delete module file '%s': %s", filename,
-			 strerror (errno));
+			 xstrerror (errno));
       if (rename (filename_tmp, filename))
 	gfc_fatal_error ("Can't rename module file '%s' to '%s': %s",
-			 filename_tmp, filename, strerror (errno));
+			 filename_tmp, filename, xstrerror (errno));
     }
   else
     {
       if (unlink (filename_tmp))
 	gfc_fatal_error ("Can't delete temporary module file '%s': %s",
-			 filename_tmp, strerror (errno));
+			 filename_tmp, xstrerror (errno));
     }
 }
 
@@ -5530,7 +5530,7 @@ gfc_use_module (void)
 
   if (module_fp == NULL)
     gfc_fatal_error ("Can't open module file '%s' for reading at %C: %s",
-		     filename, strerror (errno));
+		     filename, xstrerror (errno));
 
   /* Check that we haven't already USEd an intrinsic module with the
      same name.  */
Index: lto/lto.c
===================================================================
--- lto/lto.c	(revision 159955)
+++ lto/lto.c	(working copy)
@@ -1604,8 +1604,7 @@ read_cgraph_and_symbols (unsigned nfiles
 
       resolution = fopen (resolution_file_name, "r");
       if (resolution == NULL)
-	fatal_error ("could not open symbol resolution file: %s",
-		     xstrerror (errno));
+	fatal_error ("could not open symbol resolution file: %m");
 
       t = fscanf (resolution, "%u", &num_objects);
       gcc_assert (t == 1);
Index: lto/lto-coff.c
===================================================================
--- lto/lto-coff.c	(revision 159955)
+++ lto/lto-coff.c	(working copy)
@@ -1,5 +1,5 @@
 /* LTO routines for COFF object files.
-   Copyright 2009 Free Software Foundation, Inc.
+   Copyright 2009, 2010 Free Software Foundation, Inc.
    Contributed by Dave Korn.
 
 This file is part of GCC.
@@ -133,15 +133,6 @@ lto_file_init (lto_file *file, const cha
   file->offset = offset;
 }
 
-/* Return an error string after an error, or a predetermined one
-   if ERRCODE is not -1.  */
-
-static const char *
-coff_errmsg (int errcode)
-{
-  return strerror (errcode == -1 ? errno : errcode);
-}
-
 /* Returns a hash code for P.  */
 
 static hashval_t
@@ -273,7 +264,7 @@ lto_coff_begin_section_with_type (const 
   /* Create a new section.  */
   file->scn = coff_newsection (file, name, type);
   if (!file->scn)
-    fatal_error ("could not create a new COFF section: %s", coff_errmsg (-1));
+    fatal_error ("could not create a new COFF section: %m");
 
   /* Add a string table entry and record the offset.  */
   gcc_assert (file->shstrtab_stream);
@@ -312,7 +303,7 @@ lto_obj_append_data (const void *data, s
 
   coff_data = coff_newdata (file->scn);
   if (!coff_data)
-    fatal_error ("could not append data to COFF section: %s", coff_errmsg (-1));
+    fatal_error ("could not append data to COFF section: %m");
 
   coff_data->d_buf = CONST_CAST (void *, data);
   coff_data->d_size = len;
Index: lto/lto-macho.c
===================================================================
--- lto/lto-macho.c	(revision 159955)
+++ lto/lto-macho.c	(working copy)
@@ -140,15 +140,6 @@ lto_file_init (lto_file *file, const cha
   file->offset = offset;
 }
 
-/* Return an error string after an error, or a predetermined one
-   if ERRCODE is not -1.  */
-
-static const char *
-mach_o_errmsg (int errcode)
-{
-  return strerror (errcode == -1 ? errno : errcode);
-}
-
 /* Returns a hash code for P.  */
 
 static hashval_t
@@ -321,7 +312,7 @@ lto_obj_begin_section (const char *name)
   /* Create a new section.  */
   file->scn = mach_o_new_section (file, name);
   if (!file->scn)
-    fatal_error ("could not create a new Mach-O section: %s", mach_o_errmsg (-1));
+    fatal_error ("could not create a new Mach-O section: %m");
 }
 
 
@@ -343,7 +334,7 @@ lto_obj_append_data (const void *data, s
 
   mach_o_data = mach_o_new_data (file->scn);
   if (!mach_o_data)
-    fatal_error ("could not append data to Mach-O section: %s", mach_o_errmsg (-1));
+    fatal_error ("could not append data to Mach-O section: %m");
 
   mach_o_data->d_buf = CONST_CAST (void *, data);
   mach_o_data->d_size = len;

-- 
Joseph S. Myers
joseph@codesourcery.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]