Blame SOURCES/0004-Allow-CHARACTER-literals-in-assignments-and-data-sta.patch

ac4b94
From c1d6c81730ffda61eff8fccf4d0c7efa3ae6fd8d Mon Sep 17 00:00:00 2001
ac4b94
From: Jim MacArthur <jim.macarthur@codethink.co.uk>
ac4b94
Date: Thu, 4 Feb 2016 17:18:30 +0000
ac4b94
Subject: [PATCH 04/16] Allow CHARACTER literals in assignments and data
ac4b94
 statements
ac4b94
ac4b94
Warnings are raised when this happens.
ac4b94
ac4b94
Enable using -fdec-char-as-int or -fdec
ac4b94
---
ac4b94
 gcc/fortran/arith.c                                | 96 +++++++++++++++++++++-
ac4b94
 gcc/fortran/arith.h                                |  4 +
ac4b94
 gcc/fortran/expr.c                                 |  5 ++
ac4b94
 gcc/fortran/intrinsic.c                            | 32 +++++++-
ac4b94
 gcc/fortran/lang.opt                               |  5 ++
ac4b94
 gcc/fortran/options.c                              |  1 +
ac4b94
 gcc/fortran/resolve.c                              | 11 ++-
ac4b94
 gcc/fortran/simplify.c                             | 29 ++++++-
ac4b94
 gcc/fortran/trans-const.c                          |  3 +-
ac4b94
 .../dec_char_conversion_in_assignment_1.f90        | 61 ++++++++++++++
ac4b94
 .../dec_char_conversion_in_assignment_2.f90        | 61 ++++++++++++++
ac4b94
 .../dec_char_conversion_in_assignment_3.f90        | 61 ++++++++++++++
ac4b94
 .../gfortran.dg/dec_char_conversion_in_data_1.f90  | 69 ++++++++++++++++
ac4b94
 .../gfortran.dg/dec_char_conversion_in_data_2.f90  | 69 ++++++++++++++++
ac4b94
 .../gfortran.dg/dec_char_conversion_in_data_3.f90  | 69 ++++++++++++++++
ac4b94
 gcc/testsuite/gfortran.dg/hollerith5.f90           |  5 +-
ac4b94
 gcc/testsuite/gfortran.dg/hollerith_legacy.f90     |  2 +-
ac4b94
 .../gfortran.dg/no_char_to_int_assign.f90          | 20 +++++
ac4b94
 18 files changed, 589 insertions(+), 14 deletions(-)
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_1.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_2.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_3.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_1.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_2.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_3.f90
ac4b94
 create mode 100644 gcc/testsuite/gfortran.dg/no_char_to_int_assign.f90
ac4b94
ac4b94
diff --git a/gcc/fortran/arith.c b/gcc/fortran/arith.c
ac4b94
index f2d311c044c..7e6d6dd3bb8 100644
ac4b94
--- a/gcc/fortran/arith.c
ac4b94
+++ b/gcc/fortran/arith.c
ac4b94
@@ -2553,11 +2553,11 @@ hollerith2representation (gfc_expr *result, gfc_expr *src)
ac4b94
   src_len = src->representation.length - src->ts.u.pad;
ac4b94
   gfc_target_expr_size (result, &result_len);
ac4b94
 
ac4b94
-  if (src_len > result_len)
ac4b94
+  if (src_len > result_len && warn_character_truncation)
ac4b94
     {
ac4b94
-      gfc_warning (0,
ac4b94
-		   "The Hollerith constant at %L is too long to convert to %qs",
ac4b94
-		   &src->where, gfc_typename(&result->ts));
ac4b94
+      gfc_warning (OPT_Wcharacter_truncation, "The Hollerith constant at %L "
ac4b94
+		   "is truncated in conversion to %qs", &src->where,
ac4b94
+		   gfc_typename(&result->ts));
ac4b94
     }
ac4b94
 
ac4b94
   result->representation.string = XCNEWVEC (char, result_len + 1);
ac4b94
@@ -2572,6 +2572,36 @@ hollerith2representation (gfc_expr *result, gfc_expr *src)
ac4b94
 }
ac4b94
 
ac4b94
 
