Blame SOURCES/0002-Pad-character-to-int-conversions-with-spaces-instead.patch

840d93
From 40d6590b03a9f92c19b7097b1cae296276d6ce22 Mon Sep 17 00:00:00 2001
840d93
From: Jim MacArthur <jim.macarthur@codethink.co.uk>
840d93
Date: Mon, 28 Sep 2015 16:06:30 +0100
840d93
Subject: [PATCH 02/23] Pad character-to-int conversions with spaces instead of
840d93
 zeros.
840d93
840d93
The pad character is 'undefined' or 'processor dependent' depending on which
840d93
standard you read. This makes it 0x20 which matches the Oracle Fortran
840d93
compiler. One of the tests tests this undefined behaviour, so I had to modify
840d93
it.
840d93
840d93
    0002-Pad-character-to-int-conversions-with-spaces-instead.patch
840d93
840d93
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
840d93
index 4808c27..93908f8 100644
840d93
--- a/gcc/fortran/lang.opt
840d93
+++ b/gcc/fortran/lang.opt
840d93
@@ -428,6 +428,10 @@ fdec
840d93
 Fortran Var(flag_dec)
840d93
 Enable all DEC language extensions.
840d93
 
840d93
+fdec-pad-with-spaces
840d93
+Fortran Var(flag_dec_pad_with_spaces)
840d93
+For character to integer conversions, use spaces for the pad rather than NUL.
840d93
+
840d93
 fdec-intrinsic-ints
840d93
 Fortran Var(flag_dec_intrinsic_ints)
840d93
 Enable kind-specific variants of integer intrinsic functions.
840d93
diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
840d93
index d12ae5f..09da1d2 100644
840d93
--- a/gcc/fortran/simplify.c
840d93
+++ b/gcc/fortran/simplify.c
840d93
@@ -6623,7 +6623,7 @@ gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
840d93
   /* Allocate the buffer to store the binary version of the source.  */
840d93
   buffer_size = MAX (source_size, result_size);
840d93
   buffer = (unsigned char*)alloca (buffer_size);
840d93
-  memset (buffer, 0, buffer_size);
840d93
+  memset (buffer, (flag_dec_pad_with_spaces ? 0x20 : 0x0), buffer_size);
840d93
 
840d93
   /* Now write source to the buffer.  */
840d93
   gfc_target_encode_expr (source, buffer, buffer_size);
840d93
--- /dev/null
840d93
+++ b/gcc/testsuite/gfortran.dg/c_ptr_tests_20.f90
840d93
@@ -0,0 +1,62 @@
840d93
+! { dg-do compile }
840d93
+! { dg-options "-fdump-tree-optimized -O -fdec-pad-with-spaces" }
840d93
+!
840d93
+! PR fortran/46974
840d93
+
840d93
+program test
840d93
+  use ISO_C_BINDING
840d93
+  implicit none
840d93
+  type(c_ptr) :: m
840d93
+  integer(c_intptr_t) :: a
840d93
+  integer(transfer(transfer(4_c_intptr_t, c_null_ptr),1_c_intptr_t)) :: b
840d93
+  a = transfer (transfer("ABCE", m), 1_c_intptr_t)
840d93
+  print '(z8)', a
840d93
+  if (     int(z'45434241') /= a  &
840d93
+     .and. int(z'41424345') /= a  &
840d93
+     .and. int(z'4142434520202020',kind=8) /= a &
840d93
+     .and. int(z'2020202045434241',kind=8) /= a ) &
840d93
+    call i_do_not_exist()
840d93
+end program test
840d93
+
840d93
+! Examples contributed by Steve Kargl and James Van Buskirk
840d93
+
840d93
+subroutine bug1
840d93
+   use ISO_C_BINDING
840d93
+   implicit none
840d93
+   type(c_ptr) :: m
840d93
+   type mytype
840d93
+     integer a, b, c
840d93
+   end type mytype
840d93
+   type(mytype) x
840d93
+   print *, transfer(32512, x)  ! Works.
840d93
+   print *, transfer(32512, m)  ! Caused ICE.
840d93
+end subroutine bug1 
840d93
+
840d93
+subroutine bug6
840d93
+   use ISO_C_BINDING
840d93
+   implicit none
840d93
+   interface
840d93
+      function fun()
840d93
+         use ISO_C_BINDING
840d93
+         implicit none
840d93
+         type(C_FUNPTR) fun
840d93
+      end function fun
840d93
+   end interface
840d93
+   type(C_PTR) array(2)
840d93
+   type(C_FUNPTR) result
840d93
+   integer(C_INTPTR_T), parameter :: const(*) = [32512,32520]
840d93
+
840d93
+   result = fun()
840d93
+   array = transfer([integer(C_INTPTR_T)::32512,32520],array)
840d93
+!   write(*,*) transfer(result,const)
840d93
+!   write(*,*) transfer(array,const)
840d93
+end subroutine bug6
840d93
+
840d93
+function fun()
840d93
+   use ISO_C_BINDING
840d93
+   implicit none
840d93
+   type(C_FUNPTR) fun
840d93
+   fun = transfer(32512_C_INTPTR_T,fun)
840d93
+end function fun 
840d93
+
840d93
+! { dg-final { scan-tree-dump-times "i_do_not_exist" 0 "optimized" } }