Blame SOURCES/0001-Default-widths-for-i-f-and-g-format-specifiers-in-fo.patch

ac4b94
From f3e3034684c7ac44a14c70d6a248d8acee303176 Mon Sep 17 00:00:00 2001
ac4b94
From: law <law@138bc75d-0d04-0410-961f-82ee72b054a4>
ac4b94
Date: Thu, 10 May 2018 11:48:34 +0100
ac4b94
Subject: [PATCH 01/16] Default widths for i, f and g format specifiers in
ac4b94
 format strings.
ac4b94
ac4b94
Enabled using -fdec.
ac4b94
ac4b94
The behaviour is modelled on the Oracle Fortran compiler. At the time
ac4b94
of writing, the details were available at this URL:
ac4b94
ac4b94
  https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnc3/index.html#z4000743746d
ac4b94
ac4b94
Addition by Mark Eggleston <mark.eggleston@codethink.com>:
ac4b94
ac4b94
Use -fdec-format-defaults to enable this feature. Also enabled using -fdec.
ac4b94
---
ac4b94
 gcc/fortran/io.c                                   | 31 +++++++++++--
ac4b94
 gcc/fortran/lang.opt                               |  4 ++
ac4b94
 gcc/fortran/options.c                              |  1 +
ac4b94
 .../gfortran.dg/fmt_f_default_field_width_1.f90    | 43 ++++++++++++++++++
ac4b94
 .../gfortran.dg/fmt_f_default_field_width_2.f90    | 46 +++++++++++++++++++
ac4b94
 .../gfortran.dg/fmt_f_default_field_width_3.f90    | 28 ++++++++++++
ac4b94
 .../gfortran.dg/fmt_g_default_field_width_1.f90    | 48 ++++++++++++++++++++
ac4b94
 .../gfortran.dg/fmt_g_default_field_width_2.f90    | 52 ++++++++++++++++++++++
ac4b94
 .../gfortran.dg/fmt_g_default_field_width_3.f90    | 31 +++++++++++++
ac4b94
 .../gfortran.dg/fmt_i_default_field_width_1.f90    | 38 ++++++++++++++++
ac4b94
 .../gfortran.dg/fmt_i_default_field_width_2.f90    | 42 +++++++++++++++++
ac4b94
 .../gfortran.dg/fmt_i_default_field_width_3.f90    | 35 +++++++++++++++
ac4b94
 libgfortran/io/format.c                            | 35 +++++++++++++++
ac4b94
 libgfortran/io/io.h                                | 50 +++++++++++++++++++++
ac4b94
 libgfortran/io/read.c                              |  6 +++
ac4b94
 libgfortran/io/write.c                             | 22 +++++----
ac4b94
 libgfortran/io/write_float.def                     | 37 ++++++++++++---
ac4b94
 17 files changed, 531 insertions(+), 18 deletions(-)
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_f_default_field_width_1.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_f_default_field_width_2.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_f_default_field_width_3.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_g_default_field_width_1.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_g_default_field_width_2.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_g_default_field_width_3.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_i_default_field_width_1.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_i_default_field_width_2.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_i_default_field_width_3.f90
ac4b94
840d93
diff --git a/gcc/fortran/io.c b/gcc/fortran/io.c
ac4b94
index 9828897852a..57117579627 100644
840d93
--- a/gcc/fortran/io.c
840d93
+++ b/gcc/fortran/io.c
ac4b94
@@ -903,6 +903,13 @@ data_desc:
840d93
 
840d93
       if (u != FMT_POSINT)
