diff --git a/flang/runtime/format-implementation.h b/flang/runtime/format-implementation.h index 0daacc6bcccbb..57c176ea8d77c 100644 --- a/flang/runtime/format-implementation.h +++ b/flang/runtime/format-implementation.h @@ -284,8 +284,8 @@ int FormatControl::CueUpNextDataEdit(Context &context, bool stop) { } else { stack_[height_].remaining = 0; } - if (height_ == 1) { - // Subtle point (F'2018 13.4 para 9): tha last parenthesized group + if (height_ == 1 && !hitEnd_) { + // Subtle point (F'2018 13.4 para 9): the last parenthesized group // at height 1 becomes the restart point after control reaches the // end of the format, including its repeat count. stack_[0].start = maybeReversionPoint; @@ -300,6 +300,7 @@ int FormatControl::CueUpNextDataEdit(Context &context, bool stop) { return 0; // end of FORMAT and no data items remain } context.AdvanceRecord(); // implied / before rightmost ) + hitEnd_ = true; } auto restart{stack_[height_ - 1].start}; if (format_[restart] == '(') { diff --git a/flang/runtime/format.h b/flang/runtime/format.h index b9f8f73a48dec..989006ecd85ca 100644 --- a/flang/runtime/format.h +++ b/flang/runtime/format.h @@ -184,6 +184,7 @@ template class FormatControl { const std::uint8_t maxHeight_{maxMaxHeight}; std::uint8_t height_{0}; bool freeFormat_{false}; + bool hitEnd_{false}; const CharType *format_{nullptr}; int formatLength_{0}; // in units of characters int offset_{0}; // next item is at format_[offset_] diff --git a/flang/unittests/Runtime/Format.cpp b/flang/unittests/Runtime/Format.cpp index 1f55b39d59053..e9004b7798f36 100644 --- a/flang/unittests/Runtime/Format.cpp +++ b/flang/unittests/Runtime/Format.cpp @@ -107,6 +107,10 @@ TEST(FormatTests, FormatStringTraversal) { ResultsTy{"'PI='", "F9.7", "'PI='", "F9.7"}, 1}, {2, "(*('PI=',F9.7,:))", ResultsTy{"'PI='", "F9.7", "'PI='", "F9.7"}, 1}, {1, "(3F9.7)", ResultsTy{"2*F9.7"}, 2}, + {9, "((I4,2(E10.1)))", + ResultsTy{"I4", "E10.1", "E10.1", "/", "I4", "E10.1", "E10.1", "/", + "I4", "E10.1", "E10.1"}, + 1}, }; for (const auto &[n, format, expect, repeat] : params) {