Improve file_util.m.

compiler/file_util.m:
    Delete the obsolete predicate report_error/3 which wrote to the implicit
    current output stream.

    Move a predicate that is used only in prog_event.m to prog_event.m.

    Rename of the two open_temp_output predicates to avoid ambiguity.

compiler/prog_event.m:
    Move a predicate from file_util.m to here, and give it
    a more descriptive name.

compiler/compile_target_code.m:
    Conform to the predicate rename above.
This commit is contained in:
Zoltan Somogyi
2023-09-12 09:57:36 +10:00
parent ecab3e6d1e
commit 8ccd1c6c78
3 changed files with 51 additions and 54 deletions

View File

@@ -2913,7 +2913,7 @@ create_java_exe_or_lib(Globals, ProgressStream, ErrorStream, LinkTargetType,
% extremely long. We create the temporary file in the current directory to
% avoid problems under Cygwin, where absolute paths will be interpreted
% incorrectly when passed to a non-Cygwin jar program.
open_temp_output(".", "mtmp", "", TempFileResult, !IO),
open_temp_output_with_naming_scheme(".", "mtmp", "", TempFileResult, !IO),
(
TempFileResult = ok({TempFileName, Stream}),
list.foldl(write_jar_class_argument(Stream, ClassSubDir),

View File

@@ -86,8 +86,6 @@
%---------------------------------------------------------------------------%
:- pred report_error(string::in, io::di, io::uo) is det.
:- pragma obsolete(pred(report_error/3), [report_error/4]).
:- pred report_error(io.text_output_stream::in, string::in,
io::di, io::uo) is det.
@@ -113,22 +111,14 @@
% returns the file's name and output stream. On error, any temporary
% file will be removed.
%
:- pred open_temp_output(string::in, string::in, string::in,
:- pred open_temp_output_with_naming_scheme(string::in, string::in, string::in,
maybe_error({string, text_output_stream})::out, io::di, io::uo) is det.
% As above, but with any old name and location for the temporary file.
%
:- pred open_temp_output(maybe_error({string, text_output_stream})::out,
io::di, io::uo) is det.
% open_temp_input(Result, WritePred, !IO):
%
% Create a temporary file and call WritePred which will write data to it.
% If successful Result returns the file's name and a freshly opened
% input stream. On error any temporary file will be removed.
%
:- pred open_temp_input(maybe_error({string, text_input_stream})::out,
pred(string, maybe_error, io, io)::in(pred(in, out, di, uo) is det),
io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
@@ -333,10 +323,6 @@ unable_to_open_file(ErrorStream, FileName, IOErr, !IO) :-
%---------------------------------------------------------------------------%
report_error(ErrorMessage, !IO) :-
io.output_stream(Stream, !IO),
report_error(Stream, ErrorMessage, !IO).
report_error(Stream, ErrorMessage, !IO) :-
io.format(Stream, "Error: %s\n", [s(ErrorMessage)], !IO),
io.flush_output(Stream, !IO),
@@ -369,19 +355,19 @@ make_install_dir_command(Globals, SourceDirName, InstallDir) = Command :-
%---------------------------------------------------------------------------%
open_temp_output(Dir, Prefix, Suffix, Result, !IO) :-
open_temp_output_with_naming_scheme(Dir, Prefix, Suffix, Result, !IO) :-
% XXX Both open_temp_output and io.make_temp_file are ambiguous.
io.file.make_temp_file(Dir, Prefix, Suffix, TempFileResult, !IO),
open_temp_output_2(TempFileResult, Result, !IO).
open_temp_file(TempFileResult, Result, !IO).
open_temp_output(Result, !IO) :-
io.file.make_temp_file(TempFileResult, !IO),
open_temp_output_2(TempFileResult, Result, !IO).
open_temp_file(TempFileResult, Result, !IO).
:- pred open_temp_output_2(io.res(string)::in,
:- pred open_temp_file(io.res(string)::in,
maybe_error({string, text_output_stream})::out, io::di, io::uo) is det.
open_temp_output_2(TempFileResult, Result, !IO) :-
open_temp_file(TempFileResult, Result, !IO) :-
(
TempFileResult = ok(TempFileName),
io.open_output(TempFileName, OpenResult, !IO),
@@ -401,34 +387,6 @@ open_temp_output_2(TempFileResult, Result, !IO) :-
[s(error_message(Error))]))
).
open_temp_input(Result, Pred, !IO) :-
io.file.make_temp_file(TempFileResult, !IO),
(
TempFileResult = ok(TempFileName),
Pred(TempFileName, PredResult, !IO),
(
PredResult = ok,
io.open_input(TempFileName, OpenResult, !IO),
(
OpenResult = ok(Stream),
Result = ok({TempFileName, Stream})
;
OpenResult = error(Error),
Result = error(format("could not open `%s': %s",
[s(TempFileName), s(error_message(Error))])),
io.file.remove_file(TempFileName, _, !IO)
)
;
PredResult = error(ErrorMessage),
io.file.remove_file(TempFileName, _, !IO),
Result = error(ErrorMessage)
)
;
TempFileResult = error(Error),
Result = error(format("could not create temporary file: %s",
[s(error_message(Error))]))
).
%---------------------------------------------------------------------------%
:- end_module libs.file_util.
%---------------------------------------------------------------------------%

View File

@@ -60,8 +60,6 @@
:- implementation.
:- import_module libs.
:- import_module libs.file_util.
:- import_module mdbcomp.
:- import_module mdbcomp.sym_name.
:- import_module parse_tree.builtin_lib_types.
@@ -95,7 +93,8 @@ read_event_set(SpecsFileName, EventSetName, EventSpecMap, ErrorSpecs, !IO) :-
% those tools are not yet mature enough. When they are, we should switch
% to using them.
open_temp_input(TermFileResult, read_specs_file(SpecsFileName), !IO),
make_temp_file_and_open_it(read_specs_file(SpecsFileName),
TermFileResult, !IO),
(
TermFileResult = ok({TermFileName, TermStream}),
io.read(TermStream, TermReadRes, !IO),
@@ -135,6 +134,46 @@ read_event_set(SpecsFileName, EventSetName, EventSpecMap, ErrorSpecs, !IO) :-
ErrorSpecs = [ErrorSpec]
).
% make_temp_file_and_open_it(WritePred, Result, !IO):
%
% Create a temporary file and call WritePred which will write data to it.
% If successful, return the file's name and a freshly opened input stream.
% On error, remove the temporary file.
%
:- pred make_temp_file_and_open_it(
pred(string, maybe_error, io, io)::in(pred(in, out, di, uo) is det),
maybe_error({string, text_input_stream})::out,
io::di, io::uo) is det.
make_temp_file_and_open_it(WritePred, Result, !IO) :-
io.file.make_temp_file(TempFileResult, !IO),
(
TempFileResult = ok(TempFileName),
WritePred(TempFileName, PredResult, !IO),
(
PredResult = ok,
io.open_input(TempFileName, OpenResult, !IO),
(
OpenResult = ok(Stream),
Result = ok({TempFileName, Stream})
;
OpenResult = error(Error),
Result = error(format("could not open `%s': %s",
[s(TempFileName), s(error_message(Error))])),
io.file.remove_file(TempFileName, _, !IO)
)
;
PredResult = error(ErrorMessage),
io.file.remove_file(TempFileName, _, !IO),
Result = error(ErrorMessage)
)
;
TempFileResult = error(Error),
Result = error(format("could not create temporary file: %s",
[s(error_message(Error))]))
).
:- pred read_specs_file(string::in, string::in, maybe_error::out,
io::di, io::uo) is det.