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

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