dumpfile cleanup

https://gcc.gnu.org/ml/gcc-patches/2018-04/msg01173.html
	* dumpfile.c (dump_open): New.
	(dump_open_alternate_stream, dump_start, dump_begin): Call it.
	(dump_finish): Detect stdio/stderr by value not name.

From-SVN: r259681
This commit is contained in:
Nathan Sidwell 2018-04-26 14:15:58 +00:00 committed by Nathan Sidwell
parent 030b3bddb0
commit 5d8b352a10
2 changed files with 35 additions and 31 deletions

View File

@ -1,3 +1,9 @@
2018-04-26 Nathan Sidwell <nathan@acm.org>
* dumpfile.c (dump_open): New.
(dump_open_alternate_stream, dump_start, dump_begin): Call it.
(dump_finish): Detect stdio/stderr by value not name.
2018-04-26 Jonathan Wakely <jwakely@redhat.com> 2018-04-26 Jonathan Wakely <jwakely@redhat.com>
* doc/invoke.texi (-Wreturn-type): Document default status for C++. * doc/invoke.texi (-Wreturn-type): Document default status for C++.

View File

@ -312,6 +312,27 @@ get_dump_file_name (struct dump_file_info *dfi) const
return concat (dump_base_name, dump_id, dfi->suffix, NULL); return concat (dump_base_name, dump_id, dfi->suffix, NULL);
} }
/* Open a dump file called FILENAME. Some filenames are special and
refer to the standard streams. TRUNC indicates whether this is the
first open (so the file should be truncated, rather than appended).
An error message is emitted in the event of failure. */
static FILE *
dump_open (const char *filename, bool trunc)
{
if (strcmp ("stderr", filename) == 0)
return stderr;
if (strcmp ("stdout", filename) == 0)
return stdout;
FILE *stream = fopen (filename, trunc ? "w" : "a");
if (!stream)
error ("could not open dump file %qs: %m", filename);
return stream;
}
/* For a given DFI, open an alternate dump filename (which could also /* For a given DFI, open an alternate dump filename (which could also
be a standard stream such as stdout/stderr). If the alternate dump be a standard stream such as stdout/stderr). If the alternate dump
file cannot be opened, return NULL. */ file cannot be opened, return NULL. */
@ -319,22 +340,15 @@ get_dump_file_name (struct dump_file_info *dfi) const
static FILE * static FILE *
dump_open_alternate_stream (struct dump_file_info *dfi) dump_open_alternate_stream (struct dump_file_info *dfi)
{ {
FILE *stream ;
if (!dfi->alt_filename) if (!dfi->alt_filename)
return NULL; return NULL;
if (dfi->alt_stream) if (dfi->alt_stream)
return dfi->alt_stream; return dfi->alt_stream;
stream = strcmp ("stderr", dfi->alt_filename) == 0 FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
? stderr
: strcmp ("stdout", dfi->alt_filename) == 0
? stdout
: fopen (dfi->alt_filename, dfi->alt_state < 0 ? "w" : "a");
if (!stream) if (stream)
error ("could not open dump file %qs: %m", dfi->alt_filename);
else
dfi->alt_state = 1; dfi->alt_state = 1;
return stream; return stream;
@ -515,14 +529,8 @@ dump_start (int phase, dump_flags_t *flag_ptr)
name = get_dump_file_name (phase); name = get_dump_file_name (phase);
if (name) if (name)
{ {
stream = strcmp ("stderr", name) == 0 stream = dump_open (name, dfi->pstate < 0);
? stderr if (stream)
: strcmp ("stdout", name) == 0
? stdout
: fopen (name, dfi->pstate < 0 ? "w" : "a");
if (!stream)
error ("could not open dump file %qs: %m", name);
else
{ {
dfi->pstate = 1; dfi->pstate = 1;
count++; count++;
@ -562,13 +570,10 @@ dump_finish (int phase)
if (phase < 0) if (phase < 0)
return; return;
dfi = get_dump_file_info (phase); dfi = get_dump_file_info (phase);
if (dfi->pstream && (!dfi->pfilename if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
|| (strcmp ("stderr", dfi->pfilename) != 0
&& strcmp ("stdout", dfi->pfilename) != 0)))
fclose (dfi->pstream); fclose (dfi->pstream);
if (dfi->alt_stream && strcmp ("stderr", dfi->alt_filename) != 0 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
&& strcmp ("stdout", dfi->alt_filename) != 0)
fclose (dfi->alt_stream); fclose (dfi->alt_stream);
dfi->alt_stream = NULL; dfi->alt_stream = NULL;
@ -607,15 +612,8 @@ dump_begin (int phase, dump_flags_t *flag_ptr)
return NULL; return NULL;
dfi = get_dump_file_info (phase); dfi = get_dump_file_info (phase);
stream = strcmp ("stderr", name) == 0 stream = dump_open (name, dfi->pstate < 0);
? stderr if (stream)
: strcmp ("stdout", name) == 0
? stdout
: fopen (name, dfi->pstate < 0 ? "w" : "a");
if (!stream)
error ("could not open dump file %qs: %m", name);
else
dfi->pstate = 1; dfi->pstate = 1;
free (name); free (name);