ac4b94
+/* Helper function to set the representation in a character conversion.
ac4b94
+   This assumes that the ts.type and ts.kind of the result have already
ac4b94
+   been set.  */
ac4b94
+
ac4b94
+static void
ac4b94
+character2representation (gfc_expr *result, gfc_expr *src)
ac4b94
+{
ac4b94
+  size_t src_len, result_len;
ac4b94
+  int i;
ac4b94
+  src_len = src->value.character.length;
ac4b94
+  gfc_target_expr_size (result, &result_len);
ac4b94
+
ac4b94
+  if (src_len > result_len && warn_character_truncation)
ac4b94
+    gfc_warning (OPT_Wcharacter_truncation, "The character constant at %L is "
ac4b94
+		 "is truncated in conversion to %s", &src->where,
ac4b94
+		 gfc_typename(&result->ts));
ac4b94
+
ac4b94
+  result->representation.string = XCNEWVEC (char, result_len + 1);
ac4b94
+
ac4b94
+  for (i = 0; i < MIN (result_len, src_len); i++)
ac4b94
+    result->representation.string[i] = (char) src->value.character.string[i];
ac4b94
+
ac4b94
+  if (src_len < result_len)
ac4b94
+    memset (&result->representation.string[src_len], ' ',
ac4b94
+	    result_len - src_len);
ac4b94
+
ac4b94
+  result->representation.string[result_len] = '\0'; /* For debugger  */
ac4b94
+  result->representation.length = result_len;
ac4b94
+}
ac4b94
+
ac4b94
 /* Convert Hollerith to integer. The constant will be padded or truncated.  */
ac4b94
 
ac4b94
 gfc_expr *
ac4b94
@@ -2587,6 +2617,19 @@ gfc_hollerith2int (gfc_expr *src, int kind)
ac4b94
   return result;
ac4b94
 }
ac4b94
 
ac4b94
+/* Convert character to integer. The constant will be padded or truncated. */
ac4b94
+
ac4b94
+gfc_expr *
ac4b94
+gfc_character2int (gfc_expr *src, int kind)
ac4b94
+{
ac4b94
+  gfc_expr *result;
ac4b94
+  result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
ac4b94
+
ac4b94
+  character2representation (result, src);
ac4b94
+  gfc_interpret_integer (kind, (unsigned char *) result->representation.string,
ac4b94
+			 result->representation.length, result->value.integer);
ac4b94
+  return result;
ac4b94
+}
ac4b94
 
ac4b94
 /* Convert Hollerith to real. The constant will be padded or truncated.  */
ac4b94
 
ac4b94
@@ -2603,6 +2646,21 @@ gfc_hollerith2real (gfc_expr *src, int kind)
ac4b94
   return result;
ac4b94
 }
ac4b94
 
ac4b94
+/* Convert character to real. The constant will be padded or truncated.  */
ac4b94
+
ac4b94
+gfc_expr *
ac4b94
+gfc_character2real (gfc_expr *src, int kind)
ac4b94
+{
ac4b94
+  gfc_expr *result;
ac4b94
+  result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
ac4b94
+
ac4b94
+  character2representation (result, src);
ac4b94
+  gfc_interpret_float (kind, (unsigned char *) result->representation.string,
ac4b94
+		       result->representation.length, result->value.real);
ac4b94
+
ac4b94
+  return result;
ac4b94
+}
ac4b94
+
ac4b94
 
ac4b94
 /* Convert Hollerith to complex. The constant will be padded or truncated.  */
ac4b94
 
ac4b94
@@ -2619,6 +2677,21 @@ gfc_hollerith2complex (gfc_expr *src, int kind)
ac4b94
   return result;
ac4b94
 }
ac4b94
 
ac4b94
+/* Convert character to complex. The constant will be padded or truncated.  */
ac4b94
+
ac4b94
+gfc_expr *
ac4b94
+gfc_character2complex (gfc_expr *src, int kind)
ac4b94
+{
ac4b94
+  gfc_expr *result;
ac4b94
+  result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
ac4b94
+
ac4b94
+  character2representation (result, src);
ac4b94
+  gfc_interpret_complex (kind, (unsigned char *) result->representation.string,
ac4b94
+			 result->representation.length, result->value.complex);
ac4b94
+
ac4b94
+  return result;
ac4b94
+}
ac4b94
+
ac4b94
 
ac4b94
 /* Convert Hollerith to character.  */
ac4b94
 
ac4b94
@@ -2654,3 +2727,18 @@ gfc_hollerith2logical (gfc_expr *src, int kind)
ac4b94
 
ac4b94
   return result;
ac4b94
 }
ac4b94
+
ac4b94
+/* Convert character to logical. The constant will be padded or truncated.  */
ac4b94
+
ac4b94
+gfc_expr *
ac4b94
+gfc_character2logical (gfc_expr *src, int kind)
ac4b94
+{
ac4b94
+  gfc_expr *result;
ac4b94
+  result = gfc_get_constant_expr (BT_LOGICAL, kind, &src->where);
ac4b94
+
ac4b94
+  character2representation (result, src);
ac4b94
+  gfc_interpret_logical (kind, (unsigned char *) result->representation.string,
ac4b94
+			 result->representation.length, &result->value.logical);
ac4b94
+
ac4b94
+  return result;
ac4b94
+}
ac4b94
diff --git a/gcc/fortran/arith.h b/gcc/fortran/arith.h
ac4b94
index e06c7059885..13ffd8d0b6c 100644
ac4b94
--- a/gcc/fortran/arith.h
ac4b94
+++ b/gcc/fortran/arith.h
ac4b94
@@ -82,7 +82,11 @@ gfc_expr *gfc_hollerith2real (gfc_expr *, int);
ac4b94
 gfc_expr *gfc_hollerith2complex (gfc_expr *, int);
