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?

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.

From 0d1b702aeebfc073f7b607531fa5fa8f703ae596 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Mon, 23 Nov 2026 10:26:43 -0800
Subject: [PATCH] fortran: [PR45129] Warn on too-small real output field widths

A D, E, EN, ES, 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.
	(format_asterisk): Initialize output_width_checked.
	(f_min_width, check_real_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 a D, E, EN, ES, 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.
---
 gcc/fortran/gfortran.h                |   3 +
 gcc/fortran/invoke.texi               |  12 +-
 gcc/fortran/io.cc                     | 152 ++++++++++++++++++++++----
 gcc/fortran/lang.opt                  |   4 +
 gcc/testsuite/gfortran.dg/pr45129.f90 |  45 ++++++++
 5 files changed, 194 insertions(+), 22 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr45129.f90

diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index 49ba791c378..49f04b46e85 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -1971,6 +1971,9 @@ typedef struct gfc_st_label
 
   gfc_namespace *ns;
   int omp_region;
+
+  /* Set by gfc_resolve_dt once a FORMAT's output width warnings are done.  */
+  bool output_width_checked;
 }
 gfc_st_label;
 
diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 69c4fa69cc6..8d8762b828d 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -150,7 +150,8 @@ by type.  Explanations are in the following sections.
 and warnings}.
 @gccoptlist{-Waliasing -Wall -Wampersand -Warray-bounds
 -Wc-binding-type -Wcharacter-truncation -Wconversion
--Wno-deprecated-openmp -Wdo-subscript -Wfunction-elimination
+-Wno-deprecated-openmp -Wdo-subscript -Wformat-width
+-Wfunction-elimination
 -Wimplicit-interface -Wimplicit-procedure -Wintrinsic-shadow
 -Wuse-without-only -Wintrinsics-std -Wline-truncation -Wno-align-commons
 -Wno-overwrite-recursive -Wno-tabs -Wreal-q-constant -Wsurprising
@@ -995,6 +996,7 @@ Enables commonly used warning options pertaining to usage that
 we recommend avoiding and that we believe are easy to avoid.
 This currently includes @option{-Waliasing}, @option{-Wampersand},
 @option{-Wconversion}, @option{-Wsurprising}, @option{-Wc-binding-type},
+@option{-Wformat-width},
 @option{-Wintrinsics-std}, @option{-Wtabs}, @option{-Wintrinsic-shadow},
 @option{-Wline-truncation}, @option{-Wtarget-lifetime},
 @option{-Winteger-division}, @option{-Wreal-q-constant}, @option{-Wunused},
@@ -1100,6 +1102,14 @@ may be problematic. This currently includes @option{-Wcompare-reals},
 @option{-Wunused-parameter}, @option{-Wdo-subscript},
 @option{-Wunused-intent-out} and @option{-Wunused-read}.
 
+@opindex Wformat-width
+@cindex warnings, format width
+@item -Wformat-width
+Warn when the field width of a @code{D}, @code{E}, @code{EN},
+@code{ES}, @code{F} or @code{G} edit descriptor used for output is too
+small for its digits, so that every value is written as asterisks.  This option is implied by
+@option{-Wall}.
+
 @opindex Wfrontend-loop-interchange
 @cindex warnings, loop interchange
 @cindex loop interchange, warning
diff --git a/gcc/fortran/io.cc b/gcc/fortran/io.cc
index ee73d7c118f..545867ad068 100644
--- a/gcc/fortran/io.cc
+++ b/gcc/fortran/io.cc
@@ -29,7 +29,7 @@ along with GCC; see the file COPYING3.  If not see
 
 gfc_st_label
 format_asterisk = {0, NULL, NULL, -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL,
-		   0, {NULL, {NULL}}, NULL, 0};
+		   0, {NULL, {NULL}}, NULL, 0, false};
 
 typedef struct
 {
@@ -140,6 +140,9 @@ static enum
 { MODE_STRING, MODE_FORMAT, MODE_COPY }
 mode;
 
+/* Set by check_format_string during an FMT_CHECK_WIDTH pass.  */
+static bool checking_width_only = false;
+
 
 /* Return the next character in the format string.  */
 
@@ -206,7 +209,7 @@ next_char_not_space ()
   do
     {
       error_element = c = next_char (NONSTRING);
-      if (c == '\t')
+      if (c == '\t' && !checking_width_only)
 	gfc_warning (OPT_Wtabs, "Nonconforming tab character in format at %C");
     }
   while (gfc_is_whitespace (c));
@@ -617,13 +620,68 @@ token_to_string (format_token t)
     }
 }
 
