Skip to content

Commit

Permalink
[flang][runtime] Make ENDFILE work after non-advancing READ
Browse files Browse the repository at this point in the history
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
  • Loading branch information
klausler committed Jul 7, 2022
1 parent aeaca85 commit 0508fd5
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions flang/runtime/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 0508fd5

Please sign in to comment.