ac4b94
 gfc_expr *gfc_hollerith2character (gfc_expr *, int);
ac4b94
 gfc_expr *gfc_hollerith2logical (gfc_expr *, int);
ac4b94
+gfc_expr *gfc_character2int (gfc_expr *, int);
ac4b94
+gfc_expr *gfc_character2real (gfc_expr *, int);
ac4b94
+gfc_expr *gfc_character2complex (gfc_expr *, int);
ac4b94
 gfc_expr *gfc_character2character (gfc_expr *, int);
ac4b94
+gfc_expr *gfc_character2logical (gfc_expr *, int);
ac4b94
 
ac4b94
 #endif /* GFC_ARITH_H  */
ac4b94
 
ac4b94
diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c
ac4b94
index 474e9ecc401..77600a5f2e8 100644
ac4b94
--- a/gcc/fortran/expr.c
ac4b94
+++ b/gcc/fortran/expr.c
ac4b94
@@ -3695,6 +3695,11 @@ gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform,
ac4b94
 	  || rvalue->ts.type == BT_HOLLERITH)
ac4b94
 	return true;
ac4b94
 
ac4b94
+      if (flag_dec_char_conversions && (gfc_numeric_ts (&lvalue->ts)
ac4b94
+	  || lvalue->ts.type == BT_LOGICAL)     
ac4b94
+          && rvalue->ts.type == BT_CHARACTER)
ac4b94
+	return true;
ac4b94
+
ac4b94
       if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
ac4b94
 	return true;
ac4b94
 
ac4b94
diff --git a/gcc/fortran/intrinsic.c b/gcc/fortran/intrinsic.c
ac4b94
index c21fbddd5fb..e94d5d3225f 100644
ac4b94
--- a/gcc/fortran/intrinsic.c
ac4b94
+++ b/gcc/fortran/intrinsic.c
ac4b94
@@ -4017,6 +4017,28 @@ add_conversions (void)
ac4b94
 	  add_conv (BT_LOGICAL, gfc_logical_kinds[j].kind,
ac4b94
 		    BT_INTEGER, gfc_integer_kinds[i].kind, GFC_STD_LEGACY);
ac4b94
 	}
ac4b94
+
ac4b94
+  /* Flang allows character conversions similar to Hollerith conversions
ac4b94
+     - the first characters will be turned into ascii values. */
ac4b94
+  if (flag_dec_char_conversions)
ac4b94
+    {
ac4b94
+      /* Character-Integer conversions.  */
ac4b94
+      for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
ac4b94
+	add_conv (BT_CHARACTER, gfc_default_character_kind,
ac4b94
+		  BT_INTEGER, gfc_integer_kinds[i].kind, GFC_STD_LEGACY);
ac4b94
+      /* Character-Real conversions.  */
ac4b94
+      for (i = 0; gfc_real_kinds[i].kind != 0; i++)
ac4b94
+	add_conv (BT_CHARACTER, gfc_default_character_kind,
ac4b94
+		  BT_REAL, gfc_real_kinds[i].kind, GFC_STD_LEGACY);
ac4b94
+      /* Character-Complex conversions.  */
ac4b94
+      for (i = 0; gfc_real_kinds[i].kind != 0; i++)
ac4b94
+	add_conv (BT_CHARACTER, gfc_default_character_kind,
ac4b94
+		  BT_COMPLEX, gfc_real_kinds[i].kind, GFC_STD_LEGACY);
ac4b94
+      /* Character-Logical conversions.  */
ac4b94
+      for (i = 0; gfc_logical_kinds[i].kind != 0; i++)
ac4b94
+	add_conv (BT_CHARACTER, gfc_default_character_kind,
ac4b94
+		  BT_LOGICAL, gfc_logical_kinds[i].kind, GFC_STD_LEGACY);
ac4b94
+    }
ac4b94
 }
ac4b94
 
ac4b94
 
ac4b94
@@ -5128,8 +5150,16 @@ gfc_convert_type_warn (gfc_expr *expr, gfc_typespec *ts, int eflag, int wflag)
ac4b94
 			     gfc_typename (&from_ts), gfc_typename (ts),
ac4b94
 			     &expr->where);
ac4b94
 	}
ac4b94
+      else if (flag_dec_char_conversions && from_ts.type == BT_CHARACTER
ac4b94
+	       && (gfc_numeric_ts (ts) || ts->type == BT_LOGICAL))
ac4b94
+	{
ac4b94
+	  if (warn_conversion)
ac4b94
+	    gfc_warning_now (OPT_Wconversion, "Conversion from %s to %s at %L",
ac4b94
+			     gfc_typename (&from_ts), gfc_typename (ts),
ac4b94
+			     &expr->where);
ac4b94
+	}
ac4b94
       else
