Hi Jerry!
Am 24.11.26 um 03:58 schrieb Jerry D:
On 11/23/26 12:55 PM, Harald Anlauf wrote:
Hi Jerry!
Am 23.11.26 um 19:59 schrieb Jerry DeLisle:
After some offlist discussion with Steve Kargl, the attached is a
version 2. The warning for an E, F or G edit descriptor width
too small is now controlled by a new option, -Wformat-width, which
-Wall enables. The option is documented in invoke.texi. Without -Wall
there is no warning, so fmt_f0_1.f90 and fmt_g.f no longer need
changes, and pr45129.f90 now uses -Wall. The checks themselves are
unchanged from version 1.
Regression tested on x86_64.
OK for mainline?
Well, there is room for improvement:
program foo
real :: x = 1
write(*,'(f2.2)') x
write(*,'(f1.2)') x
write(*,'(e4.1e0)') x
write(*,'(es4.1e0)') x
end
This gets warnings on the f2.2 and f1.2 lines, is it should, but it
misses the formats with e and es. All of them will print asterisks.
Can you have a look?
The attached v3 of the patch adds in some more.
As far as being more helpful. I would like to do that as a followup to
this patch.
Let me know if this is OK to commit?
This is OK, provided you initialize the string s in the testcase
suitably, like
character(20) :: s = "1.0"
Otherwise the program would crash here if one tries to execute it.
Thanks,
Harald
Jerry
I also wonder if we can be a little more helpful to the user.
Intel's ifx emits:
fmt.f90(5): remark #8293: Recommended relationship between field width
'W', the number of fractional digits 'D' and the number of exponential
digits 'E' in this edit descriptor is 'W>=D+E+5'.
write(*,'(e4.1e0)') x
-------------^
fmt.f90(6): remark #8293: Recommended relationship between field width
'W', the number of fractional digits 'D' and the number of exponential
digits 'E' in this edit descriptor is 'W>=D+E+5'.
write(*,'(es4.1e0)') x
--------------^
NAG:
NAG Fortran Compiler Release 7.2(Shin-Urayasu) Build 7203
Warning: fmt.f90, line 5: Field width too small for output of finite
numbers - the E4.1E0 edit descriptor will produce all asterisks
Warning: fmt.f90, line 6: Field width too small for output of finite
numbers - the ES4.1E0 edit descriptor will produce all asterisks
[NAG Fortran Compiler normal termination, 2 warnings]
One can argue about the precise recommendations, as if the user knows
that x is positive, there is no need to reserve space for the sign.
Up to you.
Thanks,
Harald
Regards,
Jerry
---
fortran: [PR45129] Warn on too-small E, F and G field widths
An E, F or G edit descriptor on output whose width is too small for
its digits always produces asterisks, but gfortran gave no warning at
compile time. check_format now warns about it under the new option
-Wformat-width, which -Wall enables.
A labeled FORMAT is checked when it is matched, before a READ or
WRITE refers to it, so its I/O direction is not yet known.
check_format takes a format_check_kind: that first pass skips the
width warnings, and gfc_resolve_dt reruns the check for the width
warnings alone the first time the FORMAT is used for output, with the
other diagnostics suppressed.
Assisted-by: Claude Opus 5.5
PR fortran/45129
gcc/fortran/ChangeLog:
* gfortran.h (gfc_st_label): Add output_width_checked.
* invoke.texi (-Wformat-width): Document.
(-Wall): List -Wformat-width.
* io.cc (checking_width_only): New.
(next_char_not_space): Skip the tab warning when it is set.
(f_min_width): New.
(format_check_kind): New enum.
(check_format): Add a format_check_kind argument. Skip the width
warnings for FMT_CHECK_DECL and the other warnings for
FMT_CHECK_WIDTH. Warn with -Wformat-width when an E, F or G field
width on output is too small for its digits.
(check_format_string): Add a format_check_kind argument. For
FMT_CHECK_WIDTH, suppress errors and skip the check for trailing
characters.
(gfc_match_format): Pass FMT_CHECK_DECL.
(gfc_resolve_dt): With -Wformat-width, recheck a labeled FORMAT
with FMT_CHECK_WIDTH the first time it is used for output.
* lang.opt (Wformat-width): New.
gcc/testsuite/ChangeLog:
* gfortran.dg/pr45129.f90: New test.