[flang-commits] [PATCH] D155967: [flang][runtime] Detect NEWUNIT= without FILE= or STATUS='SCRATCH'

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Jul 21 08:27:50 PDT 2023


klausler created this revision.
klausler added a reviewer: vzakhari.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

It is an error to open a new unit with OPEN(NEWUNIT=) and have   
neither a file name nor a scratch status.  Catch it, and report a
new error code.


https://reviews.llvm.org/D155967

Files:
  flang/include/flang/Runtime/iostat.h
  flang/runtime/io-api.cpp
  flang/runtime/io-stmt.cpp
  flang/runtime/io-stmt.h
  flang/runtime/iostat.cpp


Index: flang/runtime/iostat.cpp
===================================================================
--- flang/runtime/iostat.cpp
+++ flang/runtime/iostat.cpp
@@ -111,6 +111,8 @@
     return "FLUSH attempted on a bad or unconnected unit number";
   case IostatBadOpOnChildUnit:
     return "Impermissible I/O statement on child I/O unit";
+  case IostatBadNewUnit:
+    return "NEWUNIT= without FILE= or STATUS='SCRATCH'";
   default:
     return nullptr;
   }
Index: flang/runtime/io-stmt.h
===================================================================
--- flang/runtime/io-stmt.h
+++ flang/runtime/io-stmt.h
@@ -537,10 +537,10 @@
 // OPEN
 class OpenStatementState : public ExternalIoStatementBase {
 public:
-  OpenStatementState(ExternalFileUnit &unit, bool wasExtant,
+  OpenStatementState(ExternalFileUnit &unit, bool wasExtant, bool isNewUnit,
       const char *sourceFile = nullptr, int sourceLine = 0)
-      : ExternalIoStatementBase{unit, sourceFile, sourceLine}, wasExtant_{
-                                                                   wasExtant} {}
+      : ExternalIoStatementBase{unit, sourceFile, sourceLine},
+        wasExtant_{wasExtant}, isNewUnit_{isNewUnit} {}
   bool wasExtant() const { return wasExtant_; }
   void set_status(OpenStatus status) { status_ = status; } // STATUS=
   void set_path(const char *, std::size_t); // FILE=
@@ -555,6 +555,7 @@
 
 private:
   bool wasExtant_;
+  bool isNewUnit_;
   std::optional<OpenStatus> status_;
   std::optional<Position> position_;
   std::optional<Action> action_;
Index: flang/runtime/io-stmt.cpp
===================================================================
--- flang/runtime/io-stmt.cpp
+++ flang/runtime/io-stmt.cpp
@@ -240,6 +240,12 @@
       SignalError("FILE= may not appear on OPEN with STATUS='SCRATCH'");
     }
   }
+  // F'2023 12.5.6.13 - NEWUNIT= requires either FILE= or STATUS='SCRATCH'
+  if (isNewUnit_ && !path_.get() &&
+      status_.value_or(OpenStatus::Unknown) != OpenStatus::Scratch) {
+    SignalError(IostatBadNewUnit);
+    status_ = OpenStatus::Scratch; // error recovery
+  }
   if (path_.get() || wasExtant_ ||
       (status_ && *status_ == OpenStatus::Scratch)) {
     unit().OpenUnit(status_, action_, position_.value_or(Position::AsIs),
Index: flang/runtime/io-api.cpp
===================================================================
--- flang/runtime/io-api.cpp
+++ flang/runtime/io-api.cpp
@@ -379,8 +379,8 @@
           IostatBadOpOnChildUnit, nullptr /* no unit */, sourceFile,
           sourceLine);
     } else {
-      return &unit->BeginIoStatement<OpenStatementState>(
-          terminator, *unit, wasExtant, sourceFile, sourceLine);
+      return &unit->BeginIoStatement<OpenStatementState>(terminator, *unit,
+          wasExtant, false /*not NEWUNIT=*/, sourceFile, sourceLine);
     }
   } else {
     return NoopUnit(terminator, unitNumber, IostatBadUnitNumber);
@@ -392,8 +392,9 @@
   Terminator terminator{sourceFile, sourceLine};
   ExternalFileUnit &unit{
       ExternalFileUnit::NewUnit(terminator, false /*not child I/O*/)};
-  return &unit.BeginIoStatement<OpenStatementState>(
-      terminator, unit, false /*was an existing file*/, sourceFile, sourceLine);
+  return &unit.BeginIoStatement<OpenStatementState>(terminator, unit,
+      false /*was an existing file*/, true /*NEWUNIT=*/, sourceFile,
+      sourceLine);
 }
 
 Cookie IONAME(BeginWait)(ExternalUnit unitNumber, AsynchronousId id,
Index: flang/include/flang/Runtime/iostat.h
===================================================================
--- flang/include/flang/Runtime/iostat.h
+++ flang/include/flang/Runtime/iostat.h
@@ -83,6 +83,7 @@
   IostatBadUnitNumber,
   IostatBadFlushUnit,
   IostatBadOpOnChildUnit,
+  IostatBadNewUnit,
 };
 
 const char *IostatErrorString(int);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155967.542936.patch
Type: text/x-patch
Size: 3801 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230721/d3394ef5/attachment-0001.bin>


More information about the flang-commits mailing list