840d93
 	{
ac4b94
+	  if (flag_dec_format_defaults)
840d93
+	    {
840d93
+	      /* Assume a default width based on the variable size.  */
840d93
+	      saved_token = u;
840d93
+	      break;
840d93
+	    }
840d93
+
840d93
 	  format_locus.nextc += format_string_pos;
840d93
 	  gfc_error ("Positive width required in format "
840d93
 			 "specifier %s at %L", token_to_string (t),
ac4b94
@@ -1027,6 +1034,13 @@ data_desc:
840d93
 	goto fail;
840d93
       if (t != FMT_ZERO && t != FMT_POSINT)
840d93
 	{
ac4b94
+	  if (flag_dec_format_defaults)
840d93
+	    {
840d93
+	      /* Assume the default width is expected here and continue lexing.  */
840d93
+	      value = 0; /* It doesn't matter what we set the value to here.  */
840d93
+	      saved_token = t;
840d93
+	      break;
840d93
+	    }
840d93
 	  error = nonneg_required;
840d93
 	  goto syntax;
840d93
 	}
ac4b94
@@ -1096,8 +1110,17 @@ data_desc:
840d93
 	goto fail;
840d93
       if (t != FMT_ZERO && t != FMT_POSINT)
840d93
 	{
840d93
-	  error = nonneg_required;
840d93
-	  goto syntax;
ac4b94
+	  if (flag_dec_format_defaults)
840d93
+	    {
840d93
+	      /* Assume the default width is expected here and continue lexing.  */
840d93
+	      value = 0; /* It doesn't matter what we set the value to here.  */
840d93
+	      saved_token = t;
840d93
+	    }
840d93
+	  else
840d93
+	    {
840d93
+	      error = nonneg_required;
840d93
+	      goto syntax;
840d93
+	    }
840d93
 	}
840d93
       else if (is_input && t == FMT_ZERO)
840d93
 	{
ac4b94
@@ -4368,8 +4391,8 @@ get_io_list:
ac4b94
     }
ac4b94
 
ac4b94
   /* See if we want to use defaults for missing exponents in real transfers
ac4b94
-     and other DEC runtime extensions.  */
ac4b94
-  if (flag_dec)
ac4b94
+     and other DEC runtime extensions. */
ac4b94
+  if (flag_dec_format_defaults)
ac4b94
     dt->dec_ext = 1;
ac4b94
 
ac4b94
   /* A full IO statement has been matched.  Check the constraints.  spec_end is
ac4b94
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
ac4b94
index 9151d02c491..26e82601b62 100644
ac4b94
--- a/gcc/fortran/lang.opt
ac4b94
+++ b/gcc/fortran/lang.opt
ac4b94
@@ -444,6 +444,10 @@ fdec-include
ac4b94
 Fortran Var(flag_dec_include)
ac4b94
 Enable legacy parsing of INCLUDE as statement.
ac4b94
 
ac4b94
+fdec-format-defaults
ac4b94
+Fortran Var(flag_dec_format_defaults)
ac4b94
+Enable default widths for i, f and g format specifiers.
ac4b94
+
ac4b94
 fdec-intrinsic-ints
ac4b94
 Fortran Var(flag_dec_intrinsic_ints)
ac4b94
 Enable kind-specific variants of integer intrinsic functions.
ac4b94
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
ac4b94
index 02970d59066..4f91486e977 100644
ac4b94
--- a/gcc/fortran/options.c
ac4b94
+++ b/gcc/fortran/options.c
ac4b94
@@ -74,6 +74,7 @@ set_dec_flags (int value)
ac4b94
   SET_BITFLAG (flag_dec_static, value, value);
ac4b94
   SET_BITFLAG (flag_dec_math, value, value);
ac4b94
   SET_BITFLAG (flag_dec_include, value, value);
ac4b94
+  SET_BITFLAG (flag_dec_format_defaults, value, value);
ac4b94
 }
ac4b94
 
ac4b94
 /* Finalize DEC flags.  */
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/fmt_f_default_field_width_1.f90 b/gcc/testsuite/gfortran.dg/fmt_f_default_field_width_1.f90
840d93
new file mode 100644
ac4b94
index 00000000000..49c77155761
840d93
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/fmt_f_default_field_width_1.f90
840d93
@@ -0,0 +1,43 @@
840d93
+! { dg-do run }
840d93
+! { dg-options -fdec }
840d93
+!
840d93
+! Test case for the default field widths enabled by the -fdec-format-defaults flag.
840d93
+!
840d93
+! This feature is not part of any Fortran standard, but it is supported by the
840d93
+! Oracle Fortran compiler and others.
840d93
+!
840d93
+! libgfortran uses printf() internally to implement FORMAT. If you print float
840d93
+! values to a higher precision than the type can actually store, the results
840d93
+! are implementation dependent: some platforms print zeros, others print random
840d93
+! numbers. Don't depend on this behaviour in tests because they will not be
840d93
+! portable.
840d93
+
840d93
+    character(50) :: buffer
840d93
+
840d93
+    real*4 :: real_4
840d93
+    real*8 :: real_8
840d93
+    real*16 :: real_16
840d93
+    integer :: len
840d93
+
840d93
+    real_4 = 4.18
840d93
+    write(buffer, '(A, F, A)') ':',real_4,':'
840d93
+    print *,buffer
ac4b94
+    if (buffer.ne.":      4.1799998:") stop 1
840d93
+
840d93
+    real_4 = 0.00000018
840d93
+    write(buffer, '(A, F, A)') ':',real_4,':'
840d93
+    print *,buffer
ac4b94
+    if (buffer.ne.":      0.0000002:") stop 2
840d93
+
840d93
+    real_8 = 4.18
840d93
+    write(buffer, '(A, F, A)') ':',real_8,':'
840d93
+    print *,buffer
840d93
+    len = len_trim(buffer)
ac4b94
+    if (len /= 27) stop 3
840d93
+
840d93
+    real_16 = 4.18
840d93
+    write(buffer, '(A, F, A)') ':',real_16,':'
840d93
+    print *,buffer
840d93
+    len = len_trim(buffer)
ac4b94
+    if (len /= 44) stop 4
840d93
+end
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/fmt_f_default_field_width_2.f90 b/gcc/testsuite/gfortran.dg/fmt_f_default_field_width_2.f90
840d93
new file mode 100644
ac4b94
index 00000000000..1c2ec0413a7
840d93
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/fmt_f_default_field_width_2.f90
ac4b94
@@ -0,0 +1,46 @@
ac4b94
+! { dg-do run }
ac4b94
+! { dg-options -fdec-format-defaults }
ac4b94
+!
ac4b94
+! Test case for the default field widths enabled by the -fdec-format-defaults flag.
ac4b94
+!
ac4b94
+! This feature is not part of any Fortran standard, but it is supported by the
ac4b94
+! Oracle Fortran compiler and others.
ac4b94
+!
ac4b94
+! libgfortran uses printf() internally to implement FORMAT. If you print float
ac4b94
+! values to a higher precision than the type can actually store, the results
ac4b94
+! are implementation dependent: some platforms print zeros, others print random
ac4b94
+! numbers. Don't depend on this behaviour in tests because they will not be
ac4b94
+! portable.
ac4b94
+!
ac4b94
+! Test case added by Mark Eggleston <mark.eggleston@codethink.com> to check
ac4b94
+! use of -fdec-format-defaults
ac4b94
+!
ac4b94
+    character(50) :: buffer
ac4b94
+
ac4b94
+    real*4 :: real_4
ac4b94
+    real*8 :: real_8
ac4b94
+    real*16 :: real_16
ac4b94
+    integer :: len
ac4b94
+
ac4b94
+    real_4 = 4.18
ac4b94
+    write(buffer, '(A, F, A)') ':',real_4,':'
ac4b94
+    print *,buffer
ac4b94
+    if (buffer.ne.":      4.1799998:") stop 1
ac4b94
+
ac4b94
+    real_4 = 0.00000018
ac4b94
+    write(buffer, '(A, F, A)') ':',real_4,':'
ac4b94
+    print *,buffer
ac4b94
+    if (buffer.ne.":      0.0000002:") stop 2
ac4b94
+
ac4b94
+    real_8 = 4.18
ac4b94
+    write(buffer, '(A, F, A)') ':',real_8,':'
ac4b94
+    print *,buffer
ac4b94
+    len = len_trim(buffer)
ac4b94
+    if (len /= 27) stop 3
ac4b94
+
ac4b94
+    real_16 = 4.18
ac4b94
+    write(buffer, '(A, F, A)') ':',real_16,':'
ac4b94
+    print *,buffer
ac4b94
+    len = len_trim(buffer)
ac4b94
+    if (len /= 44) stop 4
ac4b94
+end
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/fmt_f_default_field_width_3.f90 b/gcc/testsuite/gfortran.dg/fmt_f_default_field_width_3.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..e513063189b
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/fmt_f_default_field_width_3.f90
ac4b94
@@ -0,0 +1,28 @@
ac4b94
+! { dg-do compile }
ac4b94
+! { dg-options "-fdec -fno-dec-format-defaults" }
ac4b94
+!
ac4b94
+! Test case for the default field widths not enabled.
ac4b94
+!
ac4b94
+! Test case added by Mark Eggleston <mark.eggleston@codethink.com> to check
ac4b94
+! use of -fno-dec-format-defaults
ac4b94
+!
ac4b94
+
ac4b94
+    character(50) :: buffer
ac4b94
+
ac4b94
+    real*4 :: real_4
ac4b94
+    real*8 :: real_8
ac4b94
+    real*16 :: real_16
ac4b94
+    integer :: len
ac4b94
+
ac4b94
+    real_4 = 4.18
ac4b94
+    write(buffer, '(A, F, A)') ':',real_4,':' ! { dg-error "Nonnegative width required" }
ac4b94
+
ac4b94
+    real_4 = 0.00000018
ac4b94
+    write(buffer, '(A, F, A)') ':',real_4,':' ! { dg-error "Nonnegative width required" }
ac4b94
+
ac4b94
+    real_8 = 4.18
ac4b94
+    write(buffer, '(A, F, A)') ':',real_8,':' ! { dg-error "Nonnegative width required" }
ac4b94
+
ac4b94
+    real_16 = 4.18
ac4b94
+    write(buffer, '(A, F, A)') ':',real_16,':' ! { dg-error "Nonnegative width required" }
ac4b94
+end
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/fmt_g_default_field_width_1.f90 b/gcc/testsuite/gfortran.dg/fmt_g_default_field_width_1.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..6e2ad141d4a
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/fmt_g_default_field_width_1.f90
840d93
@@ -0,0 +1,48 @@
840d93
+! { dg-do run }
840d93
+! { dg-options -fdec }
840d93
+!
840d93
+! Test case for the default field widths enabled by the -fdec-format-defaults flag.
840d93
+!
840d93
+! This feature is not part of any Fortran standard, but it is supported by the
840d93
+! Oracle Fortran compiler and others.
840d93
+!
840d93
+! libgfortran uses printf() internally to implement FORMAT. If you print float
840d93
+! values to a higher precision than the type can actually store, the results
840d93
+! are implementation dependent: some platforms print zeros, others print random
840d93
+! numbers. Don't depend on this behaviour in tests because they will not be
840d93
+! portable.
840d93
+
840d93
+    character(50) :: buffer
840d93
+
840d93
+    real*4 :: real_4
840d93
+    real*8 :: real_8
840d93
+    real*16 :: real_16
840d93
+    integer :: len
840d93
+
840d93
+    real_4 = 4.18
840d93
+    write(buffer, '(A, G, A)') ':',real_4,':'
840d93
+    print *,buffer
ac4b94
+    if (buffer.ne.":   4.180000    :") stop 1
840d93
+
840d93
+    real_4 = 0.00000018
840d93
+    write(buffer, '(A, G, A)') ':',real_4,':'
840d93
+    print *,buffer
ac4b94
+    if (buffer.ne.":  0.1800000E-06:") stop 2
840d93
+
840d93
+    real_4 = 18000000.4
840d93
+    write(buffer, '(A, G, A)') ':',real_4,':'
840d93
+    print *,buffer
ac4b94
+    if (buffer.ne.":  0.1800000E+08:") stop 3
840d93
+
840d93
+    real_8 = 4.18
840d93
+    write(buffer, '(A, G, A)') ':',real_8,':'
840d93
+    print *,buffer
840d93
+    len = len_trim(buffer)
ac4b94
+    if (len /= 27) stop 4
840d93
+
840d93
+    real_16 = 4.18
840d93
+    write(buffer, '(A, G, A)') ':',real_16,':'
840d93
+    print *,buffer
840d93
+    len = len_trim(buffer)
ac4b94
+    if (len /= 44) stop 5
840d93
+end
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/fmt_g_default_field_width_2.f90 b/gcc/testsuite/gfortran.dg/fmt_g_default_field_width_2.f90
840d93
new file mode 100644
ac4b94
index 00000000000..7b218af8610
840d93
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/fmt_g_default_field_width_2.f90
ac4b94
@@ -0,0 +1,52 @@
ac4b94
+! { dg-do run }
ac4b94
+! { dg-options -fdec-format-defaults }
ac4b94
+!
ac4b94
+! Test case for the default field widths enabled by the -fdec-format-defaults flag.
ac4b94
+!
ac4b94
+! This feature is not part of any Fortran standard, but it is supported by the
ac4b94
+! Oracle Fortran compiler and others.
ac4b94
+!
ac4b94
+! libgfortran uses printf() internally to implement FORMAT. If you print float
ac4b94
+! values to a higher precision than the type can actually store, the results
ac4b94
+! are implementation dependent: some platforms print zeros, others print random
ac4b94
+! numbers. Don't depend on this behaviour in tests because they will not be
ac4b94
+! portable.
ac4b94
+!
ac4b94
+! Test case added by Mark Eggleston <mark.eggleston@codethink.com> to check
ac4b94
+! use of -fdec-format-defaults
ac4b94
+!
ac4b94
+
ac4b94
+    character(50) :: buffer
ac4b94
+
ac4b94
+    real*4 :: real_4
ac4b94
+    real*8 :: real_8
ac4b94
+    real*16 :: real_16
ac4b94
+    integer :: len
ac4b94
+
ac4b94
+    real_4 = 4.18
ac4b94
+    write(buffer, '(A, G, A)') ':',real_4,':'
ac4b94
+    print *,buffer
ac4b94
+    if (buffer.ne.":   4.180000    :") stop 1
ac4b94
+
ac4b94
+    real_4 = 0.00000018
ac4b94
+    write(buffer, '(A, G, A)') ':',real_4,':'
ac4b94
+    print *,buffer
ac4b94
+    if (buffer.ne.":  0.1800000E-06:") stop 2
ac4b94
+
ac4b94
+    real_4 = 18000000.4
ac4b94
+    write(buffer, '(A, G, A)') ':',real_4,':'
ac4b94
+    print *,buffer
ac4b94
+    if (buffer.ne.":  0.1800000E+08:") stop 3
ac4b94
+
ac4b94
+    real_8 = 4.18
ac4b94
+    write(buffer, '(A, G, A)') ':',real_8,':'
ac4b94
+    print *,buffer
ac4b94
+    len = len_trim(buffer)
ac4b94
+    if (len /= 27) stop 4
ac4b94
+
ac4b94
+    real_16 = 4.18
ac4b94
+    write(buffer, '(A, G, A)') ':',real_16,':'
ac4b94
+    print *,buffer
ac4b94
+    len = len_trim(buffer)
ac4b94
+    if (len /= 44) stop 5
ac4b94
+end
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/fmt_g_default_field_width_3.f90 b/gcc/testsuite/gfortran.dg/fmt_g_default_field_width_3.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..e255c2f94a0
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/fmt_g_default_field_width_3.f90
ac4b94
@@ -0,0 +1,31 @@
ac4b94
+! { dg-do compile }
ac4b94
+! { dg-options "-fdec -fno-dec-format-defaults" }
ac4b94
+!
ac4b94
+! Test case for the default field widths not enabled.
ac4b94
+!
ac4b94
+! Test case added by Mark Eggleston <mark.eggleston@codethink.com> to check
ac4b94
+! use of -fno-dec-format-defaults
ac4b94
+!
ac4b94
+
ac4b94
+    character(50) :: buffer
ac4b94
+
ac4b94
+    real*4 :: real_4
ac4b94
+    real*8 :: real_8
ac4b94
+    real*16 :: real_16
ac4b94
+    integer :: len
ac4b94
+
ac4b94
+    real_4 = 4.18
ac4b94
+    write(buffer, '(A, G, A)') ':',real_4,':' ! { dg-error "Positive width required" }
ac4b94
+
ac4b94
+    real_4 = 0.00000018
ac4b94
+    write(buffer, '(A, G, A)') ':',real_4,':' ! { dg-error "Positive width required" }
ac4b94
+
ac4b94
+    real_4 = 18000000.4
ac4b94
+    write(buffer, '(A, G, A)') ':',real_4,':' ! { dg-error "Positive width required" }
ac4b94
+
ac4b94
+    real_8 = 4.18
ac4b94
+    write(buffer, '(A, G, A)') ':',real_8,':' ! { dg-error "Positive width required" }
ac4b94
+
ac4b94
+    real_16 = 4.18
ac4b94
+    write(buffer, '(A, G, A)') ':',real_16,':' ! { dg-error "Positive width required" }
ac4b94
+end
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/fmt_i_default_field_width_1.f90 b/gcc/testsuite/gfortran.dg/fmt_i_default_field_width_1.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..0d32d240394
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/fmt_i_default_field_width_1.f90
840d93
@@ -0,0 +1,38 @@
840d93
+! { dg-do run }
840d93
+! { dg-options -fdec }
840d93
+!
840d93
+! Test case for the default field widths enabled by the -fdec-format-defaults flag.
840d93
+!
840d93
+! This feature is not part of any Fortran standard, but it is supported by the
840d93
+! Oracle Fortran compiler and others.
840d93
+
840d93
+    character(50) :: buffer
840d93
+    character(1) :: colon
840d93
+
840d93
+    integer*2 :: integer_2
840d93
+    integer*4 :: integer_4
840d93
+    integer*8 :: integer_8
840d93
+
840d93
+    write(buffer, '(A, I, A)') ':',12340,':'
840d93
+    print *,buffer
ac4b94
+    if (buffer.ne.":       12340:") stop 1
ac4b94
+
ac4b94
+    read(buffer, '(A1, I, A1)') colon, integer_4, colon
ac4b94
+    if (integer_4.ne.12340) stop 2
ac4b94
+
ac4b94
+    integer_2 = -99
ac4b94
+    write(buffer, '(A, I, A)') ':',integer_2,':'
ac4b94
+    print *,buffer
ac4b94
+    if (buffer.ne.":    -99:") stop 3
ac4b94
+
ac4b94
+    integer_8 = -11112222
ac4b94
+    write(buffer, '(A, I, A)') ':',integer_8,':'
ac4b94
+    print *,buffer
ac4b94
+    if (buffer.ne.":              -11112222:") stop 4
ac4b94
+
ac4b94
+! If the width is 7 and there are 7 leading zeroes, the result should be zero.
ac4b94
+    integer_2 = 789
ac4b94
+    buffer = '0000000789'
ac4b94
+    read(buffer, '(I)') integer_2
ac4b94
+    if (integer_2.ne.0) stop 5
ac4b94
+end
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/fmt_i_default_field_width_2.f90 b/gcc/testsuite/gfortran.dg/fmt_i_default_field_width_2.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..6cee3f86809
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/fmt_i_default_field_width_2.f90
ac4b94
@@ -0,0 +1,42 @@
ac4b94
+! { dg-do run }
ac4b94
+! { dg-options -fdec-format-defaults }
ac4b94
+!
ac4b94
+! Test case for the default field widths enabled by the -fdec-format-defaults flag.
ac4b94
+!
ac4b94
+! This feature is not part of any Fortran standard, but it is supported by the
ac4b94
+! Oracle Fortran compiler and others.
ac4b94
+!
ac4b94
+! Test case added by Mark Eggleston <mark.eggleston@codethink.com> to check
ac4b94
+! use of -fdec-format-defaults
ac4b94
+!
ac4b94
+
ac4b94
+    character(50) :: buffer
ac4b94
+    character(1) :: colon
ac4b94
+
ac4b94
+    integer*2 :: integer_2
ac4b94
+    integer*4 :: integer_4
ac4b94
+    integer*8 :: integer_8
ac4b94
+
ac4b94
+    write(buffer, '(A, I, A)') ':',12340,':'
ac4b94
+    print *,buffer
ac4b94
+    if (buffer.ne.":       12340:") stop 1
840d93
+
840d93
+    read(buffer, '(A1, I, A1)') colon, integer_4, colon
ac4b94
+    if (integer_4.ne.12340) stop 2
840d93
+
840d93
+    integer_2 = -99
840d93
+    write(buffer, '(A, I, A)') ':',integer_2,':'
840d93
+    print *,buffer
ac4b94
+    if (buffer.ne.":    -99:") stop 3
840d93
+
840d93
+    integer_8 = -11112222
840d93
+    write(buffer, '(A, I, A)') ':',integer_8,':'
840d93
+    print *,buffer
ac4b94
+    if (buffer.ne.":              -11112222:") stop 4
840d93
+
840d93
+! If the width is 7 and there are 7 leading zeroes, the result should be zero.
840d93
+    integer_2 = 789
840d93
+    buffer = '0000000789'
840d93
+    read(buffer, '(I)') integer_2
ac4b94
+    if (integer_2.ne.0) stop 5
ac4b94
+end
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/fmt_i_default_field_width_3.f90 b/gcc/testsuite/gfortran.dg/fmt_i_default_field_width_3.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..3a6684b3c4d
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/fmt_i_default_field_width_3.f90
ac4b94
@@ -0,0 +1,35 @@
ac4b94
+! { dg-do compile }
ac4b94
+! { dg-options "-fdec -fno-dec-format-defaults" }
ac4b94
+!
ac4b94
+! Test case for the default field widths enabled by the -fdec-format-defaults flag.
ac4b94
+!
ac4b94
+! This feature is not part of any Fortran standard, but it is supported by the
ac4b94
+! Oracle Fortran compiler and others.
ac4b94
+!
ac4b94
+! Test case added by Mark Eggleston <mark.eggleston@codethink.com> to check
ac4b94
+! use of -fdec-format-defaults
ac4b94
+!
ac4b94
+
ac4b94
+    character(50) :: buffer
ac4b94
+    character(1) :: colon
ac4b94
+
ac4b94
+    integer*2 :: integer_2
ac4b94
+    integer*4 :: integer_4
ac4b94
+    integer*8 :: integer_8
ac4b94
+
ac4b94
+    write(buffer, '(A, I, A)') ':',12340,':' ! { dg-error "Nonnegative width required" }
ac4b94
+
ac4b94
+    read(buffer, '(A1, I, A1)') colon, integer_4, colon ! { dg-error "Nonnegative width required" }
ac4b94
+    if (integer_4.ne.12340) stop 2
ac4b94
+
ac4b94
+    integer_2 = -99
ac4b94
+    write(buffer, '(A, I, A)') ':',integer_2,':' ! { dg-error "Nonnegative width required" }
ac4b94
+
ac4b94
+    integer_8 = -11112222
ac4b94
+    write(buffer, '(A, I, A)') ':',integer_8,':' ! { dg-error "Nonnegative width required" }
ac4b94
+
ac4b94
+! If the width is 7 and there are 7 leading zeroes, the result should be zero.
ac4b94
+    integer_2 = 789
ac4b94
+    buffer = '0000000789'
ac4b94
+    read(buffer, '(I)') integer_2 ! { dg-error "Nonnegative width required" }
840d93
+end
840d93
diff --git a/libgfortran/io/format.c b/libgfortran/io/format.c
ac4b94
index 688764785da..e798d9bda87 100644
840d93
--- a/libgfortran/io/format.c
840d93
+++ b/libgfortran/io/format.c
840d93
@@ -956,12 +956,33 @@ parse_format_list (st_parameter_dt *dtp, bool *seen_dd)
840d93
 	  *seen_dd = true;
840d93
 	  if (u != FMT_POSINT && u != FMT_ZERO)
840d93
 	    {
840d93
+	      if (dtp->common.flags & IOPARM_DT_DEC_EXT)
840d93
+		{
840d93
+		  tail->u.real.w = DEFAULT_WIDTH;
840d93
+		  tail->u.real.d = 0;
840d93
+		  tail->u.real.e = -1;
840d93
+		  fmt->saved_token = u;
840d93
+		  break;
840d93
+		}
840d93
 	      fmt->error = nonneg_required;
840d93
 	      goto finished;
840d93
 	    }
840d93
 	}
840d93
+      else if (u == FMT_ZERO)
840d93
+	{
840d93
+	  fmt->error = posint_required;
840d93
+	  goto finished;
840d93
+	}
840d93
       else if (u != FMT_POSINT)
840d93
 	{
840d93
+	  if (dtp->common.flags & IOPARM_DT_DEC_EXT)
840d93
+	    {
840d93
+	      tail->u.real.w = DEFAULT_WIDTH;
840d93
+	      tail->u.real.d = 0;
840d93
+	      tail->u.real.e = -1;
840d93
+	      fmt->saved_token = u;
840d93
+	      break;
840d93
+	    }
840d93
 	  fmt->error = posint_required;
840d93
 	  goto finished;
840d93
 	}
ac4b94
@@ -1100,6 +1121,13 @@ parse_format_list (st_parameter_dt *dtp, bool *seen_dd)
840d93
 	{
840d93
 	  if (t != FMT_POSINT)
840d93
 	    {
840d93
+	      if (dtp->common.flags & IOPARM_DT_DEC_EXT)
840d93
+		{
840d93
+		  tail->u.integer.w = DEFAULT_WIDTH;
840d93
+		  tail->u.integer.m = -1;
840d93
+		  fmt->saved_token = t;
840d93
+		  break;
840d93
+		}
840d93
 	      fmt->error = posint_required;
840d93
 	      goto finished;
840d93
 	    }
ac4b94
@@ -1108,6 +1136,13 @@ parse_format_list (st_parameter_dt *dtp, bool *seen_dd)
840d93
 	{
840d93
 	  if (t != FMT_ZERO && t != FMT_POSINT)
840d93
 	    {
840d93
+	      if (dtp->common.flags & IOPARM_DT_DEC_EXT)
840d93
+		{
840d93
+		  tail->u.integer.w = DEFAULT_WIDTH;
840d93
+		  tail->u.integer.m = -1;
840d93
+		  fmt->saved_token = t;
840d93
+		  break;
840d93
+		}
840d93
 	      fmt->error = nonneg_required;
840d93
 	      goto finished;
840d93
 	    }
840d93
diff --git a/libgfortran/io/io.h b/libgfortran/io/io.h
ac4b94
index 5caaea280f0..f5e63797ba1 100644
840d93
--- a/libgfortran/io/io.h
840d93
+++ b/libgfortran/io/io.h
ac4b94
@@ -1011,6 +1011,56 @@ memset4 (gfc_char4_t *p, gfc_char4_t c, int k)
840d93
     *p++ = c;
840d93
 }
840d93
 
840d93
+/* Used in width fields to indicate that the default should be used */
840d93
+#define DEFAULT_WIDTH -1
840d93
+
840d93
+/* Defaults for certain format field descriptors. These are decided based on
840d93
+ * the type of the value being formatted.
840d93
+ *
840d93
+ * The behaviour here is modelled on the Oracle Fortran compiler. At the time
840d93
+ * of writing, the details were available at this URL:
840d93
+ *
840d93
+ *   https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnc3/index.html#z4000743746d
840d93
+ */
840d93
+
840d93
+static inline int
840d93
+default_width_for_integer (int kind)
840d93
+{
840d93
+  switch (kind)
840d93
+    {
840d93
+    case 1:
840d93
+    case 2:  return  7;
840d93
+    case 4:  return 12;
840d93
+    case 8:  return 23;
840d93
+    case 16: return 44;
840d93
+    default: return  0;
840d93
+    }
840d93
+}
840d93
+
840d93
+static inline int
840d93
+default_width_for_float (int kind)
840d93
+{
840d93
+  switch (kind)
840d93
+    {
840d93
+    case 4:  return 15;
840d93
+    case 8:  return 25;
840d93
+    case 16: return 42;
840d93
+    default: return  0;
840d93
+    }
840d93
+}
840d93
+
840d93
+static inline int
840d93
+default_precision_for_float (int kind)
840d93
+{
840d93
+  switch (kind)
840d93
+    {
840d93
+    case 4:  return 7;
840d93
+    case 8:  return 16;
840d93
+    case 16: return 33;
840d93
+    default: return 0;
840d93
+    }
840d93
+}
840d93
+
840d93
 #endif
840d93
 
ac4b94
 extern void
840d93
diff --git a/libgfortran/io/read.c b/libgfortran/io/read.c
ac4b94
index 52ffb4639ac..be9f6cb6f76 100644
840d93
--- a/libgfortran/io/read.c
840d93
+++ b/libgfortran/io/read.c
ac4b94
@@ -635,6 +635,12 @@ read_decimal (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
840d93
 
840d93
   w = f->u.w;
840d93
 
840d93
+  /* This is a legacy extension, and the frontend will only allow such cases
840d93
+   * through when -fdec-format-defaults is passed.
840d93
+   */
840d93
+  if (w == DEFAULT_WIDTH)
840d93
+    w = default_width_for_integer (length);
840d93
+
840d93
   p = read_block_form (dtp, &w);
840d93
 
840d93
   if (p == NULL)
840d93
diff --git a/libgfortran/io/write.c b/libgfortran/io/write.c
ac4b94
index c8811e200e0..4ef35561fdd 100644
840d93
--- a/libgfortran/io/write.c
840d93
+++ b/libgfortran/io/write.c
ac4b94
@@ -685,9 +685,8 @@ write_l (st_parameter_dt *dtp, const fnode *f, char *source, int len)
840d93
   p[wlen - 1] = (n) ? 'T' : 'F';
840d93
 }
840d93
 
840d93
-
840d93
 static void
840d93
-write_boz (st_parameter_dt *dtp, const fnode *f, const char *q, int n)
840d93
+write_boz (st_parameter_dt *dtp, const fnode *f, const char *q, int n, int len)
840d93
 {
840d93
   int w, m, digits, nzero, nblank;
840d93
   char *p;
ac4b94
@@ -720,6 +719,9 @@ write_boz (st_parameter_dt *dtp, const fnode *f, const char *q, int n)
840d93
   /* Select a width if none was specified.  The idea here is to always
840d93
      print something.  */
840d93
 
840d93
+  if (w == DEFAULT_WIDTH)
840d93
+    w = default_width_for_integer (len);
840d93
+
840d93
   if (w == 0)
840d93
     w = ((digits < m) ? m : digits);
840d93
 
ac4b94
@@ -846,6 +848,8 @@ write_decimal (st_parameter_dt *dtp, const fnode *f, const char *source,
840d93
 
840d93
   /* Select a width if none was specified.  The idea here is to always
840d93
      print something.  */
840d93
+  if (w == DEFAULT_WIDTH)
840d93
+    w = default_width_for_integer (len);
840d93
 
840d93
   if (w == 0)
840d93
     w = ((digits < m) ? m : digits) + nsign;
ac4b94
@@ -1206,13 +1210,13 @@ write_b (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
840d93
   if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
840d93
     {
840d93
       p = btoa_big (source, itoa_buf, len, &n);
840d93
-      write_boz (dtp, f, p, n);
840d93
+      write_boz (dtp, f, p, n, len);
840d93
     }
840d93
   else
840d93
     {
840d93
       n = extract_uint (source, len);
840d93
       p = btoa (n, itoa_buf, sizeof (itoa_buf));
840d93
-      write_boz (dtp, f, p, n);
840d93
+      write_boz (dtp, f, p, n, len);
840d93
     }
840d93
 }
840d93
 
ac4b94
@@ -1227,13 +1231,13 @@ write_o (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
840d93
   if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
840d93
     {
840d93
       p = otoa_big (source, itoa_buf, len, &n);
840d93
-      write_boz (dtp, f, p, n);
840d93
+      write_boz (dtp, f, p, n, len);
840d93
     }
840d93
   else
840d93
     {
840d93
       n = extract_uint (source, len);
840d93
       p = otoa (n, itoa_buf, sizeof (itoa_buf));
840d93
-      write_boz (dtp, f, p, n);
840d93
+      write_boz (dtp, f, p, n, len);
840d93
     }
840d93
 }
840d93
 
ac4b94
@@ -1247,13 +1251,13 @@ write_z (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
840d93
   if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
840d93
     {
840d93
       p = ztoa_big (source, itoa_buf, len, &n);
840d93
-      write_boz (dtp, f, p, n);
840d93
+      write_boz (dtp, f, p, n, len);
840d93
     }
840d93
   else
840d93
     {
840d93
       n = extract_uint (source, len);
840d93
       p = gfc_xtoa (n, itoa_buf, sizeof (itoa_buf));
840d93
-      write_boz (dtp, f, p, n);
840d93
+      write_boz (dtp, f, p, n, len);
840d93
     }
840d93
 }
840d93
 
ac4b94
@@ -1491,7 +1495,7 @@ size_from_kind (st_parameter_dt *dtp, const fnode *f, int kind)
840d93
 {
840d93
   int size;
840d93
 
840d93
-  if (f->format == FMT_F && f->u.real.w == 0)
840d93
+  if ((f->format == FMT_F && f->u.real.w == 0) || f->u.real.w == DEFAULT_WIDTH)
840d93
     {
840d93
       switch (kind)
840d93
       {
840d93
diff --git a/libgfortran/io/write_float.def b/libgfortran/io/write_float.def
ac4b94
index c63db4e77ef..daa16679f53 100644
840d93
--- a/libgfortran/io/write_float.def
840d93
+++ b/libgfortran/io/write_float.def
ac4b94
@@ -113,7 +113,8 @@ determine_precision (st_parameter_dt * dtp, const fnode * f, int len)
840d93
 static void
840d93
 build_float_string (st_parameter_dt *dtp, const fnode *f, char *buffer,
840d93
 		    size_t size, int nprinted, int precision, int sign_bit,
840d93
-		    bool zero_flag, int npad, char *result, size_t *len)
840d93
+		    bool zero_flag, int npad, int default_width, char *result,
ac4b94
+		    size_t *len)
840d93
 {
840d93
   char *put;
840d93
   char *digits;
ac4b94
@@ -132,8 +133,17 @@ build_float_string (st_parameter_dt *dtp, const fnode *f, char *buffer,
840d93
   sign_t sign;
840d93
 
840d93
   ft = f->format;
840d93
-  w = f->u.real.w;
840d93
-  d = f->u.real.d;
840d93
+  if (f->u.real.w == DEFAULT_WIDTH)
840d93
+    /* This codepath can only be reached with -fdec-format-defaults. */
840d93
+    {
840d93
+      w = default_width;
840d93
+      d = precision;
840d93
+    }
840d93
+  else
840d93
+    {
840d93
+      w = f->u.real.w;
840d93
+      d = f->u.real.d;
840d93
+    }
840d93
   p = dtp->u.p.scale_factor;
840d93
   *len = 0;
840d93
 
ac4b94
@@ -960,6 +970,11 @@ determine_en_precision (st_parameter_dt *dtp, const fnode *f,
840d93
       int save_scale_factor;\
840d93
       volatile GFC_REAL_ ## x temp;\
840d93
       save_scale_factor = dtp->u.p.scale_factor;\
840d93
+      if (w == DEFAULT_WIDTH)\
840d93
+	{\
840d93
+	  w = default_width;\
840d93
+	  d = precision;\
840d93
+	}\
840d93
       switch (dtp->u.p.current_unit->round_status)\
840d93
 	{\
840d93
 	  case ROUND_ZERO:\
ac4b94
@@ -1035,7 +1050,8 @@ determine_en_precision (st_parameter_dt *dtp, const fnode *f,
840d93
 	  nprinted = FDTOA(y,precision,m);\
840d93
 	}\
840d93
       build_float_string (dtp, &newf, buffer, size, nprinted, precision,\
840d93
-				   sign_bit, zero_flag, npad, result, res_len);\
840d93
+				   sign_bit, zero_flag, npad, default_width,\
840d93
+				   result, res_len);\
840d93
       dtp->u.p.scale_factor = save_scale_factor;\
840d93
     }\
840d93
   else\
ac4b94
@@ -1045,7 +1061,8 @@ determine_en_precision (st_parameter_dt *dtp, const fnode *f,
840d93
       else\
840d93
 	nprinted = DTOA(y,precision,m);\
840d93
       build_float_string (dtp, f, buffer, size, nprinted, precision,\
840d93
-				   sign_bit, zero_flag, npad, result, res_len);\
840d93
+				   sign_bit, zero_flag, npad, default_width,\
840d93
+				   result, res_len);\
840d93
     }\
840d93
 }\
840d93
 
ac4b94
@@ -1059,6 +1076,16 @@ get_float_string (st_parameter_dt *dtp, const fnode *f, const char *source,
840d93
 {
840d93
   int sign_bit, nprinted;
840d93
   bool zero_flag;
840d93
+  int default_width = 0;
840d93
+
840d93
+  if (f->u.real.w == DEFAULT_WIDTH)
840d93
+    /* This codepath can only be reached with -fdec-format-defaults. The default
840d93
+     * values are based on those used in the Oracle Fortran compiler.
840d93
+     */
840d93
+    {
840d93
+      default_width = default_width_for_float (kind);
840d93
+      precision = default_precision_for_float (kind);
840d93
+    }
840d93
 
840d93
   switch (kind)
840d93
     {
ac4b94
-- 
ac4b94
2.11.0
ac4b94