[flang-commits] [flang] [flang][runtime] Terminate last partial record after non-advancing write (PR #74524)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Fri Dec 8 14:16:39 PST 2023
https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/74524
>From 19aa79ca5e53e510250cdaa0afb2842418193f40 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 5 Dec 2023 13:10:11 -0800
Subject: [PATCH] [flang][runtime] Terminate last partial record after
non-advancing write
After a non-advancing WRITE to a unit, ensure that any ENDFILE
operation (explicit or implicit) flushes and, if not stream output,
terminates the record. (All other Fortran implementations do so
except XLF.)
Fixes llvm-test-suite/Fortran/gfortran/regression/advance_6.f90.
---
flang/runtime/unit.cpp | 33 ++++++++++++++++-----------------
1 file changed, 16 insertions(+), 17 deletions(-)
diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 5fa8565c2f61fc..7758a3f6880899 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -595,9 +595,6 @@ void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
if (IsAfterEndfile()) {
// BACKSPACE after explicit ENDFILE
currentRecordNumber = *endfileRecordNumber;
- } else if (leftTabLimit) {
- // BACKSPACE after non-advancing I/O
- leftTabLimit.reset();
} else {
DoImpliedEndfile(handler);
if (frameOffsetInFile_ + recordOffsetInFrame_ > 0) {
@@ -896,28 +893,30 @@ 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) {
+ // 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) {
+ if (IsRecordFile() && access != Access::Direct &&
+ direction_ == Direction::Output) {
furthestPositionInRecord =
std::max(positionInRecord, furthestPositionInRecord);
- if (leftTabLimit) {
- // Last read/write was non-advancing, so AdvanceRecord() was not called.
- leftTabLimit.reset();
- ++currentRecordNumber;
+ if (leftTabLimit) { // last write was non-advancing
+ if (access == Access::Sequential) {
+ AdvanceRecord(handler);
+ } else { // Access::Stream
+ leftTabLimit.reset();
+ ++currentRecordNumber;
+ }
}
endfileRecordNumber = currentRecordNumber;
}
More information about the flang-commits
mailing list