+/* Minimum F-style field width for D fractional digits.  */
+
+static int
+f_min_width (int d)
+{
+  return d == 0 ? 2 : d + 1;
+}
+
+/* Warn at WHERE if the field width W of the D, E, EN, ES or G edit
+   descriptor T with D digits and exponent width E is too small for every
+   output value.  E is 2 if not given.  */
+
+static void
+check_real_width (format_token t, int w, int d, int e, locus *where)
+{
+  /* Letter, sign and digits; Ee with E zero writes at least one digit.  */
+  int exp = (e == 0 ? 1 : e) + 2;
+  int min_w;
+  switch (t)
+    {
+    case FMT_D:
+    case FMT_E:
+      min_w = d + 1 + exp;
+      break;
+    case FMT_EN:
+    case FMT_ES:
+      /* A nonzero digit precedes the decimal symbol.  */
+      min_w = d + 2 + exp;
+      break;
+    case FMT_G:
+      /* G with D == 0 edits like E.  Otherwise a value in range edits
+	 as F(w-n).(d-1) followed by n blanks, n being 4 for E zero or
+	 not given and E + 2 otherwise, and one out of range like E.  */
+      min_w = d + 1 + exp;
+      if (d != 0)
+	min_w = MIN (min_w, (e == 0 ? 4 : e + 2) + f_min_width (d - 1));
+      break;
+    default:
+      return;
+    }
+  if (w < min_w)
+    gfc_warning (OPT_Wformat_width, "Field width of the %s edit "
+		 "descriptor at %L too small for the number of digits",
+		 token_to_string (t), where);
+}
+
+/* Which diagnostics check_format issues.  */
+
+enum format_check_kind
+{
+  FMT_CHECK_NORMAL,	/* All of them.  */
+  FMT_CHECK_DECL,	/* No width warnings; I/O direction unknown.  */
+  FMT_CHECK_WIDTH	/* Only the width warnings, after FMT_CHECK_DECL.  */
+};
+
 /* Check a format statement.  The format string, either from a FORMAT
    statement or a constant in an I/O statement has already been parsed
    by itself, and we are checking it for validity.  The dual origin
    means that the warning message is a little less than great.  */
 
 static bool
-check_format (bool is_input)
+check_format (bool is_input, format_check_kind kind = FMT_CHECK_NORMAL)
 {
   const char *posint_required
     = G_("Positive width required in format string at %L");
@@ -640,6 +698,7 @@ check_format (bool is_input)
   format_token t, u;
   int level;
   int repeat;
+  int value_w, value_d, value_e;
   bool rv;
 
   use_last_char = 0;
@@ -764,8 +823,9 @@ format_item_1:
 	return false;
       if (t != FMT_RPAREN || level > 0)
 	{
-	  gfc_warning (0, "$ should be the last specifier in format at %L",
-		       &format_locus);
+	  if (kind != FMT_CHECK_WIDTH)
+	    gfc_warning (0, "$ should be the last specifier in format at %L",
+			 &format_locus);
 	  goto optional_comma_1;
 	}
 
@@ -877,8 +937,9 @@ data_desc:
 	  switch (gfc_notification_std (GFC_STD_GNU))
 	    {
 	      case WARNING:
-		gfc_warning (0, "Extension: Zero width after L "
-			     "descriptor at %L", &format_locus);
+		if (kind != FMT_CHECK_WIDTH)
+		  gfc_warning (0, "Extension: Zero width after L "
+			       "descriptor at %L", &format_locus);
 		break;
 	      case ERROR:
 		gfc_error ("Extension: Zero width after L "
@@ -917,10 +978,12 @@ data_desc:
     case FMT_G:
     case FMT_EN:
     case FMT_ES:
+      value_w = value_d = 0;
+      value_e = 2;
       u = format_lex ();
       if (t == FMT_G && u == FMT_ZERO)
 	{
-	  if (is_input)
+	  if (is_input && kind != FMT_CHECK_WIDTH)
 	    {
 	      error = zero_width;
 	      goto syntax;
@@ -987,6 +1050,8 @@ data_desc:
 	    }
 	}
 
+      value_w = value;
+
       u = format_lex ();
       if (u == FMT_ERROR)
 	goto fail;
@@ -1002,7 +1067,7 @@ data_desc:
 	      saved_token = u;
               goto fail;
 	    }
-	  else
+	  else if (kind != FMT_CHECK_WIDTH)
 	    gfc_warning (0, "Period required in format "
 			 "specifier %s at %L", token_to_string (t),
 			  &format_locus);
@@ -1022,8 +1087,14 @@ data_desc:
 	  goto syntax;
 	}
 
+      value_d = value;
+
       if (t == FMT_D)
-	break;
+	{
+	  if (kind != FMT_CHECK_DECL && !is_input && value_w != 0)
+	    check_real_width (t, value_w, value_d, value_e, &format_locus);
+	  break;
+	}
 
       /* Look for optional exponent.  */
       u = format_lex ();
@@ -1047,6 +1118,7 @@ data_desc:
 		      saved_token = u;
 		      goto fail;
 		    }