ac4b94
-        gcc_unreachable ();
ac4b94
+	gcc_unreachable ();
ac4b94
     }
ac4b94
 
ac4b94
   /* Insert a pre-resolved function call to the right function.  */
ac4b94
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
ac4b94
index 13a8e9778bb..5746b99b1d4 100644
ac4b94
--- a/gcc/fortran/lang.opt
ac4b94
+++ b/gcc/fortran/lang.opt
ac4b94
@@ -444,6 +444,11 @@ fdec-duplicates
ac4b94
 Fortran Var(flag_dec_duplicates)
ac4b94
 Allow varibles to be duplicated in the type specification matches.
ac4b94
 
ac4b94
+fdec-char-conversions
ac4b94
+Fortran Var(flag_dec_char_conversions)
ac4b94
+Enable the use of character literals in assignments and data statements
ac4b94
+for non-character variables.
ac4b94
+
ac4b94
 fdec-include
ac4b94
 Fortran Var(flag_dec_include)
ac4b94
 Enable legacy parsing of INCLUDE as statement.
ac4b94
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
ac4b94
index f93db8b6d7c..e97b1568810 100644
ac4b94
--- a/gcc/fortran/options.c
ac4b94
+++ b/gcc/fortran/options.c
ac4b94
@@ -76,6 +76,7 @@ set_dec_flags (int value)
ac4b94
   SET_BITFLAG (flag_dec_include, value, value);
ac4b94
   SET_BITFLAG (flag_dec_format_defaults, value, value);
ac4b94
   SET_BITFLAG (flag_dec_duplicates, value, value);
ac4b94
+  SET_BITFLAG (flag_dec_char_conversions, value, value);
ac4b94
 }
ac4b94
 
ac4b94
 /* Finalize DEC flags.  */
ac4b94
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
ac4b94
index 32b8d504ff6..43559185481 100644
ac4b94
--- a/gcc/fortran/resolve.c
ac4b94
+++ b/gcc/fortran/resolve.c
ac4b94
@@ -4320,7 +4320,6 @@ bad_op:
ac4b94
   return false;
ac4b94
 }
ac4b94
 
ac4b94
-
ac4b94
 /************** Array resolution subroutines **************/
ac4b94
 
ac4b94
 enum compare_result
ac4b94
@@ -10498,6 +10497,16 @@ resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
ac4b94
   lhs = code->expr1;
ac4b94
   rhs = code->expr2;
ac4b94
 
ac4b94
+  if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
ac4b94
+      && rhs->ts.type == BT_CHARACTER
ac4b94
+      && rhs->expr_type != EXPR_CONSTANT)
ac4b94
+    {
ac4b94
+      gfc_error ("Cannot convert CHARACTER into %s at %L",
ac4b94
+                 gfc_typename (&lhs->ts),     
ac4b94
+                 &rhs->where);
ac4b94
+      return false;
ac4b94
+    }
ac4b94
+
ac4b94
   if (rhs->is_boz
ac4b94
       && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
ac4b94
 			  "a DATA statement and outside INT/REAL/DBLE/CMPLX",
ac4b94
diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
ac4b94
index 6c1f4bd4fce..7d7e3f22f73 100644
ac4b94
--- a/gcc/fortran/simplify.c
ac4b94
+++ b/gcc/fortran/simplify.c
ac4b94
@@ -8457,10 +8457,31 @@ gfc_convert_constant (gfc_expr *e, bt type, int kind)
ac4b94
       break;
ac4b94
 
ac4b94
     case BT_CHARACTER:
ac4b94
-      if (type == BT_CHARACTER)
ac4b94
-	f = gfc_character2character;
ac4b94
-      else
ac4b94
-	goto oops;
ac4b94
+      switch (type)
ac4b94
+	{
ac4b94
+	case BT_INTEGER:
ac4b94
+	  f = gfc_character2int;
ac4b94
+	  break;
ac4b94
+
ac4b94
+	case BT_REAL:
ac4b94
+	  f = gfc_character2real;
ac4b94
+	  break;
ac4b94
+
ac4b94
+	case BT_COMPLEX:
ac4b94
+	  f = gfc_character2complex;
ac4b94
+	  break;
ac4b94
+
ac4b94
+	case BT_CHARACTER:
ac4b94
+	  f = gfc_character2character;
ac4b94
+	  break;
ac4b94
+
ac4b94
+	case BT_LOGICAL:
ac4b94
+	  f = gfc_character2logical;
ac4b94
+	  break;
ac4b94
+
ac4b94
+	default:
ac4b94
+	  goto oops;
ac4b94
+	}
ac4b94
       break;
ac4b94
 
ac4b94
     default:
ac4b94
diff --git a/gcc/fortran/trans-const.c b/gcc/fortran/trans-const.c
ac4b94
index 432d12bf168..b155e35cbdd 100644
ac4b94
--- a/gcc/fortran/trans-const.c
ac4b94
+++ b/gcc/fortran/trans-const.c
ac4b94
@@ -25,6 +25,7 @@ along with GCC; see the file COPYING3.  If not see
ac4b94
 #include "coretypes.h"
ac4b94
 #include "tree.h"
ac4b94
 #include "gfortran.h"
ac4b94
+#include "options.h"
ac4b94
 #include "trans.h"
ac4b94
 #include "fold-const.h"
ac4b94
 #include "stor-layout.h"
ac4b94
@@ -330,7 +331,7 @@ gfc_conv_constant_to_tree (gfc_expr * expr)
ac4b94
 			gfc_get_int_type (expr->ts.kind),
ac4b94
 			gfc_build_string_const (expr->representation.length,
ac4b94
 						expr->representation.string));
