Skip to content

Commit 933882f

Browse files
authored
[flang][runtime] Fix trailing blanks for Gw.dEe output editing (#75263)
When generalized numeric output editing of real data maps to Fw.d output editing, either two or four trailing blanks are emitted depending on the presence and value of 'e'. The code that detects field width overflow didn't take these trailing blanks into account, and sometimes the field width adjustment was producing an F0.d output edit descriptor (no fixed field width). Fix by retaining the original field width, but requiring it to also accommodate the trailing blanks. Fixes llvm-test-suite/Fortran/gfortran/regression/fmt_g.f.
1 parent 475d18f commit 933882f

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

flang/runtime/edit-output.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,12 @@ bool RealOutputEditing<KIND>::EditEorDOutput(const DataEdit &edit) {
341341
ConvertToDecimal(significantDigits, edit.modes.round, flags)};
342342
if (IsInfOrNaN(converted.str, static_cast<int>(converted.length))) {
343343
return editWidth > 0 &&
344-
converted.length > static_cast<std::size_t>(editWidth)
344+
converted.length + trailingBlanks_ >
345+
static_cast<std::size_t>(editWidth)
345346
? EmitRepeated(io_, '*', editWidth)
346347
: EmitPrefix(edit, converted.length, editWidth) &&
347348
EmitAscii(io_, converted.str, converted.length) &&
348-
EmitSuffix(edit);
349+
EmitRepeated(io_, ' ', trailingBlanks_) && EmitSuffix(edit);
349350
}
350351
if (!IsZero()) {
351352
converted.decimalExponent -= scale;
@@ -522,8 +523,9 @@ bool RealOutputEditing<KIND>::EditFOutput(const DataEdit &edit) {
522523
zeroesBeforePoint = 1; // "." -> "0."
523524
}
524525
int totalLength{signLength + digitsBeforePoint + zeroesBeforePoint +
525-
1 /*'.'*/ + zeroesAfterPoint + digitsAfterPoint + trailingZeroes};
526-
int width{editWidth > 0 ? editWidth : totalLength};
526+
1 /*'.'*/ + zeroesAfterPoint + digitsAfterPoint + trailingZeroes +
527+
trailingBlanks_ /* G editing converted to F */};
528+
int width{editWidth > 0 || trailingBlanks_ ? editWidth : totalLength};
527529
if (totalLength > width) {
528530
return EmitRepeated(io_, '*', width);
529531
}
@@ -574,8 +576,11 @@ DataEdit RealOutputEditing<KIND>::EditForGOutput(DataEdit edit) {
574576
trailingBlanks_ = 0;
575577
if (editWidth > 0) {
576578
int expoDigits{edit.expoDigits.value_or(0)};
579+
// F'2023 13.7.5.2.3 p5: "If 0 <= s <= d, the scale factor has no effect
580+
// and F(w − n).(d − s),n(’b’) editing is used where b is a blank and
581+
// n is 4 for Gw.d editing, e + 2 for Gw.dEe editing if e > 0, and
582+
// 4 for Gw.dE0 editing."
577583
trailingBlanks_ = expoDigits > 0 ? expoDigits + 2 : 4; // 'n'
578-
*edit.width = std::max(0, editWidth - trailingBlanks_);
579584
}
580585
if (edit.digits.has_value()) {
581586
*edit.digits = std::max(0, *edit.digits - expo);

0 commit comments

Comments
 (0)