Skip to content

Commit

Permalink
[flang][runtime] INQUIRE(UNIT=666,NUMBER=n) must set n=666
Browse files Browse the repository at this point in the history
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
  • Loading branch information
klausler committed May 24, 2022
1 parent 28432b0 commit d90e866
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
6 changes: 3 additions & 3 deletions flang/runtime/io-api.cpp
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down
15 changes: 9 additions & 6 deletions flang/runtime/io-stmt.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"):
Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 11 additions & 5 deletions flang/runtime/io-stmt.h
Expand Up @@ -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
};

Expand Down Expand Up @@ -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 &);
Expand Down

0 comments on commit d90e866

Please sign in to comment.