Skip to content

[flang][runtime] When OPEN implies CLOSE, disable later extant unit c… #69390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions flang/runtime/io-stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,10 @@ void OpenStatementState::CompleteOperation() {
}
if (path_.get() || wasExtant_ ||
(status_ && *status_ == OpenStatus::Scratch)) {
unit().OpenUnit(status_, action_, position_.value_or(Position::AsIs),
std::move(path_), pathLength_, convert_, *this);
if (unit().OpenUnit(status_, action_, position_.value_or(Position::AsIs),
std::move(path_), pathLength_, convert_, *this)) {
wasExtant_ = false; // existing unit was closed
}
} else {
unit().OpenAnonymousUnit(
status_, action_, position_.value_or(Position::AsIs), convert_, *this);
Expand Down
11 changes: 7 additions & 4 deletions flang/runtime/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ ExternalFileUnit &ExternalFileUnit::NewUnit(
return unit;
}

void ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status,
bool ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status,
std::optional<Action> action, Position position, OwningPtr<char> &&newPath,
std::size_t newPathLength, Convert convert, IoErrorHandler &handler) {
if (convert == Convert::Unknown) {
Expand All @@ -99,24 +99,26 @@ void ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status,
swapEndianness_ = convert == Convert::Swap ||
(convert == Convert::LittleEndian && !isHostLittleEndian) ||
(convert == Convert::BigEndian && isHostLittleEndian);
bool impliedClose{false};
if (IsConnected()) {
bool isSamePath{newPath.get() && path() && pathLength() == newPathLength &&
std::memcmp(path(), newPath.get(), newPathLength) == 0};
if (status && *status != OpenStatus::Old && isSamePath) {
handler.SignalError("OPEN statement for connected unit may not have "
"explicit STATUS= other than 'OLD'");
return;
return impliedClose;
}
if (!newPath.get() || isSamePath) {
// OPEN of existing unit, STATUS='OLD' or unspecified, not new FILE=
newPath.reset();
return;
return impliedClose;
}
// Otherwise, OPEN on open unit with new FILE= implies CLOSE
DoImpliedEndfile(handler);
FlushOutput(handler);
TruncateFrame(0, handler);
Close(CloseStatus::Keep, handler);
impliedClose = true;
}
if (newPath.get() && newPathLength > 0) {
if (const auto *already{
Expand All @@ -125,7 +127,7 @@ void ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status,
"OPEN(UNIT=%d,FILE='%.*s'): file is already connected to unit %d",
unitNumber_, static_cast<int>(newPathLength), newPath.get(),
already->unitNumber_);
return;
return impliedClose;
}
}
set_path(std::move(newPath), newPathLength);
Expand Down Expand Up @@ -166,6 +168,7 @@ void ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status,
currentRecordNumber = *endfileRecordNumber;
}
}
return impliedClose;
}

void ExternalFileUnit::OpenAnonymousUnit(std::optional<OpenStatus> status,
Expand Down
3 changes: 2 additions & 1 deletion flang/runtime/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class ExternalFileUnit : public ConnectionState,
static void CloseAll(IoErrorHandler &);
static void FlushAll(IoErrorHandler &);

void OpenUnit(std::optional<OpenStatus>, std::optional<Action>, Position,
// Returns true if an existing unit was closed
bool OpenUnit(std::optional<OpenStatus>, std::optional<Action>, Position,
OwningPtr<char> &&path, std::size_t pathLength, Convert,
IoErrorHandler &);
void OpenAnonymousUnit(std::optional<OpenStatus>, std::optional<Action>,
Expand Down