[flang-commits] [flang] c94f780 - [flang][runtime] Support READ after WRITE w/o positioning (#74650)

via flang-commits flang-commits at lists.llvm.org
Mon Dec 11 12:56:38 PST 2023


Author: Peter Klausler
Date: 2023-12-11T12:56:34-08:00
New Revision: c94f7804877ed39a67d58efa27ea190170d30bc3

URL: https://github.com/llvm/llvm-project/commit/c94f7804877ed39a67d58efa27ea190170d30bc3
DIFF: https://github.com/llvm/llvm-project/commit/c94f7804877ed39a67d58efa27ea190170d30bc3.diff

LOG: [flang][runtime] Support READ after WRITE w/o positioning (#74650)

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.

Added: 
    

Modified: 
    flang/runtime/unit.cpp
    flang/runtime/unit.h

Removed: 
    


################################################################################
diff  --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 962f97bf260cc..e4f346ae941f3 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -355,6 +355,7 @@ bool ExternalFileUnit::Emit(const char *data, std::size_t bytes,
   }
   positionInRecord += bytes;
   furthestPositionInRecord = furthestAfter;
+  anyWriteSinceLastPositioning_ = true;
   return true;
 }
 
@@ -459,6 +460,11 @@ bool ExternalFileUnit::BeginReadingRecord(IoErrorHandler &handler) {
         HitEndOnRead(handler);
       }
     } else {
+      if (anyWriteSinceLastPositioning_ && access == Access::Sequential) {
+        // Most Fortran implementations allow a READ after a WRITE;
+        // the read then just hits an EOF.
+        DoEndfile(handler);
+      }
       recordLength.reset();
       RUNTIME_CHECK(handler, isUnformatted.has_value());
       if (*isUnformatted) {
@@ -619,6 +625,7 @@ void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
       }
     }
     BeginRecord();
+    anyWriteSinceLastPositioning_ = false;
   }
 }
 
@@ -671,6 +678,7 @@ void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
     SetPosition(0, handler);
     currentRecordNumber = 1;
     leftTabLimit.reset();
+    anyWriteSinceLastPositioning_ = false;
   }
 }
 
@@ -934,6 +942,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


        


More information about the flang-commits mailing list