[flang-commits] [flang] d90e866 - [flang][runtime] INQUIRE(UNIT=666, NUMBER=n) must set n=666

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue May 24 09:48:43 PDT 2022


Author: Peter Klausler
Date: 2022-05-24T09:48:32-07:00
New Revision: d90e866a191fb77bb20140c6a955a4b3258c6ee3

URL: https://github.com/llvm/llvm-project/commit/d90e866a191fb77bb20140c6a955a4b3258c6ee3
DIFF: https://github.com/llvm/llvm-project/commit/d90e866a191fb77bb20140c6a955a4b3258c6ee3.diff

LOG: [flang][runtime] INQUIRE(UNIT=666,NUMBER=n) must set n=666

Whether a unit number in an inquire-by-unit statement is valid or not,
it should be the value to which the NUMBER= variable is set, not -1.
-1 should be returned to NUMBER= only for an inquire-by-file statement
when the FILE= is not connected to any unit.

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

Added: 
    

Modified: 
    flang/runtime/io-api.cpp
    flang/runtime/io-stmt.cpp
    flang/runtime/io-stmt.h

Removed: 
    


################################################################################
diff  --git a/flang/runtime/io-api.cpp b/flang/runtime/io-api.cpp
index 18e1a075855a..a50f01131b7a 100644
--- a/flang/runtime/io-api.cpp
+++ b/flang/runtime/io-api.cpp
@@ -360,7 +360,7 @@ Cookie IONAME(BeginClose)(
   } else {
     // CLOSE(UNIT=bad unit) is just a no-op
     Terminator oom{sourceFile, sourceLine};
-    return &New<NoopStatementState>{oom}(sourceFile, sourceLine)
+    return &New<NoopStatementState>{oom}(sourceFile, sourceLine, unitNumber)
                 .release()
                 ->ioStatementState();
   }
@@ -374,7 +374,7 @@ Cookie IONAME(BeginFlush)(
   } else {
     // FLUSH(UNIT=unknown) is a no-op
     Terminator oom{sourceFile, sourceLine};
-    return &New<NoopStatementState>{oom}(sourceFile, sourceLine)
+    return &New<NoopStatementState>{oom}(sourceFile, sourceLine, unitNumber)
                 .release()
                 ->ioStatementState();
   }
@@ -420,7 +420,7 @@ Cookie IONAME(BeginInquireUnit)(
   } else {
     // INQUIRE(UNIT=unrecognized unit)
     Terminator oom{sourceFile, sourceLine};
-    return &New<InquireNoUnitState>{oom}(sourceFile, sourceLine)
+    return &New<InquireNoUnitState>{oom}(sourceFile, sourceLine, unitNumber)
                 .release()
                 ->ioStatementState();
   }

diff  --git a/flang/runtime/io-stmt.cpp b/flang/runtime/io-stmt.cpp
index 237d2648b61d..78fae20a173c 100644
--- a/flang/runtime/io-stmt.cpp
+++ b/flang/runtime/io-stmt.cpp
@@ -1270,7 +1270,7 @@ bool InquireUnitState::Inquire(
     }
     return true;
   case HashInquiryKeyword("NUMBER"):
-    result = unit().IsConnected() ? unit().unitNumber() : -1;
+    result = unit().unitNumber();
     return true;
   case HashInquiryKeyword("POS"):
     result = unit().InquirePos();
@@ -1300,8 +1300,9 @@ bool InquireUnitState::Inquire(
   }
 }
 
