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

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