[flang-commits] [flang] [flang][runtime] Don't write implied ENDFILE for REC=/POS= (PR #79637)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Jan 30 12:21:25 PST 2024


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/79637

>From 8b63eaf0eca35cb6f2e3d298d0c25a5c2b4e38e8 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Fri, 26 Jan 2024 11:06:31 -0800
Subject: [PATCH] [flang][runtime] Don't write implied ENDFILE for REC=/POS=

An implied ENDFILE record, which truncates an external file,
should be written to a sequential unit whenever the file is
repositioned for a BACKSPACE or REWIND statement if a WRITE
statement has executed since the last OPEN/BACKSPACE/REWIND.

But the REC= and POS= positioning specifiers don't apply to
sequential units (they're for direct and stream units, resp.),
so don't truncate the file when they're used.
---
 flang/runtime/unit.cpp | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 18590567c65eb..58ca313d9e445 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -679,6 +679,7 @@ void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
     handler.SignalError(IostatRewindNonSequential,
         "REWIND(UNIT=%d) on non-sequential file", unitNumber());
   } else {
+    DoImpliedEndfile(handler);
     SetPosition(0, handler);
     currentRecordNumber = 1;
     leftTabLimit.reset();
@@ -687,7 +688,6 @@ void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
 }
 
 void ExternalFileUnit::SetPosition(std::int64_t pos, IoErrorHandler &handler) {
-  DoImpliedEndfile(handler);
   frameOffsetInFile_ = pos;
   recordOffsetInFrame_ = 0;
   if (access == Access::Direct) {
@@ -707,6 +707,12 @@ bool ExternalFileUnit::SetStreamPos(
         "POS=%zd is invalid", static_cast<std::intmax_t>(oneBasedPos));
     return false;
   }
+  // A backwards POS= implies truncation after writing, at least in
+  // Intel and NAG.
+  if (static_cast<std::size_t>(oneBasedPos - 1) <
+      frameOffsetInFile_ + recordOffsetInFrame_) {
+    DoImpliedEndfile(handler);
+  }
   SetPosition(oneBasedPos - 1, handler);
   // We no longer know which record we're in.  Set currentRecordNumber to
   // a large value from whence we can both advance and backspace.



More information about the flang-commits mailing list