[flang-commits] [flang] [flang][runtime] Support READ after WRITE w/o positioning (PR #74650)
via flang-commits
flang-commits at lists.llvm.org
Wed Dec 6 12:03:35 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-runtime
Author: Peter Klausler (klausler)
<details>
<summary>Changes</summary>
Most Fortran implementations support a READ statement after a WRITE without repositioning on a sequential unit; it implies on ENDFILE and then hits an EOF condition.
Fixes llvm-test-suite/Fortran/gfortran/regression/backspace_2.f.
---
Full diff: https://github.com/llvm/llvm-project/pull/74650.diff
2 Files Affected:
- (modified) flang/runtime/unit.cpp (+9)
- (modified) flang/runtime/unit.h (+1)
``````````diff
diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 995656b9480c4..b069e93caf354 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -351,6 +351,7 @@ bool ExternalFileUnit::Emit(const char *data, std::size_t bytes,
}
positionInRecord += bytes;
furthestPositionInRecord = furthestAfter;
+ anyWriteSinceLastPositioning_ = true;
return true;
}
@@ -447,6 +448,11 @@ bool ExternalFileUnit::BeginReadingRecord(IoErrorHandler &handler) {
HitEndOnRead(handler);
}
} else {
+ if (anyWriteSinceLastPositioning_) {
+ // Most Fortran implementations allow a READ after a WRITE;
+ // it just hits an EOF.
+ DoEndfile(handler);
+ }
recordLength.reset();
if (IsAtEOF()) {
handler.SignalEnd();
@@ -611,6 +617,7 @@ void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
}
}
BeginRecord();
+ anyWriteSinceLastPositioning_ = false;
}
}
@@ -663,6 +670,7 @@ void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
SetPosition(0, handler);
currentRecordNumber = 1;
leftTabLimit.reset();
+ anyWriteSinceLastPositioning_ = false;
}
}
@@ -924,6 +932,7 @@ void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
TruncateFrame(frameOffsetInFile_, handler);
BeginRecord();
impliedEndfile_ = false;
+ anyWriteSinceLastPositioning_ = false;
}
void ExternalFileUnit::CommitWrites() {
diff --git a/flang/runtime/unit.h b/flang/runtime/unit.h
index 1ec3013ba82b3..140fda3c4d2a8 100644
--- a/flang/runtime/unit.h
+++ b/flang/runtime/unit.h
@@ -140,6 +140,7 @@ class ExternalFileUnit : public ConnectionState,
Direction direction_{Direction::Output};
bool impliedEndfile_{false}; // sequential/stream output has taken place
bool beganReadingRecord_{false};
+ bool anyWriteSinceLastPositioning_{false};
bool directAccessRecWasSet_{false}; // REC= appeared
// Subtle: The beginning of the frame can't be allowed to advance
// during a single list-directed READ due to the possibility of a
``````````
</details>
https://github.com/llvm/llvm-project/pull/74650
More information about the flang-commits
mailing list