This is an archive of the discontinued LLVM Phabricator instance.

[flang] Debugging of ACCESS='STREAM' I/O (take 2)
ClosedPublic

Authored by klausler on Feb 4 2022, 9:43 AM.

Details

Summary

Corrects the runtime implementation of I/O on files with
the access mode ACCESS='STREAM'. This is a collection
of edge-case tweaks to ensure that the distinctions between
stream and direct/sequential files, unformatted or formatted,
are respected where appropriate.

Moves NextInField() from io-stmt.h to io-stmt.cpp --
it was getting too big to keep in a header.

The original version of this patch exposed a problem with
the I/O runtime on Windows and it was reverted. This new
version also fixes that problem; files are now opened on Windows
in binary mode to prevent inadvertent insertions of
carriage returns before line feeds, and those line
endings (CR+LF) are now explicitly generated.

Diff Detail

Event Timeline

klausler created this revision.Feb 4 2022, 9:43 AM
klausler requested review of this revision.Feb 4 2022, 9:43 AM
Meinersbur added inline comments.Feb 4 2022, 10:51 AM
flang/runtime/file.cpp
164

I have a concern here: If fd is <=2, (stdout or stderr), it will be in text mode, which expects a single \n for a newline, but with isWindowsTextFile_ = true, we will write \r\n (which the c-runtime expands to \r\r\n).

klausler marked an inline comment as done.Feb 4 2022, 10:58 AM
klausler added inline comments.
flang/runtime/file.cpp
164

When isWindowsTextFile_ is true, only the \n will be written. See unit.cpp line 557 -- the emission of the \r is controlled by "if (!isWindowsTextFile())" (note the negation).

klausler marked an inline comment as done.Feb 4 2022, 11:36 AM
Meinersbur accepted this revision.Feb 4 2022, 1:18 PM

LGTM

flang/runtime/file.cpp
164

Correct, I was inverting what the flag means. Hence, effectively with OpenFile::Open the file will be opened in binary mode write \r\n and with OpenFile::Predefine text mode is assumed and only \n is written.

Still means that with OpenFile::Predefine lineEndingBytes is set to 2 even though it effectively writes \r\n, but since stdout/stderr is not generalably seekable I don't think it matters.

This revision is now accepted and ready to land.Feb 4 2022, 1:18 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 4 2022, 6:02 PM