+		  value_e = 0;
 		}
 	      else
 		{
@@ -1055,8 +1127,12 @@ data_desc:
 		  goto syntax;
 		}
 	    }
+	  else
+	    value_e = value;
 	}
 
+      if (kind != FMT_CHECK_DECL && !is_input && value_w != 0)
+	check_real_width (t, value_w, value_d, value_e, &format_locus);
       break;
 
     case FMT_DT:
@@ -1126,12 +1202,14 @@ data_desc:
 	  error = nonneg_required;
 	  goto syntax;
 	}
-      else if (is_input && t == FMT_ZERO)
+      else if (is_input && kind != FMT_CHECK_WIDTH && t == FMT_ZERO)
 	{
 	  error = posint_required;
 	  goto syntax;
 	}
 
+      value_w = value;
+
       t = format_lex ();
       if (t == FMT_ERROR)
 	goto fail;
@@ -1146,8 +1224,9 @@ data_desc:
 	    }
 	  if (mode != MODE_FORMAT)
 	    format_locus.nextc += format_string_pos;
-	  gfc_warning (0, "Period required in format specifier at %L",
-		       &format_locus);
+	  if (kind != FMT_CHECK_WIDTH)
+	    gfc_warning (0, "Period required in format specifier at %L",
+			 &format_locus);
 	  saved_token = t;
 	  break;
 	}
@@ -1161,6 +1240,13 @@ data_desc:
 	  goto syntax;
 	}
 
+      value_d = value;
+
+      if (kind != FMT_CHECK_DECL && !is_input && value_w != 0
+	  && value_w < f_min_width (value_d))
+	gfc_warning (OPT_Wformat_width, "Field width of the F edit "
+		     "descriptor at %L too small for the number of digits",
+		     &format_locus);
       break;
 
     case FMT_H:
@@ -1172,8 +1258,9 @@ data_desc:
 	}
       if (mode != MODE_FORMAT)
 	format_locus.nextc += format_string_pos;
-      gfc_warning (0, "The H format specifier at %L is"
-		   " a Fortran 95 deleted feature", &format_locus);
+      if (kind != FMT_CHECK_WIDTH)
+	gfc_warning (0, "The H format specifier at %L is"
+		     " a Fortran 95 deleted feature", &format_locus);
       if (mode == MODE_STRING)
 	{
 	  format_string += value;
@@ -1208,7 +1295,7 @@ data_desc:
 	      goto syntax;
 	    }
 	}
