Blame SOURCES/0022-Default-values-for-certain-field-descriptors-in-form.patch

3db796
diff --git a/gcc/fortran/io.c b/gcc/fortran/io.c
3db796
index d93dcfa..f47565c 100644
3db796
--- a/gcc/fortran/io.c
3db796
+++ b/gcc/fortran/io.c
3db796
@@ -909,6 +909,13 @@ data_desc:
3db796
 
3db796
       if (u != FMT_POSINT)
3db796
 	{
3db796
+	  if (flag_dec)
3db796
+	    {
3db796
+	      /* Assume a default width based on the variable size.  */
3db796
+	      saved_token = u;
3db796
+	      break;
3db796
+	    }
3db796
+
3db796
 	  format_locus.nextc += format_string_pos;
3db796
 	  gfc_error ("Positive width required in format "
3db796
 			 "specifier %s at %L", token_to_string (t),
3db796
@@ -1030,6 +1037,13 @@ data_desc:
3db796
 	goto fail;
3db796
       if (t != FMT_ZERO && t != FMT_POSINT)
3db796
 	{
3db796
+	  if (flag_dec)
3db796
+	    {
3db796
+	      /* Assume the default width is expected here and continue lexing.  */
3db796
+	      value = 0; /* It doesn't matter what we set the value to here.  */
3db796
+	      saved_token = t;
3db796
+	      break;
3db796
+	    }
3db796
 	  error = nonneg_required;
3db796
 	  goto syntax;
3db796
 	}
3db796
@@ -1099,8 +1113,17 @@ data_desc:
3db796
 	goto fail;
3db796
       if (t != FMT_ZERO && t != FMT_POSINT)
3db796
 	{
3db796
-	  error = nonneg_required;
3db796
-	  goto syntax;
3db796
+	  if (flag_dec)
3db796
+	    {
3db796
+	      /* Assume the default width is expected here and continue lexing.  */
3db796
+	      value = 0; /* It doesn't matter what we set the value to here.  */
3db796
+	      saved_token = t;
3db796
+	    }
3db796
+	  else
3db796
+	    {
3db796
+	      error = nonneg_required;
3db796
+	      goto syntax;
3db796
+	    }
3db796
 	}
3db796
       else if (is_input && t == FMT_ZERO)
3db796
 	{
3db796
diff --git a/gcc/testsuite/gfortran.dg/fmt_f_default_field_width.f90 b/gcc/testsuite/gfortran.dg/fmt_f_default_field_width.f90
3db796
new file mode 100644
3db796
index 0000000..b087b8f
3db796
--- /dev/null
3db796
+++ b/gcc/testsuite/gfortran.dg/fmt_f_default_field_width.f90
3db796
@@ -0,0 +1,43 @@
3db796
+! { dg-do run }
3db796
+! { dg-options -fdec }
3db796
+!
3db796
+! Test case for the default field widths enabled by the -fdec-format-defaults flag.
3db796
+!
3db796
+! This feature is not part of any Fortran standard, but it is supported by the
3db796
+! Oracle Fortran compiler and others.
3db796
+!
3db796
+! libgfortran uses printf() internally to implement FORMAT. If you print float
3db796
+! values to a higher precision than the type can actually store, the results
3db796
+! are implementation dependent: some platforms print zeros, others print random
3db796
+! numbers. Don't depend on this behaviour in tests because they will not be
3db796
+! portable.
3db796
+
3db796
+    character(50) :: buffer
3db796
+
3db796
+    real*4 :: real_4
3db796
+    real*8 :: real_8
3db796
+    real*16 :: real_16
3db796
+    integer :: len
3db796
+
3db796
+    real_4 = 4.18
3db796
+    write(buffer, '(A, F, A)') ':',real_4,':'
3db796
+    print *,buffer
3db796
+    if (buffer.ne.":      4.1799998:") call abort
3db796
+
3db796
+    real_4 = 0.00000018
3db796
+    write(buffer, '(A, F, A)') ':',real_4,':'
3db796
+    print *,buffer
3db796
+    if (buffer.ne.":      0.0000002:") call abort
3db796
+
3db796
+    real_8 = 4.18
3db796
+    write(buffer, '(A, F, A)') ':',real_8,':'
3db796
+    print *,buffer
3db796
+    len = len_trim(buffer)
3db796
+    if (len /= 27) call abort
3db796
+
3db796
+    real_16 = 4.18
3db796
+    write(buffer, '(A, F, A)') ':',real_16,':'
3db796
+    print *,buffer
3db796
+    len = len_trim(buffer)
3db796
+    if (len /= 44) call abort
3db796
+end
3db796
diff --git a/gcc/testsuite/gfortran.dg/fmt_g_default_field_width.f90 b/gcc/testsuite/gfortran.dg/fmt_g_default_field_width.f90
3db796
new file mode 100644
3db796
index 0000000..3d3a476
3db796
--- /dev/null
3db796
+++ b/gcc/testsuite/gfortran.dg/fmt_g_default_field_width.f90
3db796
@@ -0,0 +1,48 @@
3db796
+! { dg-do run }
3db796
+! { dg-options -fdec }
3db796
+!
3db796
+! Test case for the default field widths enabled by the -fdec-format-defaults flag.
3db796
+!
3db796
+! This feature is not part of any Fortran standard, but it is supported by the
3db796
+! Oracle Fortran compiler and others.
3db796
+!
3db796
+! libgfortran uses printf() internally to implement FORMAT. If you print float
3db796
+! values to a higher precision than the type can actually store, the results
3db796
+! are implementation dependent: some platforms print zeros, others print random
3db796
+! numbers. Don't depend on this behaviour in tests because they will not be
3db796
+! portable.
3db796
+
3db796
+    character(50) :: buffer
3db796
+
3db796
+    real*4 :: real_4
3db796
+    real*8 :: real_8
3db796
+    real*16 :: real_16
3db796
+    integer :: len
3db796
+
3db796
+    real_4 = 4.18
3db796
+    write(buffer, '(A, G, A)') ':',real_4,':'
3db796
+    print *,buffer
3db796
+    if (buffer.ne.":   4.180000    :") call abort
3db796
+
3db796
+    real_4 = 0.00000018
3db796
+    write(buffer, '(A, G, A)') ':',real_4,':'
3db796
+    print *,buffer
3db796
+    if (buffer.ne.":  0.1800000E-06:") call abort
3db796
+
3db796
+    real_4 = 18000000.4
3db796
+    write(buffer, '(A, G, A)') ':',real_4,':'
3db796
+    print *,buffer
3db796
+    if (buffer.ne.":  0.1800000E+08:") call abort
3db796
+
3db796
+    real_8 = 4.18
3db796
+    write(buffer, '(A, G, A)') ':',real_8,':'
3db796
+    print *,buffer
3db796
+    len = len_trim(buffer)
3db796
+    if (len /= 27) call abort
3db796
+
3db796
+    real_16 = 4.18
3db796
+    write(buffer, '(A, G, A)') ':',real_16,':'
3db796
+    print *,buffer
3db796
+    len = len_trim(buffer)
3db796
+    if (len /= 44) call abort
3db796
+end
3db796
diff --git a/gcc/testsuite/gfortran.dg/fmt_i_default_field_width.f90 b/gcc/testsuite/gfortran.dg/fmt_i_default_field_width.f90
3db796
new file mode 100644
3db796
index 0000000..ac4e165
3db796
--- /dev/null
3db796
+++ b/gcc/testsuite/gfortran.dg/fmt_i_default_field_width.f90
3db796
@@ -0,0 +1,38 @@
3db796
+! { dg-do run }
3db796
+! { dg-options -fdec }
3db796
+!
3db796
+! Test case for the default field widths enabled by the -fdec-format-defaults flag.
3db796
+!
3db796
+! This feature is not part of any Fortran standard, but it is supported by the
3db796
+! Oracle Fortran compiler and others.
3db796
+
3db796
+    character(50) :: buffer
3db796
+    character(1) :: colon
3db796
+
3db796
+    integer*2 :: integer_2
3db796
+    integer*4 :: integer_4
3db796
+    integer*8 :: integer_8
3db796
+
3db796
+    write(buffer, '(A, I, A)') ':',12340,':'
3db796
+    print *,buffer
3db796
+    if (buffer.ne.":       12340:") call abort
3db796
+
3db796
+    read(buffer, '(A1, I, A1)') colon, integer_4, colon
3db796
+    if (integer_4.ne.12340) call abort
3db796
+
3db796
+    integer_2 = -99
3db796
+    write(buffer, '(A, I, A)') ':',integer_2,':'
3db796
+    print *,buffer
3db796
+    if (buffer.ne.":    -99:") call abort
3db796
+
3db796
+    integer_8 = -11112222
3db796
+    write(buffer, '(A, I, A)') ':',integer_8,':'
3db796
+    print *,buffer
3db796
+    if (buffer.ne.":              -11112222:") call abort
3db796
+
3db796
+! If the width is 7 and there are 7 leading zeroes, the result should be zero.
3db796
+    integer_2 = 789
3db796
+    buffer = '0000000789'
3db796
+    read(buffer, '(I)') integer_2
3db796
+    if (integer_2.ne.0) call abort
3db796
+end
3db796
diff --git a/libgfortran/io/format.c b/libgfortran/io/format.c
3db796
index c2abdd7..692b1ff 100644
3db796
--- a/libgfortran/io/format.c
3db796
+++ b/libgfortran/io/format.c
3db796
@@ -956,12 +956,33 @@ parse_format_list (st_parameter_dt *dtp, bool *seen_dd)
3db796
 	  *seen_dd = true;
3db796
 	  if (u != FMT_POSINT && u != FMT_ZERO)
3db796
 	    {
3db796
+	      if (dtp->common.flags & IOPARM_DT_DEC_EXT)
3db796
+		{
3db796
+		  tail->u.real.w = DEFAULT_WIDTH;
3db796
+		  tail->u.real.d = 0;
3db796
+		  tail->u.real.e = -1;
3db796
+		  fmt->saved_token = u;
3db796
+		  break;
3db796
+		}
3db796
 	      fmt->error = nonneg_required;
3db796
 	      goto finished;
3db796
 	    }
3db796
 	}
3db796
+      else if (u == FMT_ZERO)
3db796
+	{
3db796
+	  fmt->error = posint_required;
3db796
+	  goto finished;
3db796
+	}
3db796
       else if (u != FMT_POSINT)
3db796
 	{
3db796
+	  if (dtp->common.flags & IOPARM_DT_DEC_EXT)
3db796
+	    {
3db796
+	      tail->u.real.w = DEFAULT_WIDTH;
3db796
+	      tail->u.real.d = 0;
3db796
+	      tail->u.real.e = -1;
3db796
+	      fmt->saved_token = u;
3db796
+	      break;
3db796
+	    }
3db796
 	  fmt->error = posint_required;
3db796
 	  goto finished;
3db796
 	}
3db796
@@ -1099,6 +1120,13 @@ parse_format_list (st_parameter_dt *dtp, bool *seen_dd)
3db796
 	{
3db796
 	  if (t != FMT_POSINT)
3db796
 	    {
3db796
+	      if (dtp->common.flags & IOPARM_DT_DEC_EXT)
3db796
+		{
3db796
+		  tail->u.integer.w = DEFAULT_WIDTH;
3db796
+		  tail->u.integer.m = -1;
3db796
+		  fmt->saved_token = t;
3db796
+		  break;
3db796
+		}
3db796
 	      fmt->error = posint_required;
3db796
 	      goto finished;
3db796
 	    }
3db796
@@ -1107,6 +1135,13 @@ parse_format_list (st_parameter_dt *dtp, bool *seen_dd)
3db796
 	{
3db796
 	  if (t != FMT_ZERO && t != FMT_POSINT)
3db796
 	    {
3db796
+	      if (dtp->common.flags & IOPARM_DT_DEC_EXT)
3db796
+		{
3db796
+		  tail->u.integer.w = DEFAULT_WIDTH;
3db796
+		  tail->u.integer.m = -1;
3db796
+		  fmt->saved_token = t;
3db796
+		  break;
3db796
+		}
3db796
 	      fmt->error = nonneg_required;
3db796
 	      goto finished;
3db796
 	    }
3db796
diff --git a/libgfortran/io/io.h b/libgfortran/io/io.h
3db796
index 5583183..d1d08e8 100644
3db796
--- a/libgfortran/io/io.h
3db796
+++ b/libgfortran/io/io.h
3db796
@@ -981,5 +981,55 @@ memset4 (gfc_char4_t *p, gfc_char4_t c, int k)
3db796
     *p++ = c;
3db796
 }
3db796
 
3db796
+/* Used in width fields to indicate that the default should be used */
3db796
+#define DEFAULT_WIDTH -1
3db796
+
3db796
+/* Defaults for certain format field descriptors. These are decided based on
3db796
+ * the type of the value being formatted.
3db796
+ *
3db796
+ * The behaviour here is modelled on the Oracle Fortran compiler. At the time
3db796
+ * of writing, the details were available at this URL:
3db796
+ *
3db796
+ *   https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnc3/index.html#z4000743746d
3db796
+ */
3db796
+
3db796
+static inline int
3db796
+default_width_for_integer (int kind)
3db796
+{
3db796
+  switch (kind)
3db796
+    {
3db796
+    case 1:
3db796
+    case 2:  return  7;
3db796
+    case 4:  return 12;
3db796
+    case 8:  return 23;
3db796
+    case 16: return 44;
3db796
+    default: return  0;
3db796
+    }
3db796
+}
3db796
+
3db796
+static inline int
3db796
+default_width_for_float (int kind)
3db796
+{
3db796
+  switch (kind)
3db796
+    {
3db796
+    case 4:  return 15;
3db796
+    case 8:  return 25;
3db796
+    case 16: return 42;
3db796
+    default: return  0;
3db796
+    }
3db796
+}
3db796
+
3db796
+static inline int
3db796
+default_precision_for_float (int kind)
3db796
+{
3db796
+  switch (kind)
3db796
+    {
3db796
+    case 4:  return 7;
3db796
+    case 8:  return 16;
3db796
+    case 16: return 33;
3db796
+    default: return 0;
3db796
+    }
3db796
+}
3db796
+
3db796
 #endif
3db796
 
3db796
diff --git a/libgfortran/io/read.c b/libgfortran/io/read.c
3db796
index 2c9de48..e911e35 100644
3db796
--- a/libgfortran/io/read.c
3db796
+++ b/libgfortran/io/read.c
3db796
@@ -629,6 +629,12 @@ read_decimal (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
3db796
 
3db796
   w = f->u.w;
3db796
 
3db796
+  /* This is a legacy extension, and the frontend will only allow such cases
3db796
+   * through when -fdec-format-defaults is passed.
3db796
+   */
3db796
+  if (w == DEFAULT_WIDTH)
3db796
+    w = default_width_for_integer (length);
3db796
+
3db796
   p = read_block_form (dtp, &w);
3db796
 
3db796
   if (p == NULL)
3db796
diff --git a/libgfortran/io/write.c b/libgfortran/io/write.c
3db796
index a7307a8..c8e52fb 100644
3db796
--- a/libgfortran/io/write.c
3db796
+++ b/libgfortran/io/write.c
3db796
@@ -684,9 +684,8 @@ write_l (st_parameter_dt *dtp, const fnode *f, char *source, int len)
3db796
   p[wlen - 1] = (n) ? 'T' : 'F';
3db796
 }
3db796
 
3db796
-
3db796
 static void
3db796
-write_boz (st_parameter_dt *dtp, const fnode *f, const char *q, int n)
3db796
+write_boz (st_parameter_dt *dtp, const fnode *f, const char *q, int n, int len)
3db796
 {
3db796
   int w, m, digits, nzero, nblank;
3db796
   char *p;
3db796
@@ -719,6 +718,9 @@ write_boz (st_parameter_dt *dtp, const fnode *f, const char *q, int n)
3db796
   /* Select a width if none was specified.  The idea here is to always
3db796
      print something.  */
3db796
 
3db796
+  if (w == DEFAULT_WIDTH)
3db796
+    w = default_width_for_integer (len);
3db796
+
3db796
   if (w == 0)
3db796
     w = ((digits < m) ? m : digits);
3db796
 
3db796
@@ -845,6 +847,8 @@ write_decimal (st_parameter_dt *dtp, const fnode *f, const char *source,
3db796
 
3db796
   /* Select a width if none was specified.  The idea here is to always
3db796
      print something.  */
3db796
+  if (w == DEFAULT_WIDTH)
3db796
+    w = default_width_for_integer (len);
3db796
 
3db796
   if (w == 0)
3db796
     w = ((digits < m) ? m : digits) + nsign;
3db796
@@ -1187,13 +1191,13 @@ write_b (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
3db796
   if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
3db796
     {
3db796
       p = btoa_big (source, itoa_buf, len, &n);
3db796
-      write_boz (dtp, f, p, n);
3db796
+      write_boz (dtp, f, p, n, len);
3db796
     }
3db796
   else
3db796
     {
3db796
       n = extract_uint (source, len);
3db796
       p = btoa (n, itoa_buf, sizeof (itoa_buf));
3db796
-      write_boz (dtp, f, p, n);
3db796
+      write_boz (dtp, f, p, n, len);
3db796
     }
3db796
 }
3db796
 
3db796
@@ -1208,13 +1212,13 @@ write_o (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
3db796
   if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
3db796
     {
3db796
       p = otoa_big (source, itoa_buf, len, &n);
3db796
-      write_boz (dtp, f, p, n);
3db796
+      write_boz (dtp, f, p, n, len);
3db796
     }
3db796
   else
3db796
     {
3db796
       n = extract_uint (source, len);
3db796
       p = otoa (n, itoa_buf, sizeof (itoa_buf));
3db796
-      write_boz (dtp, f, p, n);
3db796
+      write_boz (dtp, f, p, n, len);
3db796
     }
3db796
 }
3db796
 
3db796
@@ -1228,13 +1232,13 @@ write_z (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
3db796
   if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
3db796
     {
3db796
       p = ztoa_big (source, itoa_buf, len, &n);
3db796
-      write_boz (dtp, f, p, n);
3db796
+      write_boz (dtp, f, p, n, len);
3db796
     }
3db796
   else
3db796
     {
3db796
       n = extract_uint (source, len);
3db796
       p = gfc_xtoa (n, itoa_buf, sizeof (itoa_buf));
3db796
-      write_boz (dtp, f, p, n);
3db796
+      write_boz (dtp, f, p, n, len);
3db796
     }
3db796
 }
3db796
 
3db796
@@ -1504,7 +1508,7 @@ size_from_kind (st_parameter_dt *dtp, const fnode *f, int kind)
3db796
 {
3db796
   int size;
3db796
 
3db796
-  if (f->format == FMT_F && f->u.real.w == 0)
3db796
+  if ((f->format == FMT_F && f->u.real.w == 0) || f->u.real.w == DEFAULT_WIDTH)
3db796
     {
3db796
       switch (kind)
3db796
       {
3db796
diff --git a/libgfortran/io/write_float.def b/libgfortran/io/write_float.def
3db796
index 7f0aa1d..73dc910 100644
3db796
--- a/libgfortran/io/write_float.def
3db796
+++ b/libgfortran/io/write_float.def
3db796
@@ -113,7 +113,8 @@ determine_precision (st_parameter_dt * dtp, const fnode * f, int len)
3db796
 static void
3db796
 build_float_string (st_parameter_dt *dtp, const fnode *f, char *buffer,
3db796
 		    size_t size, int nprinted, int precision, int sign_bit,
3db796
-		    bool zero_flag, int npad, char *result, size_t *len)
3db796
+		    bool zero_flag, int npad, int default_width, char *result,
3db796
+                    size_t *len)
3db796
 {
3db796
   char *put;
3db796
   char *digits;
3db796
@@ -132,8 +133,17 @@ build_float_string (st_parameter_dt *dtp, const fnode *f, char *buffer,
3db796
   sign_t sign;
3db796
 
3db796
   ft = f->format;
3db796
-  w = f->u.real.w;
3db796
-  d = f->u.real.d;
3db796
+  if (f->u.real.w == DEFAULT_WIDTH)
3db796
+    /* This codepath can only be reached with -fdec-format-defaults. */
3db796
+    {
3db796
+      w = default_width;
3db796
+      d = precision;
3db796
+    }
3db796
+  else
3db796
+    {
3db796
+      w = f->u.real.w;
3db796
+      d = f->u.real.d;
3db796
+    }
3db796
   p = dtp->u.p.scale_factor;
3db796
 
3db796
   rchar = '5';
3db796
@@ -958,6 +968,11 @@ determine_en_precision (st_parameter_dt *dtp, const fnode *f,
3db796
       int save_scale_factor;\
3db796
       volatile GFC_REAL_ ## x temp;\
3db796
       save_scale_factor = dtp->u.p.scale_factor;\
3db796
+      if (w == DEFAULT_WIDTH)\
3db796
+	{\
3db796
+	  w = default_width;\
3db796
+	  d = precision;\
3db796
+	}\
3db796
       switch (dtp->u.p.current_unit->round_status)\
3db796
 	{\
3db796
 	  case ROUND_ZERO:\
3db796
@@ -1033,7 +1048,8 @@ determine_en_precision (st_parameter_dt *dtp, const fnode *f,
3db796
 	  nprinted = FDTOA(y,precision,m);\
3db796
 	}\
3db796
       build_float_string (dtp, &newf, buffer, size, nprinted, precision,\
3db796
-				   sign_bit, zero_flag, npad, result, res_len);\
3db796
+				   sign_bit, zero_flag, npad, default_width,\
3db796
+				   result, res_len);\
3db796
       dtp->u.p.scale_factor = save_scale_factor;\
3db796
     }\
3db796
   else\
3db796
@@ -1043,7 +1059,8 @@ determine_en_precision (st_parameter_dt *dtp, const fnode *f,
3db796
       else\
3db796
 	nprinted = DTOA(y,precision,m);\
3db796
       build_float_string (dtp, f, buffer, size, nprinted, precision,\
3db796
-				   sign_bit, zero_flag, npad, result, res_len);\
3db796
+				   sign_bit, zero_flag, npad, default_width,\
3db796
+				   result, res_len);\
3db796
     }\
3db796
 }\
3db796
 
3db796
@@ -1057,6 +1074,16 @@ get_float_string (st_parameter_dt *dtp, const fnode *f, const char *source,
3db796
 {
3db796
   int sign_bit, nprinted;
3db796
   bool zero_flag;
3db796
+  int default_width = 0;
3db796
+
3db796
+  if (f->u.real.w == DEFAULT_WIDTH)
3db796
+    /* This codepath can only be reached with -fdec-format-defaults. The default
3db796
+     * values are based on those used in the Oracle Fortran compiler.
3db796
+     */
3db796
+    {
3db796
+      default_width = default_width_for_float (kind);
3db796
+      precision = default_precision_for_float (kind);
3db796
+    }
3db796
 
3db796
   switch (kind)
3db796
     {