ac4b94
-	  if (!integer_zerop (tmp) && !integer_onep (tmp))
ac4b94
+	  if (!integer_zerop (tmp) && !integer_onep (tmp) && warn_surprising)
ac4b94
 	    gfc_warning (0, "Assigning value other than 0 or 1 to LOGICAL"
ac4b94
 			 " has undefined result at %L", &expr->where);
ac4b94
 	  return fold_convert (gfc_get_logical_type (expr->ts.kind), tmp);
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_1.f90 b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_1.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..d504f92fbbc
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_1.f90
ac4b94
@@ -0,0 +1,61 @@
ac4b94
+! { dg-do run }
ac4b94
+! { dg-options "-fdec -Wsurprising -Wcharacter-truncation" }
ac4b94
+!
ac4b94
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
ac4b94
+!
ac4b94
+program test
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  a = '1234'
ac4b94
+  b = '1234'
ac4b94
+  c = '12341234'
ac4b94
+  d = '1234'     ! { dg-warning "undefined result" }
ac4b94
+  e = 4h1234
ac4b94
+  f = 4h1234
ac4b94
+  g = 8h12341234
ac4b94
+  h = 4h1234     ! { dg-warning "undefined result" }
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 1
ac4b94
+  if (b.ne.f) stop 2
ac4b94
+  if (c.ne.g) stop 3
ac4b94
+  if (d.neqv.h) stop 4
ac4b94
+
ac4b94
+  ! padded values
ac4b94
+  a = '12'
ac4b94
+  b = '12'
ac4b94
+  c = '12234'
ac4b94
+  d = '124'   ! { dg-warning "undefined result" }
ac4b94
+  e = 2h12
ac4b94
+  f = 2h12
ac4b94
+  g = 5h12234
ac4b94
+  h = 3h123   ! { dg-warning "undefined result" }
ac4b94
+
ac4b94
+  if (a.ne.e) stop 5
ac4b94
+  if (b.ne.f) stop 6
ac4b94
+  if (c.ne.g) stop 7
ac4b94
+  if (d.neqv.h) stop 8
ac4b94
+
ac4b94
+  ! truncated values
ac4b94
+  a = '123478'       ! { dg-warning "truncated in" }
ac4b94
+  b = '123478'       ! { dg-warning "truncated in" }
ac4b94
+  c = '12341234987'  ! { dg-warning "truncated in" }
ac4b94
+  d = '1234abc'      ! { dg-warning "truncated in|undefined result" }
ac4b94
+  e = 6h123478       ! { dg-warning "truncated in" }
ac4b94
+  f = 6h123478       ! { dg-warning "truncated in" }
ac4b94
+  g = 11h12341234987 ! { dg-warning "truncated in" }
ac4b94
+  h = 7h1234abc      ! { dg-warning "truncated in|undefined result" }
ac4b94
+
ac4b94
+  if (a.ne.e) stop 5
ac4b94
+  if (b.ne.f) stop 6
ac4b94
+  if (c.ne.g) stop 7
ac4b94
+  if (d.neqv.h) stop 8
ac4b94
+
ac4b94
+end program
ac4b94
+
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_2.f90 b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_2.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..737ddc664de
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_2.f90
ac4b94
@@ -0,0 +1,61 @@
ac4b94
+! { dg-do run }
ac4b94
+! { dg-options "-fdec-char-conversions -std=legacy -Wcharacter-truncation -Wsurprising" }
ac4b94
+!
ac4b94
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
ac4b94
+!
ac4b94
+program test
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  a = '1234'
ac4b94
+  b = '1234'
ac4b94
+  c = '12341234'
ac4b94
+  d = '1234'     ! { dg-warning "undefined result" }
ac4b94
+  e = 4h1234
ac4b94
+  f = 4h1234
ac4b94
+  g = 8h12341234
ac4b94
+  h = 4h1234     ! { dg-warning "undefined result" }
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 1
ac4b94
+  if (b.ne.f) stop 2
ac4b94
+  if (c.ne.g) stop 3
ac4b94
+  if (d.neqv.h) stop 4
ac4b94
+
ac4b94
+  ! padded values
ac4b94
+  a = '12'
ac4b94
+  b = '12'
ac4b94
+  c = '12234'
ac4b94
+  d = '124'   ! { dg-warning "undefined result" }
ac4b94
+  e = 2h12
ac4b94
+  f = 2h12
ac4b94
+  g = 5h12234
ac4b94
+  h = 3h123   ! { dg-warning "undefined result" }
ac4b94
+
ac4b94
+  if (a.ne.e) stop 5
ac4b94
+  if (b.ne.f) stop 6
ac4b94
+  if (c.ne.g) stop 7
ac4b94
+  if (d.neqv.h) stop 8
ac4b94
+
ac4b94
+  ! truncated values
ac4b94
+  a = '123478'       ! { dg-warning "truncated in" }
ac4b94
+  b = '123478'       ! { dg-warning "truncated in" }
ac4b94
+  c = '12341234987'  ! { dg-warning "truncated in" }
ac4b94
+  d = '1234abc'      ! { dg-warning "truncated in|undefined result" }
ac4b94
+  e = 6h123478       ! { dg-warning "truncated in" }
ac4b94
+  f = 6h123478       ! { dg-warning "truncated in" }
ac4b94
+  g = 11h12341234987 ! { dg-warning "truncated in" }
ac4b94
+  h = 7h1234abc      ! { dg-warning "truncated in|undefined result" }
ac4b94
+
ac4b94
+  if (a.ne.e) stop 5
ac4b94
+  if (b.ne.f) stop 6
ac4b94
+  if (c.ne.g) stop 7
ac4b94
+  if (d.neqv.h) stop 8
ac4b94
+
ac4b94
+end program
ac4b94
+
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_3.f90 b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_3.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..0ec494c4a92
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_assignment_3.f90
ac4b94
@@ -0,0 +1,61 @@
ac4b94
+! { dg-do compile }
ac4b94
+! { dg-options "-fdec -fno-dec-char-conversions" }
ac4b94
+!
ac4b94
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
ac4b94
+!
ac4b94
+program test
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  a = '1234'     ! { dg-error "Cannot convert" }
ac4b94
+  b = '1234'     ! { dg-error "Cannot convert" }
ac4b94
+  c = '12341234' ! { dg-error "Cannot convert" }
ac4b94
+  d = '1234'     ! { dg-error "Cannot convert" }
ac4b94
+  e = 4h1234
ac4b94
+  f = 4h1234
ac4b94
+  g = 8h12341234
ac4b94
+  h = 4h1234
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 1
ac4b94
+  if (b.ne.f) stop 2
ac4b94
+  if (c.ne.g) stop 3
ac4b94
+  if (d.neqv.h) stop 4
ac4b94
+
ac4b94
+  ! padded values
ac4b94
+  a = '12'    ! { dg-error "Cannot convert" }
ac4b94
+  b = '12'    ! { dg-error "Cannot convert" }
ac4b94
+  c = '12234' ! { dg-error "Cannot convert" }
ac4b94
+  d = '124'   ! { dg-error "Cannot convert" }
ac4b94
+  e = 2h12
ac4b94
+  f = 2h12
ac4b94
+  g = 5h12234
ac4b94
+  h = 3h123
ac4b94
+
ac4b94
+  if (a.ne.e) stop 5
ac4b94
+  if (b.ne.f) stop 6
ac4b94
+  if (c.ne.g) stop 7
ac4b94
+  if (d.neqv.h) stop 8
ac4b94
+
ac4b94
+  ! truncated values
ac4b94
+  a = '123478'       ! { dg-error "Cannot convert" }
ac4b94
+  b = '123478'       ! { dg-error "Cannot convert" }
ac4b94
+  c = '12341234987'  ! { dg-error "Cannot convert" }
ac4b94
+  d = '1234abc'      ! { dg-error "Cannot convert" }
ac4b94
+  e = 6h123478       !
ac4b94
+  f = 6h123478       !
ac4b94
+  g = 11h12341234987 !
ac4b94
+  h = 7h1234abc      !
ac4b94
+
ac4b94
+  if (a.ne.e) stop 5
ac4b94
+  if (b.ne.f) stop 6
ac4b94
+  if (c.ne.g) stop 7
ac4b94
+  if (d.neqv.h) stop 8
ac4b94
+
ac4b94
+end program
ac4b94
+
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_1.f90 b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_1.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..c493be9314b
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_1.f90
ac4b94
@@ -0,0 +1,69 @@
ac4b94
+! { dg-do run }
ac4b94
+! { dg-options "-fdec -Wsurprising" }
ac4b94
+!
ac4b94
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
ac4b94
+!
ac4b94
+
ac4b94
+subroutine normal
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  data a, b, c, d / '1234', '1234', '12341234', '1234' / ! { dg-warning "undefined result" }
ac4b94
+  data e, f, g, h / 4h1234, 4h1234, 8h12341234, 4h1234 / ! { dg-warning "undefined result" }
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 1
ac4b94
+  if (b.ne.f) stop 2
ac4b94
+  if (c.ne.g) stop 3
ac4b94
+  if (d.neqv.h) stop 4
ac4b94
+end subroutine
ac4b94
+
ac4b94
+subroutine padded
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  data a, b, c, d / '12', '12', '12334', '123' / ! { dg-warning "undefined result" }
ac4b94
+  data e, f, g, h / 2h12, 2h12, 5h12334, 3h123 / ! { dg-warning "undefined result" }
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 5
ac4b94
+  if (b.ne.f) stop 6
ac4b94
+  if (c.ne.g) stop 7
ac4b94
+  if (d.neqv.h) stop 8
ac4b94
+end subroutine
ac4b94
+
ac4b94
+subroutine truncated
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  data a, b, c, d / '123478', '123478', '1234123498', '12345' /  ! { dg-warning "too long|undefined result" }
ac4b94
+  data e, f, g, h / 6h123478, 6h123478, 10h1234123498, 5h12345 / ! { dg-warning "too long|undefined result" }
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 9
ac4b94
+  if (b.ne.f) stop 10
ac4b94
+  if (c.ne.g) stop 11
ac4b94
+  if (d.neqv.h) stop 12
ac4b94
+end subroutine
ac4b94
+
ac4b94
+program test
ac4b94
+  call normal
ac4b94
+  call padded
ac4b94
+  call truncated
ac4b94
+end program
ac4b94
+
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_2.f90 b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_2.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..c7d8e241cec
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_2.f90
ac4b94
@@ -0,0 +1,69 @@
ac4b94
+! { dg-do run }
ac4b94
+! { dg-options "-fdec-char-conversions -std=legacy -Wsurprising" }
ac4b94
+!
ac4b94
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
ac4b94
+!
ac4b94
+
ac4b94
+subroutine normal
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  data a, b, c, d / '1234', '1234', '12341234', '1234' / ! { dg-warning "undefined result" }
ac4b94
+  data e, f, g, h / 4h1234, 4h1234, 8h12341234, 4h1234 / ! { dg-warning "undefined result" }
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 1
ac4b94
+  if (b.ne.f) stop 2
ac4b94
+  if (c.ne.g) stop 3
ac4b94
+  if (d.neqv.h) stop 4
ac4b94
+end subroutine
ac4b94
+
ac4b94
+subroutine padded
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  data a, b, c, d / '12', '12', '12334', '123' / ! { dg-warning "undefined result" }
ac4b94
+  data e, f, g, h / 2h12, 2h12, 5h12334, 3h123 / ! { dg-warning "undefined result" }
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 5
ac4b94
+  if (b.ne.f) stop 6
ac4b94
+  if (c.ne.g) stop 7
ac4b94
+  if (d.neqv.h) stop 8
ac4b94
+end subroutine
ac4b94
+
ac4b94
+subroutine truncated
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  data a, b, c, d / '123478', '123478', '1234123498', '12345' /  ! { dg-warning "too long|undefined result" }
ac4b94
+  data e, f, g, h / 6h123478, 6h123478, 10h1234123498, 5h12345 / ! { dg-warning "too long|undefined result" }
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 9
ac4b94
+  if (b.ne.f) stop 10
ac4b94
+  if (c.ne.g) stop 11
ac4b94
+  if (d.neqv.h) stop 12
ac4b94
+end subroutine
ac4b94
+
ac4b94
+program test
ac4b94
+  call normal
ac4b94
+  call padded
ac4b94
+  call truncated
ac4b94
+end program
ac4b94
+
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_3.f90 b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_3.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..e7d084b5ffc
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/dec_char_conversion_in_data_3.f90
ac4b94
@@ -0,0 +1,69 @@
ac4b94
+! { dg-do compile }
ac4b94
+! { dg-options "-fdec -fno-dec-char-conversions" }
ac4b94
+!
ac4b94
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
ac4b94
+!
ac4b94
+
ac4b94
+subroutine normal
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  data a, b, c, d / '1234', '1234', '12341234', '1234' / ! { dg-error "Incompatible types" }
ac4b94
+  data e, f, g, h / 4h1234, 4h1234, 8h12341234, 4h1234 /
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 1
ac4b94
+  if (b.ne.f) stop 2
ac4b94
+  if (c.ne.g) stop 3
ac4b94
+  if (d.neqv.h) stop 4
ac4b94
+end subroutine
ac4b94
+
ac4b94
+subroutine padded
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  data a, b, c, d / '12', '12', '12334', '123' / ! { dg-error "Incompatible types" }
ac4b94
+  data e, f, g, h / 2h12, 2h12, 5h12334, 3h123 /
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 5
ac4b94
+  if (b.ne.f) stop 6
ac4b94
+  if (c.ne.g) stop 7
ac4b94
+  if (d.neqv.h) stop 8
ac4b94
+end subroutine
ac4b94
+
ac4b94
+subroutine truncated
ac4b94
+  integer(4) :: a
ac4b94
+  real(4) :: b
ac4b94
+  complex(4) :: c
ac4b94
+  logical(4) :: d
ac4b94
+  integer(4) :: e
ac4b94
+  real(4) :: f
ac4b94
+  complex(4) :: g
ac4b94
+  logical(4) :: h
ac4b94
+
ac4b94
+  data a, b, c, d / '123478', '123478', '1234123498', '12345' /  ! { dg-error "Incompatible types" }
ac4b94
+  data e, f, g, h / 6h123478, 6h123478, 10h1234123498, 5h12345 /
ac4b94
+  
ac4b94
+  if (a.ne.e) stop 9
ac4b94
+  if (b.ne.f) stop 10
ac4b94
+  if (c.ne.g) stop 11
ac4b94
+  if (d.neqv.h) stop 12
ac4b94
+end subroutine
ac4b94
+
ac4b94
+program test
ac4b94
+  call normal
ac4b94
+  call padded
ac4b94
+  call truncated
ac4b94
+end program
ac4b94
+
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/hollerith5.f90 b/gcc/testsuite/gfortran.dg/hollerith5.f90
ac4b94
index ebd0a117c4f..d17f9ae40cf 100644
ac4b94
--- a/gcc/testsuite/gfortran.dg/hollerith5.f90
ac4b94
+++ b/gcc/testsuite/gfortran.dg/hollerith5.f90
ac4b94
@@ -1,8 +1,9 @@
ac4b94
        ! { dg-do compile }
