[flang-commits] [flang] [flang][runtime] Handle incomplete NAMELIST input derived type compon… (PR #66831)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Oct 16 12:46:55 PDT 2023


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/66831

>From c0c34d26d48835b6ae091180e1ad7df787e228cb Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 19 Sep 2023 14:59:32 -0700
Subject: [PATCH] [flang][runtime] Handle incomplete NAMELIST input derived
 type component list

When a derived type value appears in NAMELIST input, its components'
values appear in sequence.  This sequence can be truncated by
a NAME= that begins the next NAMELIST input item, or by the terminal
'/' that ends the NAMELIST group.  Extend the mechanism already in
place for truncated array item lists in NAMELIST input so that it
also applies to derived type component sequences, and rename
things appropriately.
---
 flang/runtime/descriptor-io.h |  6 +++++-
 flang/runtime/io-stmt.h       |  9 ++++-----
 flang/runtime/namelist.cpp    | 11 +++++++----
 3 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/flang/runtime/descriptor-io.h b/flang/runtime/descriptor-io.h
index 840d73b8e857cfe..e20f31e9b443134 100644
--- a/flang/runtime/descriptor-io.h
+++ b/flang/runtime/descriptor-io.h
@@ -288,7 +288,11 @@ static bool DefaultComponentwiseIO(IoStatementState &io,
           *compArray.Element<typeInfo::Component>(at)};
       if (!DefaultComponentIO<DIR>(
               io, component, descriptor, subscripts, handler, table)) {
-        return false;
+        // Truncated nonempty namelist input sequence?
+        auto *listInput{
+            io.get_if<ListDirectedStatementState<Direction::Input>>()};
+        return DIR == Direction::Input && (j > 0 || k > 0) && listInput &&
+            listInput->inNamelistSequence();
       }
     }
   }
diff --git a/flang/runtime/io-stmt.h b/flang/runtime/io-stmt.h
index fa432d07a680deb..d4ceb83265246bd 100644
--- a/flang/runtime/io-stmt.h
+++ b/flang/runtime/io-stmt.h
@@ -295,8 +295,7 @@ template <>
 class ListDirectedStatementState<Direction::Input>
     : public FormattedIoStatementState<Direction::Input> {
 public:
-  bool inNamelistArray() const { return inNamelistArray_; }
-  void set_inNamelistArray(bool yes = true) { inNamelistArray_ = yes; }
+  bool inNamelistSequence() const { return inNamelistSequence_; }
 
   // Skips value separators, handles repetition and null values.
   // Vacant when '/' appears; present with descriptor == ListDirectedNullValue
@@ -308,11 +307,11 @@ class ListDirectedStatementState<Direction::Input>
   // input statement.  This member function resets some state so that
   // repetition and null values work correctly for each successive
   // NAMELIST input item.
-  void ResetForNextNamelistItem(bool inNamelistArray) {
+  void ResetForNextNamelistItem(bool inNamelistSequence) {
     remaining_ = 0;
     eatComma_ = false;
     realPart_ = imaginaryPart_ = false;
-    inNamelistArray_ = inNamelistArray;
+    inNamelistSequence_ = inNamelistSequence;
   }
 
 private:
@@ -322,7 +321,7 @@ class ListDirectedStatementState<Direction::Input>
   bool hitSlash_{false}; // once '/' is seen, nullify further items
   bool realPart_{false};
   bool imaginaryPart_{false};
-  bool inNamelistArray_{false};
+  bool inNamelistSequence_{false};
 };
 
 template <Direction DIR>
diff --git a/flang/runtime/namelist.cpp b/flang/runtime/namelist.cpp
index 1b3207ef2f93212..61815a7cc8a4032 100644
--- a/flang/runtime/namelist.cpp
+++ b/flang/runtime/namelist.cpp
@@ -522,15 +522,18 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
     }
     io.HandleRelativePosition(byteCount);
     // Read the values into the descriptor.  An array can be short.
-    listInput->ResetForNextNamelistItem(useDescriptor->rank() > 0);
     if (const auto *addendum{useDescriptor->Addendum()};
         addendum && addendum->derivedType()) {
       const NonTbpDefinedIoTable *table{group.nonTbpDefinedIo};
+      listInput->ResetForNextNamelistItem(/*inNamelistSequence=*/true);
       if (!IONAME(InputDerivedType)(cookie, *useDescriptor, table)) {
         return false;
       }
-    } else if (!descr::DescriptorIO<Direction::Input>(io, *useDescriptor)) {
-      return false;
+    } else {
+      listInput->ResetForNextNamelistItem(useDescriptor->rank() > 0);
+      if (!descr::DescriptorIO<Direction::Input>(io, *useDescriptor)) {
+        return false;
+      }
     }
     next = io.GetNextNonBlank(byteCount);
     if (next && *next == comma) {
@@ -549,7 +552,7 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
 bool IsNamelistNameOrSlash(IoStatementState &io) {
   if (auto *listInput{
           io.get_if<ListDirectedStatementState<Direction::Input>>()}) {
-    if (listInput->inNamelistArray()) {
+    if (listInput->inNamelistSequence()) {
       SavedPosition savedPosition{io};
       std::size_t byteCount{0};
       if (auto ch{io.GetNextNonBlank(byteCount)}) {



More information about the flang-commits mailing list