[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
Tue Dec 5 13:13:50 PST 2023


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/74524

After a non-advancing WRITE to a unit, ensure that any ENDFILE operation (explicit or implicit) terminates the record. (All other Fortran implementations do so except XLF.)

Fixes llvm-test-suite/Fortran/gfortran/regression/advance_6.f90.

>From c96d3c4e24d043d7f0e26f804dd6f59c00b6c15f 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) 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 | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 995656b9480c4..3895eb5c40894 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -591,9 +591,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) {
@@ -910,10 +907,8 @@ 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 (direction_ == Direction::Output && leftTabLimit) {
+      AdvanceRecord(handler); // last write was non-advancing
     }
     endfileRecordNumber = currentRecordNumber;
   }



More information about the flang-commits mailing list