Blame SOURCES/gcc8-libgfortran-default-values.patch

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