From 9db19e13fbeaaf93014f754fa86e1ca7e0bcb63a Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Thu, 6 Nov 2025 13:09:57 -0800 Subject: [PATCH] [flang][runtime] Allow some list-directed child output to advance List-directed child input is allowed to advance to new records in some circumstances, and list-directed output should be as well so that e.g. NAMELIST output via a defined WRITE(FORMATTED) generic doesn't get truncated by FORT_FMT_RECL. Fixes https://github.com/llvm/llvm-project/issues/166804. --- flang-rt/include/flang-rt/runtime/io-stmt.h | 3 +-- flang-rt/lib/runtime/edit-output.cpp | 7 +++--- flang-rt/lib/runtime/io-stmt.cpp | 28 ++++++++++----------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/flang-rt/include/flang-rt/runtime/io-stmt.h b/flang-rt/include/flang-rt/runtime/io-stmt.h index f6a81f7cb8120..3c6bcfec8d0c4 100644 --- a/flang-rt/include/flang-rt/runtime/io-stmt.h +++ b/flang-rt/include/flang-rt/runtime/io-stmt.h @@ -730,8 +730,7 @@ class ChildListIoStatementState : public ChildIoStatementState, RT_API_ATTRS bool AdvanceRecord(int = 1); RT_API_ATTRS int EndIoStatement(); RT_API_ATTRS bool CanAdvance() { - return DIR == Direction::Input && - (canAdvance_ || this->mutableModes().inNamelist); + return canAdvance_ || this->mutableModes().inNamelist; } private: diff --git a/flang-rt/lib/runtime/edit-output.cpp b/flang-rt/lib/runtime/edit-output.cpp index f90b6fb10963f..73dba35ff08d9 100644 --- a/flang-rt/lib/runtime/edit-output.cpp +++ b/flang-rt/lib/runtime/edit-output.cpp @@ -175,9 +175,10 @@ bool RT_API_ATTRS EditIntegerOutput(IoStatementState &io, const DataEdit &edit, } if (edit.IsListDirected()) { int total{std::max(leadingSpaces, 1) + subTotal}; - if (io.GetConnectionState().NeedAdvance(static_cast(total)) && - !io.AdvanceRecord()) { - return false; + if (io.GetConnectionState().NeedAdvance(static_cast(total))) { + if (!io.AdvanceRecord()) { + return false; + } } leadingSpaces = 1; } else if (!edit.width) { diff --git a/flang-rt/lib/runtime/io-stmt.cpp b/flang-rt/lib/runtime/io-stmt.cpp index b958f23cf5342..a88fbe605f890 100644 --- a/flang-rt/lib/runtime/io-stmt.cpp +++ b/flang-rt/lib/runtime/io-stmt.cpp @@ -1109,20 +1109,20 @@ ChildListIoStatementState::ChildListIoStatementState( ChildIo &child, const char *sourceFile, int sourceLine) : ChildIoStatementState{child, sourceFile, sourceLine} { #if !defined(RT_DEVICE_AVOID_RECURSION) - if constexpr (DIR == Direction::Input) { - if (const auto *listInput{child.parent() - .get_if>()}) { - this->set_eatComma(listInput->eatComma()); - this->namelistGroup_ = listInput->namelistGroup(); - if (auto *childListInput{child.parent() - .get_if>()}) { - // Child list input whose parent is child list input: can advance - // if the parent can. - this->canAdvance_ = childListInput->CanAdvance(); - } else { - // Child list input of top-level list input: can advance. - this->canAdvance_ = true; - } + if (const auto *listParent{ + child.parent().get_if>()}) { + if constexpr (DIR == Direction::Input) { + this->set_eatComma(listParent->eatComma()); + this->namelistGroup_ = listParent->namelistGroup(); + } + if (auto *childListParent{ + child.parent().get_if>()}) { + // Child list I/O whose parent is child list I/O: can advance + // if the parent can. + this->canAdvance_ = childListParent->CanAdvance(); + } else { + // Child list I/O of top-level list I/O: can advance. + this->canAdvance_ = true; } } #else