ac4b94
+       ! { dg-options "-Wsurprising" }
ac4b94
        implicit none
ac4b94
        logical b
ac4b94
        b = 4Habcd ! { dg-warning "has undefined result" }
ac4b94
        end
ac4b94
 
ac4b94
-! { dg-warning "Hollerith constant" "const" { target *-*-* } 4 }
ac4b94
-! { dg-warning "Conversion" "conversion" { target *-*-* } 4 }
ac4b94
+! { dg-warning "Hollerith constant" "const" { target *-*-* } 5 }
ac4b94
+! { dg-warning "Conversion" "conversion" { target *-*-* } 5 }
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/hollerith_legacy.f90 b/gcc/testsuite/gfortran.dg/hollerith_legacy.f90
ac4b94
index c3322498345..9d7e989b552 100644
ac4b94
--- a/gcc/testsuite/gfortran.dg/hollerith_legacy.f90
ac4b94
+++ b/gcc/testsuite/gfortran.dg/hollerith_legacy.f90
ac4b94
@@ -1,5 +1,5 @@
ac4b94
 ! { dg-do compile }
ac4b94
-! { dg-options "-std=legacy" }
ac4b94
+! { dg-options "-std=legacy -Wsurprising" }
ac4b94
 ! PR15966, PR18781 & PR16531