-      else if (is_input && t == FMT_ZERO)
+      else if (is_input && kind != FMT_CHECK_WIDTH && t == FMT_ZERO)
 	{
 	  error = posint_required;
 	  goto syntax;
@@ -1361,7 +1448,8 @@ finished:
    like a format string.  */
 
 static bool
-check_format_string (gfc_expr *e, bool is_input)
+check_format_string (gfc_expr *e, bool is_input,
+		      format_check_kind kind = FMT_CHECK_NORMAL)
 {
   bool rv;
   int i;
@@ -1375,12 +1463,24 @@ check_format_string (gfc_expr *e, bool is_input)
      format string that has been calculated, but that's probably not worth the
      effort.  */
   format_locus = e->where;
-  rv = check_format (is_input);
+  /* The first pass already reported the errors and standard notes.  */
+  if (kind == FMT_CHECK_WIDTH)
+    {
+      gfc_push_suppress_errors ();
+      checking_width_only = true;
+    }
+  rv = check_format (is_input, kind);
+  if (kind == FMT_CHECK_WIDTH)
+    {
+      gfc_pop_suppress_errors ();
+      checking_width_only = false;
+    }
   /* check for extraneous characters at the end of an otherwise valid format
      string, like '(A10,I3)F5'
      start at the end and move back to the last character processed,
      spaces are OK */
-  if (rv && e->value.character.length > format_string_pos)
+  if (kind != FMT_CHECK_WIDTH
+      && rv && e->value.character.length > format_string_pos)
     for (i=e->value.character.length-1;i>format_string_pos-1;i--)
       if (e->value.character.string[i] != ' ')
         {
@@ -1433,7 +1533,7 @@ gfc_match_format (void)
 
   start = gfc_current_locus;
 
-  if (!check_format (false))
+  if (!check_format (false, FMT_CHECK_DECL))
     return MATCH_ERROR;
 
   if (gfc_match_eos () != MATCH_YES)
@@ -1456,7 +1556,7 @@ gfc_match_format (void)
   gfc_statement_label->format = e;
 
   mode = MODE_COPY;
-  check_format (false);		/* Guaranteed to succeed */
+  check_format (false, FMT_CHECK_DECL);	/* Guaranteed to succeed */
   gfc_match_eos ();		/* Guaranteed to succeed */
 
   return MATCH_YES;
@@ -3554,6 +3654,16 @@ gfc_resolve_dt (gfc_code *dt_code, gfc_dt *dt, locus *loc)
       return false;
     }
 
+  /* A labeled FORMAT's width warnings apply only to output; issue them
+     once per FORMAT.  */
+  if (warn_format_width && k != M_READ
+      && dt->format_label && dt->format_label != &format_asterisk
+      && !dt->format_label->output_width_checked)
+    {
+      dt->format_label->output_width_checked = true;
+      check_format_string (dt->format_label->format, false, FMT_CHECK_WIDTH);
+    }
+
   return true;
 }
 
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
index d33fc9ad3a9..99713e5cf69 100644
--- a/gcc/fortran/lang.opt
+++ b/gcc/fortran/lang.opt
@@ -253,6 +253,10 @@ Wextra
 Fortran Warning
 ; Documented in common
 
+Wformat-width
+Fortran Warning Var(warn_format_width) LangEnabledBy(Fortran,Wall)
+Warn about D, E, EN, ES, F and G output edit descriptors too narrow for their digits.
+
 Wfrontend-loop-interchange
 Fortran Var(flag_warn_frontend_loop_interchange)
 Warn if loops have been interchanged.
diff --git a/gcc/testsuite/gfortran.dg/pr45129.f90 b/gcc/testsuite/gfortran.dg/pr45129.f90
new file mode 100644
index 00000000000..1e26d223170
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr45129.f90
@@ -0,0 +1,45 @@
+! { dg-do compile }
+! { dg-options "-Wall" }
+!
+! PR 45129
+
+program p
+  real :: x = 1.0
+  character(20) :: s
+100 format (F2.3)   ! { dg-warning "Field width of the F edit descriptor" }
+200 format (E5.3)   ! { dg-warning "Field width of the E edit descriptor" }
+300 format (G4.2)   ! { dg-warning "Field width of the G edit descriptor" }
+400 format (F1.0)
+500 format (F4.2)
+  read (s, 400) x
+  read (s, 100) x
+  write (s, 100) x
+  write (s, 100) x
+  print 200, x
+  write (s, 300) x
+  write (s, 500) x
+  read (s, '(F1.0)') x
+  write (s, '(F1.0)') x      ! { dg-warning "Field width of the F edit descriptor" }
+  write (s, '(F2.0)') x
+  write (s, '(F2.2)') x      ! { dg-warning "Field width of the F edit descriptor" }
+  write (s, '(F3.2)') x
+  write (s, '(E6.2)') x      ! { dg-warning "Field width of the E edit descriptor" }
+  write (s, '(E7.2)') x
+  write (s, '(E7.2E3)') x    ! { dg-warning "Field width of the E edit descriptor" }
+  write (s, '(E8.2E3)') x
+  write (s, '(G5.2)') x      ! { dg-warning "Field width of the G edit descriptor" }
+  write (s, '(G6.2)') x
+  write (s, '(E4.1E0)') x    ! { dg-warning "Field width of the E edit descriptor" }
+  write (s, '(E5.1E0)') x
+  write (s, '(ES4.1E0)') x   ! { dg-warning "Field width of the ES edit descriptor" }
+  write (s, '(ES5.1E0)') x   ! { dg-warning "Field width of the ES edit descriptor" }
+  write (s, '(ES6.1E0)') x
+  write (s, '(ES7.2)') x     ! { dg-warning "Field width of the ES edit descriptor" }
+  write (s, '(ES8.2)') x
+  write (s, '(EN5.1E0)') x   ! { dg-warning "Field width of the EN edit descriptor" }
+  write (s, '(EN6.1E0)') x
+  write (s, '(D6.2)') x      ! { dg-warning "Field width of the D edit descriptor" }
+  write (s, '(D7.2)') x
+  write (s, '(G4.1E0)') x    ! { dg-warning "Field width of the G edit descriptor" }
+  write (s, '(G5.1E0)') x
+end program
-- 
2.56.1

Reply via email to