-InquireNoUnitState::InquireNoUnitState(const char *sourceFile, int sourceLine)
-    : NoUnitIoStatementState{sourceFile, sourceLine, *this} {}
+InquireNoUnitState::InquireNoUnitState(
+    const char *sourceFile, int sourceLine, int badUnitNumber)
+    : NoUnitIoStatementState{*this, sourceFile, sourceLine, badUnitNumber} {}
 
 bool InquireNoUnitState::Inquire(
     InquiryKeywordHash inquiry, char *result, std::size_t length) {
@@ -1370,8 +1371,10 @@ bool InquireNoUnitState::Inquire(
 bool InquireNoUnitState::Inquire(
     InquiryKeywordHash inquiry, std::int64_t &result) {
   switch (inquiry) {
-  case HashInquiryKeyword("NEXTREC"):
   case HashInquiryKeyword("NUMBER"):
+    result = badUnitNumber();
+    return true;
+  case HashInquiryKeyword("NEXTREC"):
   case HashInquiryKeyword("POS"):
   case HashInquiryKeyword("RECL"):
   case HashInquiryKeyword("SIZE"):
@@ -1385,7 +1388,7 @@ bool InquireNoUnitState::Inquire(
 
 InquireUnconnectedFileState::InquireUnconnectedFileState(
     OwningPtr<char> &&path, const char *sourceFile, int sourceLine)
-    : NoUnitIoStatementState{sourceFile, sourceLine, *this}, path_{std::move(
+    : NoUnitIoStatementState{*this, sourceFile, sourceLine}, path_{std::move(
                                                                  path)} {}
 
 bool InquireUnconnectedFileState::Inquire(
@@ -1491,7 +1494,7 @@ bool InquireUnconnectedFileState::Inquire(
 
 InquireIOLengthState::InquireIOLengthState(
     const char *sourceFile, int sourceLine)
-    : NoUnitIoStatementState{sourceFile, sourceLine, *this} {}
+    : NoUnitIoStatementState{*this, sourceFile, sourceLine} {}
 
 bool InquireIOLengthState::Emit(const char *, std::size_t n, std::size_t) {
   bytes_ += n;

diff  --git a/flang/runtime/io-stmt.h b/flang/runtime/io-stmt.h
index c9bda49458f0..16a0e031403f 100644
--- a/flang/runtime/io-stmt.h
+++ b/flang/runtime/io-stmt.h
@@ -588,23 +588,28 @@ class NoUnitIoStatementState : public IoStatementBase {
   IoStatementState &ioStatementState() { return ioStatementState_; }
   MutableModes &mutableModes() { return connection_.modes; }
   ConnectionState &GetConnectionState() { return connection_; }
+  int badUnitNumber() const { return badUnitNumber_; }
   void CompleteOperation();
   int EndIoStatement();
 
 protected:
   template <typename A>
-  NoUnitIoStatementState(const char *sourceFile, int sourceLine, A &stmt)
-      : IoStatementBase{sourceFile, sourceLine}, ioStatementState_{stmt} {}
+  NoUnitIoStatementState(A &stmt, const char *sourceFile = nullptr,
+      int sourceLine = 0, int badUnitNumber = -1)
+      : IoStatementBase{sourceFile, sourceLine}, ioStatementState_{stmt},
+        badUnitNumber_{badUnitNumber} {}
 
 private:
   IoStatementState ioStatementState_; // points to *this
   ConnectionState connection_;
+  int badUnitNumber_;
 };
 
 class NoopStatementState : public NoUnitIoStatementState {
 public:
-  NoopStatementState(const char *sourceFile, int sourceLine)
-      : NoUnitIoStatementState{sourceFile, sourceLine, *this} {}
+  NoopStatementState(
+      const char *sourceFile = nullptr, int sourceLine = 0, int unitNumber = -1)
+      : NoUnitIoStatementState{*this, sourceFile, sourceLine, unitNumber} {}
   void set_status(CloseStatus) {} // discards
 };
 
@@ -656,7 +661,8 @@ class InquireUnitState : public ExternalIoStatementBase {
 
 class InquireNoUnitState : public NoUnitIoStatementState {
 public:
-  InquireNoUnitState(const char *sourceFile = nullptr, int sourceLine = 0);
+  InquireNoUnitState(const char *sourceFile = nullptr, int sourceLine = 0,
+      int badUnitNumber = -1);
   bool Inquire(InquiryKeywordHash, char *, std::size_t);
   bool Inquire(InquiryKeywordHash, bool &);
   bool Inquire(InquiryKeywordHash, std::int64_t, bool &);


        


More information about the flang-commits mailing list