[flang][runtime] Make ENDFILE work after non-advancing READ

An ENDFILE statement executed when a non-advancing READ has
left the unit in the middle of a record must truncate the file
at that position.

Differential Revision: https://reviews.llvm.org/D129019
This commit is contained in:
Peter Klausler 2022-06-28 17:48:15 -07:00
parent aeaca854d1
commit 0508fd5935
1 changed files with 8 additions and 4 deletions

View File

@ -887,16 +887,20 @@ void ExternalFileUnit::DoImpliedEndfile(IoErrorHandler &handler) {
void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
if (IsRecordFile() && access != Access::Direct) {
furthestPositionInRecord =
std::max(positionInRecord, furthestPositionInRecord);
if (furthestPositionInRecord > 0) {
// Last write was non-advancing, so AdvanceRecord() was not called.
// Last read/write was non-advancing, so AdvanceRecord() was not called.
leftTabLimit.reset();
++currentRecordNumber;
}
endfileRecordNumber = currentRecordNumber;
}
FlushOutput(handler);
Truncate(frameOffsetInFile_ + recordOffsetInFrame_ + furthestPositionInRecord,
handler);
frameOffsetInFile_ += recordOffsetInFrame_ + furthestPositionInRecord;
recordOffsetInFrame_ = 0;
// Flush (if dirty) and reset the frame (even if reading)
WriteFrame(frameOffsetInFile_, 0, handler);
Truncate(frameOffsetInFile_, handler);
BeginRecord();
impliedEndfile_ = false;
}