ac4b94
 implicit none
ac4b94
 complex(kind=8) x(2) 
ac4b94
diff --git a/gcc/testsuite/gfortran.dg/no_char_to_int_assign.f90 b/gcc/testsuite/gfortran.dg/no_char_to_int_assign.f90
ac4b94
new file mode 100644
ac4b94
index 00000000000..ccfcc9ae512
ac4b94
--- /dev/null
ac4b94
+++ b/gcc/testsuite/gfortran.dg/no_char_to_int_assign.f90
ac4b94
@@ -0,0 +1,20 @@
ac4b94
+! { dg-do compile }
ac4b94
+! { dg-options "-fdec-char-conversions" }
ac4b94
+!
ac4b94
+! Test character to int conversion in DATA types
ac4b94
+!
ac4b94
+! Test case contributed by Mark Eggleston <mark.eggleston@codethink.com>
ac4b94
+!
ac4b94
+program test
ac4b94
+  integer a
ac4b94
+  real b
ac4b94
+  complex c
ac4b94
+  logical d
ac4b94
+  character e
ac4b94
+
ac4b94
+  e = "A"
ac4b94
+  a = e ! { dg-error "Cannot convert" }
ac4b94
+  b = e ! { dg-error "Cannot convert" }
ac4b94
+  c = e ! { dg-error "Cannot convert" }
ac4b94
+  d = e ! { dg-error "Cannot convert" }
ac4b94
+end program
ac4b94
-- 
ac4b94
2.11.0
ac4b94