[flang] Catch bad OPEN(STATUS=) cases
authorPeter Klausler <pklausler@nvidia.com>
Thu, 24 Mar 2022 19:59:04 +0000 (12:59 -0700)
committerPeter Klausler <pklausler@nvidia.com>
Sat, 26 Mar 2022 01:24:50 +0000 (18:24 -0700)
STATUS='NEW' and 'REPLACE' require FILE= to be present.
STATUS='SCRATCH' may not appear with FILE=.

These errors are caught at compilation time when constant character
strings are used in an OPEN statement, but the runtime needs
to enforce them as well to catch errors in OPEN statements
with character variables and expressions.

Differential Revision: https://reviews.llvm.org/D122509

flang/runtime/io-stmt.cpp

index ade7f9c..0f51d24 100644 (file)
@@ -232,6 +232,14 @@ void OpenStatementState::CompleteOperation() {
       position_.reset();
     }
   }
+  if (status_) { // 12.5.6.10
+    if ((*status_ == OpenStatus::New || *status_ == OpenStatus::Replace) &&
+        !path_.get()) {
+      SignalError("FILE= required on OPEN with STATUS='NEW' or 'REPLACE'");
+    } else if (*status_ == OpenStatus::Scratch && path_.get()) {
+      SignalError("FILE= may not appear on OPEN with STATUS='SCRATCH'");
+    }
+  }
   if (path_.get() || wasExtant_ ||
       (status_ && *status_ == OpenStatus::Scratch)) {
     unit().OpenUnit(status_, action_, position_.value_or(Position::AsIs),