[flang-commits] [flang] 0508fd5 - [flang][runtime] Make ENDFILE work after non-advancing READ

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Jul 6 17:30:45 PDT 2022


Author: Peter Klausler
Date: 2022-07-06T17:30:33-07:00
New Revision: 0508fd5935d91a684d178613b9bbf75760c41686

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

LOG: [flang][runtime] Make ENDFILE work after non-advancing READ

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

Added: 
    

Modified: 
    flang/runtime/unit.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index e0daf8c24a238..a96b43d6a2c9f 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -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;
 }


        


More information about the flang-commits mailing list