[llvm] [flang][runtime] Handle null list-directed fields in child input (PR #155707)

Peter Klausler via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 27 15:14:42 PDT 2025


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/155707

List-directed input statements maintain two flags ("eatComma_" and "hitSlash_") in their state that allow GetNextDataEdit() to detect and handle field separators (usually ',') and slashes in the input stream.  For list-directed input in a defined input subroutine, it's necessary to copy those flags from the parent list-directed input statement (if any) and update them afterwards.

Fixes https://github.com/llvm/llvm-project/issues/154791.

>From 70b8f4abec074ff491529a3955938e2d98dcd4fd Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Wed, 27 Aug 2025 15:08:57 -0700
Subject: [PATCH] [flang][runtime] Handle null list-directed fields in child
 input

List-directed input statements maintain two flags ("eatComma_" and
"hitSlash_") in their state that allow GetNextDataEdit() to detect
and handle field separators (usually ',') and slashes in the input
stream.  For list-directed input in a defined input subroutine, it's
necessary to copy those flags from the parent list-directed input
statement (if any) and update them afterwards.

Fixes https://github.com/llvm/llvm-project/issues/154791.
---
 flang-rt/include/flang-rt/runtime/io-stmt.h | 7 +++++++
 flang-rt/lib/runtime/io-stmt.cpp            | 9 +++++++++
 2 files changed, 16 insertions(+)

diff --git a/flang-rt/include/flang-rt/runtime/io-stmt.h b/flang-rt/include/flang-rt/runtime/io-stmt.h
index 3d1ca5091e923..4863031a0a9a5 100644
--- a/flang-rt/include/flang-rt/runtime/io-stmt.h
+++ b/flang-rt/include/flang-rt/runtime/io-stmt.h
@@ -441,6 +441,8 @@ class ListDirectedStatementState<Direction::Input>
   RT_API_ATTRS const NamelistGroup *namelistGroup() const {
     return namelistGroup_;
   }
+  RT_API_ATTRS bool eatComma() const { return eatComma_; }
+  RT_API_ATTRS bool hitSlash() const { return hitSlash_; }
   RT_API_ATTRS int EndIoStatement();
 
   // Skips value separators, handles repetition and null values.
@@ -464,6 +466,11 @@ class ListDirectedStatementState<Direction::Input>
     namelistGroup_ = namelistGroup;
   }
 
+  RT_API_ATTRS void UpdateState(bool eatComma, bool hitSlash) {
+    eatComma_ = eatComma;
+    hitSlash_ = hitSlash;
+  }
+
 protected:
   const NamelistGroup *namelistGroup_{nullptr};
 
diff --git a/flang-rt/lib/runtime/io-stmt.cpp b/flang-rt/lib/runtime/io-stmt.cpp
index 28149090eb169..4f2f2cd44c898 100644
--- a/flang-rt/lib/runtime/io-stmt.cpp
+++ b/flang-rt/lib/runtime/io-stmt.cpp
@@ -1098,6 +1098,7 @@ ChildListIoStatementState<DIR>::ChildListIoStatementState(
     if (auto *listInput{child.parent()
                 .get_if<ListDirectedStatementState<Direction::Input>>()}) {
       this->namelistGroup_ = listInput->namelistGroup();
+      this->UpdateState(listInput->eatComma(), listInput->hitSlash());
     }
   }
 #else
@@ -1121,6 +1122,14 @@ bool ChildListIoStatementState<DIR>::AdvanceRecord(int n) {
 
 template <Direction DIR> int ChildListIoStatementState<DIR>::EndIoStatement() {
   if constexpr (DIR == Direction::Input) {
+    if (auto *listInput{this->child()
+                .parent()
+                .template get_if<
+                    ListDirectedStatementState<Direction::Input>>()}) {
+      // These may have come from an outer list-directed input context and
+      // then updated here.
+      listInput->UpdateState(this->eatComma(), this->hitSlash());
+    }
     if (int status{ListDirectedStatementState<DIR>::EndIoStatement()};
         status != IostatOk) {
       return status;



More information about the llvm-commits mailing list