Skip to content

[flang][runtime] Terminate last partial record after non-advancing write #74524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions flang/runtime/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,8 @@ void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
if (IsAfterEndfile()) {
// BACKSPACE after explicit ENDFILE
currentRecordNumber = *endfileRecordNumber;
} else if (leftTabLimit) {
// BACKSPACE after non-advancing I/O
} else if (leftTabLimit && direction_ == Direction::Input) {
// BACKSPACE after non-advancing input
leftTabLimit.reset();
} else {
DoImpliedEndfile(handler);
Expand Down Expand Up @@ -896,28 +896,29 @@ void ExternalFileUnit::BackspaceVariableFormattedRecord(
}

void ExternalFileUnit::DoImpliedEndfile(IoErrorHandler &handler) {
if (!impliedEndfile_ && direction_ == Direction::Output && IsRecordFile() &&
access != Access::Direct && leftTabLimit) {
// Complete partial record after non-advancing write before
// positioning or closing the unit. Usually sets impliedEndfile_.
AdvanceRecord(handler);
}
if (impliedEndfile_) {
impliedEndfile_ = false;
if (access != Access::Direct && IsRecordFile() && mayPosition()) {
if (access != Access::Direct) {
if (!impliedEndfile_ && leftTabLimit && direction_ == Direction::Output) {
// Flush a partial record after non-advancing output
impliedEndfile_ = true;
}
if (impliedEndfile_ && mayPosition()) {
DoEndfile(handler);
}
}
impliedEndfile_ = false;
}

void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
if (IsRecordFile() && access != Access::Direct) {
furthestPositionInRecord =
std::max(positionInRecord, furthestPositionInRecord);
if (leftTabLimit) {
// Last read/write was non-advancing, so AdvanceRecord() was not called.
leftTabLimit.reset();
++currentRecordNumber;
if (leftTabLimit) { // last I/O was non-advancing
if (access == Access::Sequential && direction_ == Direction::Output) {
AdvanceRecord(handler);
} else { // Access::Stream or input
leftTabLimit.reset();
++currentRecordNumber;
}
}
endfileRecordNumber = currentRecordNumber;
}
Expand Down