[llvm] [flang][runtime] Allow child NAMELIST input to advance records (PR #153963)

Peter Klausler via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 16 09:27:28 PDT 2025


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

NAMELIST input in child I/O is rare, and it's not clear in the standard whether it should be allowed to advance to later records in the parent unit.  But GNU Fortran supports it, and there's no good reason not to do so since a NAMELIST input group that isn't terminated on the same line is otherwise going to be a fatal error.

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

>From 76946cda78e6a2bef3e0b93209cf5e18b9b5dff5 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Sat, 16 Aug 2025 09:23:10 -0700
Subject: [PATCH] [flang][runtime] Allow child NAMELIST input to advance
 records

NAMELIST input in child I/O is rare, and it's not clear in the
standard whether it should be allowed to advance to later records
in the parent unit.  But GNU Fortran supports it, and there's no
good reason not to do so since a NAMELIST input group that isn't
terminated on the same line is otherwise going to be a fatal error.

Fixes https://github.com/llvm/llvm-project/issues/153416.
---
 flang-rt/include/flang-rt/runtime/io-stmt.h |  1 +
 flang-rt/lib/runtime/io-stmt.cpp            | 20 +++++++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/flang-rt/include/flang-rt/runtime/io-stmt.h b/flang-rt/include/flang-rt/runtime/io-stmt.h
index 9f71d515cb615..9d50f7c10c4f6 100644
--- a/flang-rt/include/flang-rt/runtime/io-stmt.h
+++ b/flang-rt/include/flang-rt/runtime/io-stmt.h
@@ -696,6 +696,7 @@ class ChildListIoStatementState : public ChildIoStatementState<DIR>,
   RT_API_ATTRS ChildListIoStatementState(
       ChildIo &, const char *sourceFile = nullptr, int sourceLine = 0);
   using ListDirectedStatementState<DIR>::GetNextDataEdit;
+  RT_API_ATTRS bool AdvanceRecord(int = 1);
   RT_API_ATTRS int EndIoStatement();
 };
 
diff --git a/flang-rt/lib/runtime/io-stmt.cpp b/flang-rt/lib/runtime/io-stmt.cpp
index e08088fab4311..9b1e696b3f12f 100644
--- a/flang-rt/lib/runtime/io-stmt.cpp
+++ b/flang-rt/lib/runtime/io-stmt.cpp
@@ -1095,10 +1095,14 @@ ChildListIoStatementState<DIR>::ChildListIoStatementState(
 }
 
 template <Direction DIR>
-bool ChildUnformattedIoStatementState<DIR>::Receive(
-    char *data, std::size_t bytes, std::size_t elementBytes) {
+bool ChildListIoStatementState<DIR>::AdvanceRecord(int n) {
 #if !defined(RT_DEVICE_AVOID_RECURSION)
-  return this->child().parent().Receive(data, bytes, elementBytes);
+  // Allow child NAMELIST input to advance
+  if (DIR == Direction::Input && this->mutableModes().inNamelist) {
+    return this->child().parent().AdvanceRecord(n);
+  } else {
+    return false;
+  }
 #else
   this->ReportUnsupportedChildIo();
 #endif
@@ -1114,6 +1118,16 @@ template <Direction DIR> int ChildListIoStatementState<DIR>::EndIoStatement() {
   return ChildIoStatementState<DIR>::EndIoStatement();
 }
 
+template <Direction DIR>
+bool ChildUnformattedIoStatementState<DIR>::Receive(
+    char *data, std::size_t bytes, std::size_t elementBytes) {
+#if !defined(RT_DEVICE_AVOID_RECURSION)
+  return this->child().parent().Receive(data, bytes, elementBytes);
+#else
+  this->ReportUnsupportedChildIo();
+#endif
+}
+
 template class InternalIoStatementState<Direction::Output>;
 template class InternalIoStatementState<Direction::Input>;
 template class InternalFormattedIoStatementState<Direction::Output>;



More information about the llvm-commits mailing list