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?

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 9a2124a7b6a25737d3f6b1dbc0e729973c70da24 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 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.
---
 gcc/fortran/gfortran.h                |   3 +
 gcc/fortran/invoke.texi               |  12 ++-
 gcc/fortran/io.cc                     | 123 ++++++++++++++++++++++----
 gcc/fortran/lang.opt                  |   4 +
 gcc/testsuite/gfortran.dg/pr45129.f90 |  32 +++++++
 5 files changed, 154 insertions(+), 20 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..1c276522e47 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 an @code{E}, @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..351848300bc 100644
--- a/gcc/fortran/io.cc
+++ b/gcc/fortran/io.cc
@@ -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,30 @@ 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;
+}
+
+/* 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 +660,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 +785,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 +899,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 +940,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 +1012,8 @@ data_desc:
 	    }
 	}
 
+      value_w = value;
+
       u = format_lex ();
       if (u == FMT_ERROR)
 	goto fail;
@@ -1002,7 +1029,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,6 +1049,8 @@ data_desc:
 	  goto syntax;
 	}
 
+      value_d = value;
+
       if (t == FMT_D)
 	break;
 
@@ -1047,6 +1076,7 @@ data_desc:
 		      saved_token = u;
 		      goto fail;
 		    }
+		  value_e = 0;
 		}
 	      else
 		{
@@ -1055,8 +1085,29 @@ data_desc:
 		  goto syntax;
 		}
 	    }
+	  else
+	    value_e = value;
 	}
 
+      /* Period, D digits, exponent letter and sign, E exponent digits.  */
+      if (kind != FMT_CHECK_DECL && t == FMT_E && !is_input && value_w != 0
+	  && value_w < value_d + value_e + 3)
+	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), &format_locus);
+
+      /* G with D == 0 edits like E; otherwise its narrowest output is
+	 F(w-n).(d-1) followed by n = E + 2 blanks.  */
+      if (kind != FMT_CHECK_DECL && t == FMT_G && !is_input && value_w != 0)
+	{
+	  int g_floor = (value_d == 0
+			 ? value_e + 3
+			 : value_e + 2 + f_min_width (value_d - 1));
+	  if (value_w < g_floor)
+	    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), &format_locus);
+	}
       break;
 
     case FMT_DT:
@@ -1126,12 +1177,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 +1199,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 +1215,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 +1233,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 +1270,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 +1423,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 +1438,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 +1508,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 +1531,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 +3629,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..aa504384209 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 E, 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..bdc91005eaa
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr45129.f90
@@ -0,0 +1,32 @@
+! { 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
+end program
-- 
2.56.1

Reply via email to