[flang-commits] [PATCH] D114897: [flang] Don't close stderr in runtime (fixes STOP output)

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Dec 1 11:30:05 PST 2021


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
klausler requested review of this revision.

STOP statement output was sometimes failing to appear because
the runtime flushes and shuts down open Fortran units beforehand.
But when file descriptor 2 was closed, the STOP statement output
was suppressed.  The fix is to not actually close file descriptors
0-2 if they are connected to Fortran units being closed.  This was
already the policy when an OPEN statement was (re-)opening such a
unit, so that logic has been pulled out into a member function and
shared with CLOSE processing.


https://reviews.llvm.org/D114897

Files:
  flang/runtime/file.cpp
  flang/runtime/file.h


Index: flang/runtime/file.h
===================================================================
--- flang/runtime/file.h
+++ flang/runtime/file.h
@@ -85,6 +85,7 @@
     position_ = pos;
     openPosition_.reset();
   }
+  void CloseFd(IoErrorHandler &);
 
   int fd_{-1};
   OwningPtr<char> path_;
Index: flang/runtime/file.cpp
===================================================================
--- flang/runtime/file.cpp
+++ flang/runtime/file.cpp
@@ -66,16 +66,7 @@
       (status == OpenStatus::Old || status == OpenStatus::Unknown)) {
     return;
   }
-  if (fd_ >= 0) {
-    if (fd_ <= 2) {
-      // don't actually close a standard file descriptor, we might need it
-    } else {
-      if (::close(fd_) != 0) {
-        handler.SignalErrno();
-      }
-    }
-    fd_ = -1;
-  }
+  CloseFd(handler);
   if (status == OpenStatus::Scratch) {
     if (path_.get()) {
       handler.SignalError("FILE= must not appear with STATUS='SCRATCH'");
@@ -179,12 +170,7 @@
     break;
   }
   path_.reset();
-  if (fd_ >= 0) {
-    if (::close(fd_) != 0) {
-      handler.SignalErrno();
-    }
-    fd_ = -1;
-  }
+  CloseFd(handler);
 }
 
 std::size_t OpenFile::Read(FileOffset at, char *buffer, std::size_t minBytes,
@@ -410,6 +396,19 @@
   return id;
 }
 
+void OpenFile::CloseFd(IoErrorHandler &handler) {
+  if (fd_ >= 0) {
+    if (fd_ <= 2) {
+      // don't actually close a standard file descriptor, we might need it
+    } else {
+      if (::close(fd_) != 0) {
+        handler.SignalErrno();
+      }
+    }
+    fd_ = -1;
+  }
+}
+
 bool IsATerminal(int fd) { return ::isatty(fd); }
 
 #ifdef WIN32


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114897.391096.patch
Type: text/x-patch
Size: 1608 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20211201/0f56b946/attachment.bin>


More information about the flang-commits mailing list