[flang-commits] [PATCH] D100352: [flang] More precise enforcement of runtime constraint
Peter Klausler via Phabricator via flang-commits
flang-commits at lists.llvm.org
Mon Apr 12 17:10:49 PDT 2021
klausler created this revision.
klausler added a reviewer: sscalpone.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
klausler requested review of this revision.
An OPEN statement that affects an already connected unit
without changing its external file is required to have
STATUS="OLD" or default STATUS=. The code was eliciting
spurious runtime errors in situations where an OPEN statement
pertained to an existing unit number but did not need to have
STATUS="OLD'.
https://reviews.llvm.org/D100352
Files:
flang/runtime/io-stmt.cpp
flang/runtime/unit.cpp
flang/runtime/unit.h
Index: flang/runtime/unit.h
===================================================================
--- flang/runtime/unit.h
+++ flang/runtime/unit.h
@@ -50,11 +50,11 @@
static void CloseAll(IoErrorHandler &);
static void FlushAll(IoErrorHandler &);
- void OpenUnit(OpenStatus, std::optional<Action>, Position,
+ void OpenUnit(std::optional<OpenStatus>, std::optional<Action>, Position,
OwningPtr<char> &&path, std::size_t pathLength, Convert,
IoErrorHandler &);
void OpenAnonymousUnit(
- OpenStatus, std::optional<Action>, Position, Convert, IoErrorHandler &);
+ std::optional<OpenStatus>, std::optional<Action>, Position, Convert, IoErrorHandler &);
void CloseUnit(CloseStatus, IoErrorHandler &);
void DestroyClosed();
Index: flang/runtime/unit.cpp
===================================================================
--- flang/runtime/unit.cpp
+++ flang/runtime/unit.cpp
@@ -89,7 +89,7 @@
return GetUnitMap().NewUnit(terminator).unitNumber();
}
-void ExternalFileUnit::OpenUnit(OpenStatus status, std::optional<Action> action,
+void ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status, std::optional<Action> action,
Position position, OwningPtr<char> &&newPath, std::size_t newPathLength,
Convert convert, IoErrorHandler &handler) {
if (executionEnvironment.conversion != Convert::Unknown) {
@@ -99,11 +99,13 @@
(convert == Convert::LittleEndian && !isHostLittleEndian) ||
(convert == Convert::BigEndian && isHostLittleEndian);
if (IsOpen()) {
- if (status == OpenStatus::Old &&
- (!newPath.get() ||
- (path() && pathLength() == newPathLength &&
- std::memcmp(path(), newPath.get(), newPathLength) == 0))) {
- // OPEN of existing unit, STATUS='OLD', not new FILE=
+ 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;
+ }
+ if (!newPath.get() || isSamePath) {
+ // OPEN of existing unit, STATUS='OLD' or unspecified, not new FILE=
newPath.reset();
return;
}
@@ -113,7 +115,7 @@
Close(CloseStatus::Keep, handler);
}
set_path(std::move(newPath), newPathLength);
- Open(status, action, position, handler);
+ Open(status.value_or(OpenStatus::Unknown), action, position, handler);
auto totalBytes{knownSize()};
if (access == Access::Direct) {
if (!isFixedRecordLength || !recordLength) {
@@ -146,7 +148,7 @@
}
}
-void ExternalFileUnit::OpenAnonymousUnit(OpenStatus status,
+void ExternalFileUnit::OpenAnonymousUnit(std::optional<OpenStatus> status,
std::optional<Action> action, Position position, Convert convert,
IoErrorHandler &handler) {
// I/O to an unconnected unit reads/creates a local file, e.g. fort.7
Index: flang/runtime/io-stmt.cpp
===================================================================
--- flang/runtime/io-stmt.cpp
+++ flang/runtime/io-stmt.cpp
@@ -188,16 +188,12 @@
}
int OpenStatementState::EndIoStatement() {
- if (wasExtant_ && status_ && *status_ != OpenStatus::Old) {
- SignalError("OPEN statement for connected unit may not have STATUS= other "
- "than 'OLD'");
- }
if (path_.get() || wasExtant_ ||
(status_ && *status_ == OpenStatus::Scratch)) {
- unit().OpenUnit(status_.value_or(OpenStatus::Unknown), action_, position_,
+ unit().OpenUnit(status_, action_, position_,
std::move(path_), pathLength_, convert_, *this);
} else {
- unit().OpenAnonymousUnit(status_.value_or(OpenStatus::Unknown), action_,
+ unit().OpenAnonymousUnit(status_, action_,
position_, convert_, *this);
}
if (access_) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100352.336996.patch
Type: text/x-patch
Size: 3873 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20210413/6d96dc0e/attachment.bin>
More information about the flang-commits
mailing list