Skip to content

[flang][runtime] Allow already-documented missing 'w' on edit descriptor #72901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 37 additions & 36 deletions flang/runtime/format-implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,16 @@ std::optional<DataEdit> FormatControl<CONTEXT>::GetNextDataEdit(
int repeat{CueUpNextDataEdit(context)};
auto start{offset_};
DataEdit edit;
edit.modes = context.mutableModes();
// Handle repeated nonparenthesized edit descriptors
edit.repeat = std::min(repeat, maxRepeat); // 0 if maxRepeat==0
if (repeat > maxRepeat) {
stack_[height_].start = start; // after repeat count
stack_[height_].remaining = repeat - edit.repeat;
++height_;
}
edit.descriptor = static_cast<char>(Capitalize(GetNextChar(context)));
if (edit.descriptor == 'E') {
if (auto next{static_cast<char>(Capitalize(PeekNext()))};
next == 'N' || next == 'S' || next == 'X') {
edit.variation = next;
++offset_;
}
} else if (edit.descriptor == 'D' && Capitalize(PeekNext()) == 'T') {
if (edit.descriptor == 'D' && Capitalize(PeekNext()) == 'T') {
// DT['iotype'][(v_list)] defined I/O
edit.descriptor = DataEdit::DefinedDerivedType;
++offset_;
Expand Down Expand Up @@ -479,38 +481,37 @@ std::optional<DataEdit> FormatControl<CONTEXT>::GetNextDataEdit(
return std::nullopt;
}
}
}
if (edit.descriptor == 'A' || edit.descriptor == 'L') {
// width is optional for A[w] or L[w]
auto ch{PeekNext()};
if (ch >= '0' && ch <= '9') {
edit.width = GetIntField(context);
}
} else if (edit.descriptor != DataEdit::DefinedDerivedType) {
edit.width = GetIntField(context);
}
if constexpr (std::is_base_of_v<InputStatementState, CONTEXT>) {
if (edit.width.value_or(-1) == 0) {
ReportBadFormat(context, "Input field width is zero", start);
} else { // not DT'iotype'
if (edit.descriptor == 'E') {
if (auto next{static_cast<char>(Capitalize(PeekNext()))};
next == 'N' || next == 'S' || next == 'X') {
edit.variation = next;
++offset_;
}
}
}
if (edit.descriptor != DataEdit::DefinedDerivedType && PeekNext() == '.') {
++offset_;
edit.digits = GetIntField(context);
CharType ch{PeekNext()};
if (ch == 'e' || ch == 'E' || ch == 'd' || ch == 'D') {
++offset_;
edit.expoDigits = GetIntField(context);
// Width is optional for A[w] in the standard and optional
// for Lw in most compilers.
// Intel & (presumably, from bug report) Fujitsu allow
// a missing 'w' & 'd'/'m' for other edit descriptors -- but not
// 'd'/'m' with a missing 'w' -- and so interpret "(E)" as "(E0)".
if (CharType ch{PeekNext()}; (ch >= '0' && ch <= '9') || ch == '.') {
edit.width = GetIntField(context);
if constexpr (std::is_base_of_v<InputStatementState, CONTEXT>) {
if (edit.width.value_or(-1) == 0) {
ReportBadFormat(context, "Input field width is zero", start);
}
}
if (PeekNext() == '.') {
++offset_;
edit.digits = GetIntField(context);
if (CharType ch{PeekNext()};
ch == 'e' || ch == 'E' || ch == 'd' || ch == 'D') {
++offset_;
edit.expoDigits = GetIntField(context);
}
}
}
}
edit.modes = context.mutableModes();
// Handle repeated nonparenthesized edit descriptors
edit.repeat = std::min(repeat, maxRepeat); // 0 if maxRepeat==0
if (repeat > maxRepeat) {
stack_[height_].start = start; // after repeat count
stack_[height_].remaining = repeat - edit.repeat;
++height_;
}
return edit;
}

Expand Down
3 changes: 2 additions & 1 deletion flang/unittests/Runtime/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ TEST(FormatTests, FormatStringTraversal) {
ResultsTy{"I4", "E10.1", "E10.1", "/", "I4", "E10.1", "E10.1", "/",
"I4", "E10.1", "E10.1"},
1},
{1, "(F)", ResultsTy{"F"}, 1}, // missing 'w'
};

for (const auto &[n, format, expect, repeat] : params) {
Expand Down Expand Up @@ -170,7 +171,7 @@ TEST(InvalidFormatFailure, MissingPrecision) {
R"(Invalid FORMAT: integer expected at '\)')");
}

TEST(InvalidFormatFailure, MissingFormatWidth) {
TEST(InvalidFormatFailure, MissingFormatWidthWithDigits) {
static constexpr const char *format{"(F.9)"};
static constexpr int repeat{1};

Expand Down