[llvm] [flang-rt] enable IsNamelistNameOrSlash lookahead for scalar namelist items (PR #211224)
Kareem Ergawy via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 22 03:11:04 PDT 2026
https://github.com/ergawy updated https://github.com/llvm/llvm-project/pull/211224
>From d00d24ba16992c4eee07ddad085428820592a51d Mon Sep 17 00:00:00 2001
From: ergawy <kareem.ergawy at gmail.com>
Date: Tue, 21 Jul 2026 07:41:56 -0700
Subject: [PATCH] [flang-rt] enable IsNamelistNameOrSlash lookahead for scalar
namelist items
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Problem
-------
An empty NAMELIST assignment on a scalar item — e.g. `l =` in
&nml l= i_count=7 r_value=2.72/
— aborted at runtime with
fatal Fortran runtime error: Bad character 'i' in LOGICAL input field
Every EditIntegerInput / EditRealInput / EditLogicalInput /
EditCharacterInput function starts its list-directed arm with
if (IsNamelistNameOrSlash(io)) return false; // no value
which peeks ahead (via SavedPosition, no stream consumption) for a
`<name>=` / `<name>%` / `<name>(` shape or one of the terminators
`/` `&` `$`, letting the reader bail cleanly for empty values and
short-array ends. That helper's first line is
if (!listInput || !listInput->namelistGroup()) return false;
InputNamelist however called ResetForNextNamelistItem with
`useDescriptor->rank() > 0 ? &group : nullptr`, so `namelistGroup_`
stayed null for scalars. The peek was silently disabled and the
value reader consumed the next namelist item's name as a bare token,
producing the abort above. Legacy nvfortran / gfortran accept empty
scalar assignments as "keep current value".
Solution
--------
Pass `&group` unconditionally to `ResetForNextNamelistItem`. Today
`IsNamelistNameOrSlash` uses `namelistGroup_` only as a boolean gate
(never as a lookup table), so widening it is a no-op for arrays and
enables the same empty-value / next-name detection for scalars.
NamelistTests.NanInputAmbiguity (which motivated the original pointer
form) still passes; three new tests cover the empty-scalar case, the
empty-array case, and an empty scalar surrounded by arrays.
---
flang-rt/include/flang-rt/runtime/namelist.h | 2 +-
flang-rt/lib/runtime/namelist.cpp | 12 +-
flang-rt/unittests/Runtime/Namelist.cpp | 125 +++++++++++++++++++
3 files changed, 136 insertions(+), 3 deletions(-)
diff --git a/flang-rt/include/flang-rt/runtime/namelist.h b/flang-rt/include/flang-rt/runtime/namelist.h
index 17d7bf310cc96..3feb8440f077b 100644
--- a/flang-rt/include/flang-rt/runtime/namelist.h
+++ b/flang-rt/include/flang-rt/runtime/namelist.h
@@ -18,10 +18,10 @@
namespace Fortran::runtime {
class Descriptor;
-class IoStatementState;
} // namespace Fortran::runtime
namespace Fortran::runtime::io {
+class IoStatementState;
// A NAMELIST group is a named ordered collection of distinct variable names.
// It is packaged by lowering into an instance of this class.
diff --git a/flang-rt/lib/runtime/namelist.cpp b/flang-rt/lib/runtime/namelist.cpp
index c1745595b88f4..d644a9fb1f049 100644
--- a/flang-rt/lib/runtime/namelist.cpp
+++ b/flang-rt/lib/runtime/namelist.cpp
@@ -612,8 +612,16 @@ bool IODEF(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
return false;
}
} else {
- listInput->ResetForNextNamelistItem(
- useDescriptor->rank() > 0 ? &group : nullptr);
+ // Pass &group unconditionally (not just for arrays) so the
+ // IsNamelistNameOrSlash look-ahead in Edit{Integer,Real,Logical,
+ // Character}Input fires for scalar items too. Each of those
+ // per-type value readers starts its list-directed arm with
+ //
+ // if (IsNamelistNameOrSlash(io)) return false; // no value
+ //
+ // With &group set, that empty-value probe works for scalars as well as
+ // sequences.
+ listInput->ResetForNextNamelistItem(&group);
if (!descr::DescriptorIO<Direction::Input>(io, *useDescriptor) &&
handler.InError()) {
return false;
diff --git a/flang-rt/unittests/Runtime/Namelist.cpp b/flang-rt/unittests/Runtime/Namelist.cpp
index aaa3c1d354098..12f53e9e29db7 100644
--- a/flang-rt/unittests/Runtime/Namelist.cpp
+++ b/flang-rt/unittests/Runtime/Namelist.cpp
@@ -334,6 +334,131 @@ TEST(NamelistTests, RealValueForInt) {
EXPECT_EQ(got, expect);
}
+TEST(NamelistTests, EmptyValueForScalar) {
+ // logical :: l = .false. ; integer :: k = 42
+ // &nml l= k=7/
+ // The empty assignment `l=` should leave l at its .false. default and
+ // parsing should continue with k=7. Regression test for a bug where
+ // scalar namelist items disabled the IsNamelistNameOrSlash guard in
+ // Edit*Input, so l's parse consumed the `k` on the next token and
+ // signalled "Bad character 'k' in LOGICAL input field".
+ OwningPtr<Descriptor> lDesc{
+ MakeArray<TypeCategory::Logical, sizeof(std::uint8_t)>(
+ std::vector<int>{}, std::vector<std::uint8_t>{false})};
+ OwningPtr<Descriptor> kDesc{
+ MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
+ std::vector<int>{}, std::vector<int>{42})};
+ const NamelistGroup::Item items[]{{"l", *lDesc}, {"k", *kDesc}};
+ const NamelistGroup group{"nml", 2, items};
+ static char t1[]{"&nml l= k=7/"};
+ StaticDescriptor<1, true> statDesc;
+ Descriptor &internalDesc{statDesc.descriptor()};
+ internalDesc.Establish(TypeCode{CFI_type_char},
+ /*elementBytes=*/std::strlen(t1), t1, 0, nullptr, CFI_attribute_pointer);
+ auto inCookie{IONAME(BeginInternalArrayListInput)(
+ internalDesc, nullptr, 0, __FILE__, __LINE__)};
+ ASSERT_TRUE(IONAME(InputNamelist)(inCookie, group));
+ ASSERT_EQ(IONAME(EndIoStatement)(inCookie), IostatOk)
+ << "namelist empty scalar assignment";
+ char out[24];
+ internalDesc.Establish(TypeCode{CFI_type_char}, /*elementBytes=*/sizeof out,
+ out, 0, nullptr, CFI_attribute_pointer);
+ auto outCookie{IONAME(BeginInternalArrayListOutput)(
+ internalDesc, nullptr, 0, __FILE__, __LINE__)};
+ ASSERT_TRUE(IONAME(OutputNamelist)(outCookie, group));
+ ASSERT_EQ(IONAME(EndIoStatement)(outCookie), IostatOk) << "namelist output";
+ std::string got{out, sizeof out};
+ static const std::string expect{" &NML L= F,K= 7/ "};
+ EXPECT_EQ(got, expect);
+}
+
+TEST(NamelistTests, EmptyValueForArray) {
+ // integer :: k=1 ; integer :: arr(3)=[10,20,30] ; integer :: m=2
+ // &nml k=100 arr= m=200/
+ // The empty assignment `arr=` should leave the array at its default and
+ // parsing should continue with m=200. This case worked before the
+ // `rank() > 0 ? &group : nullptr` filter was widened, but is guarded
+ // here to make sure widening the pointer for scalars didn't regress the
+ // short-array end-of-values detection.
+ OwningPtr<Descriptor> kDesc{
+ MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
+ std::vector<int>{}, std::vector<int>{1})};
+ OwningPtr<Descriptor> arrDesc{
+ MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
+ std::vector<int>{3}, std::vector<int>{10, 20, 30})};
+ OwningPtr<Descriptor> mDesc{
+ MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
+ std::vector<int>{}, std::vector<int>{2})};
+ const NamelistGroup::Item items[]{
+ {"k", *kDesc}, {"arr", *arrDesc}, {"m", *mDesc}};
+ const NamelistGroup group{"nml", 3, items};
+ static char t1[]{"&nml k=100 arr= m=200/"};
+ StaticDescriptor<1, true> statDesc;
+ Descriptor &internalDesc{statDesc.descriptor()};
+ internalDesc.Establish(TypeCode{CFI_type_char},
+ /*elementBytes=*/std::strlen(t1), t1, 0, nullptr, CFI_attribute_pointer);
+ auto inCookie{IONAME(BeginInternalArrayListInput)(
+ internalDesc, nullptr, 0, __FILE__, __LINE__)};
+ ASSERT_TRUE(IONAME(InputNamelist)(inCookie, group));
+ ASSERT_EQ(IONAME(EndIoStatement)(inCookie), IostatOk)
+ << "namelist empty array assignment";
+ char out[48];
+ internalDesc.Establish(TypeCode{CFI_type_char}, /*elementBytes=*/sizeof out,
+ out, 0, nullptr, CFI_attribute_pointer);
+ auto outCookie{IONAME(BeginInternalArrayListOutput)(
+ internalDesc, nullptr, 0, __FILE__, __LINE__)};
+ ASSERT_TRUE(IONAME(OutputNamelist)(outCookie, group));
+ ASSERT_EQ(IONAME(EndIoStatement)(outCookie), IostatOk) << "namelist output";
+ std::string got{out, sizeof out};
+ static const std::string expect{
+ " &NML K= 100,ARR= 10 20 30,M= 200/ "};
+ EXPECT_EQ(got, expect);
+}
+
+TEST(NamelistTests, EmptyScalarBetweenArrays) {
+ // integer :: arr1(3)=[10,20,30] ; logical :: l=.false. ;
+ // integer :: arr2(3)=[40,50,60]
+ // &nml arr1=100 200 300 l= arr2=400 500 600/
+ // The empty assignment `l=` sits between two full array assignments.
+ // arr1 must be fully read (three values), then l retains its default
+ // (empty scalar), then arr2 must be fully read. Exercises the
+ // interaction between the widened scalar guard and the array
+ // short-value / end-of-values detection.
+ OwningPtr<Descriptor> arr1Desc{
+ MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
+ std::vector<int>{3}, std::vector<int>{10, 20, 30})};
+ OwningPtr<Descriptor> lDesc{
+ MakeArray<TypeCategory::Logical, sizeof(std::uint8_t)>(
+ std::vector<int>{}, std::vector<std::uint8_t>{false})};
+ OwningPtr<Descriptor> arr2Desc{
+ MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
+ std::vector<int>{3}, std::vector<int>{40, 50, 60})};
+ const NamelistGroup::Item items[]{
+ {"arr1", *arr1Desc}, {"l", *lDesc}, {"arr2", *arr2Desc}};
+ const NamelistGroup group{"nml", 3, items};
+ static char t1[]{"&nml arr1=100 200 300 l= arr2=400 500 600/"};
+ StaticDescriptor<1, true> statDesc;
+ Descriptor &internalDesc{statDesc.descriptor()};
+ internalDesc.Establish(TypeCode{CFI_type_char},
+ /*elementBytes=*/std::strlen(t1), t1, 0, nullptr, CFI_attribute_pointer);
+ auto inCookie{IONAME(BeginInternalArrayListInput)(
+ internalDesc, nullptr, 0, __FILE__, __LINE__)};
+ ASSERT_TRUE(IONAME(InputNamelist)(inCookie, group));
+ ASSERT_EQ(IONAME(EndIoStatement)(inCookie), IostatOk)
+ << "namelist empty scalar between arrays";
+ char out[64];
+ internalDesc.Establish(TypeCode{CFI_type_char}, /*elementBytes=*/sizeof out,
+ out, 0, nullptr, CFI_attribute_pointer);
+ auto outCookie{IONAME(BeginInternalArrayListOutput)(
+ internalDesc, nullptr, 0, __FILE__, __LINE__)};
+ ASSERT_TRUE(IONAME(OutputNamelist)(outCookie, group));
+ ASSERT_EQ(IONAME(EndIoStatement)(outCookie), IostatOk) << "namelist output";
+ std::string got{out, sizeof out};
+ static const std::string expect{
+ " &NML ARR1= 100 200 300,L= F,ARR2= 400 500 600/ "};
+ EXPECT_EQ(got, expect);
+}
+
TEST(NamelistTests, NanInputAmbiguity) {
OwningPtr<Descriptor> xDesc{// real :: x(5) = 0.
MakeArray<TypeCategory::Real, static_cast<int>(sizeof(float))>(
More information about the llvm-commits
mailing list