Re: [PATCH] Fortran: fix passing of optional dummies to bind(c) procedures [PR113866]

2024-02-13 Thread Steve Kargl
On Mon, Feb 12, 2024 at 09:57:08PM +0100, Harald Anlauf wrote:
> Dear all,
> 
> the attached patch fixes a mis-handling of optional dummy arguments
> passed to optional dummy arguments of procedures with the bind(c)
> attribute.  When those procedures are expecting CFI descriptors,
> there is no special treatment like a presence check necessary
> that by default passes a NULL pointer as default.
> 
> The testcase tries to exercise various combinations of passing
> assumed-length character between bind(c) and non-bind(c), which
> apparently was insufficiently covered in the testsuite.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 

Yes.  Thanks for filling out the more detailed testcase.

-- 
Steve


Re: [PATCH] Fortran: fix passing of optional dummies to bind(c) procedures [PR113866]

2024-02-13 Thread Harald Anlauf

Hi Steve,

Am 13.02.24 um 18:21 schrieb Steve Kargl:

On Mon, Feb 12, 2024 at 09:57:08PM +0100, Harald Anlauf wrote:

Dear all,

the attached patch fixes a mis-handling of optional dummy arguments
passed to optional dummy arguments of procedures with the bind(c)
attribute.  When those procedures are expecting CFI descriptors,
there is no special treatment like a presence check necessary
that by default passes a NULL pointer as default.

The testcase tries to exercise various combinations of passing
assumed-length character between bind(c) and non-bind(c), which
apparently was insufficiently covered in the testsuite.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?



Yes.  Thanks for filling out the more detailed testcase.


indeed the new testcase just regressed due to commit
r14-8947-g6caec7d9ec37e6 ... :-(

Reduced testcase which fails on trunk:

program p
  implicit none
  integer, parameter :: n = 100, l = 10
  character(l) :: a = 'a234567890', b(n) = 'bcdefghijk'
  character(:), allocatable :: d, e(:)
  allocate (d, source=a)
  allocate (e, source=b)
  print *, len (d), len (e), size (e)
  call not_bindc_optional_deferred (d, e)
  deallocate (d, e)
contains
  subroutine not_bindc_optional_deferred (c5, c6)
character(:), allocatable, optional :: c5, c6(:)
if (.not. present (c5) .or. .not. present (c6)) stop 6
print *, len (c5), len (c6), size (c6)
if (len (c5) /= l .or. len (c6) /= l) stop 84
  end
end

Expected:

  10  10 100
  10  10 100

After above commit:

  10  10 100
  10   0 100
STOP 84

Will have to wait until the cause is found and fixed...



Re: [PATCH] Fortran: fix passing of optional dummies to bind(c) procedures [PR113866]

2024-02-13 Thread Harald Anlauf

Am 13.02.24 um 19:13 schrieb Harald Anlauf:

indeed the new testcase just regressed due to commit
r14-8947-g6caec7d9ec37e6 ... :-(

Reduced testcase which fails on trunk:

program p
   implicit none
   integer, parameter :: n = 100, l = 10
   character(l) :: a = 'a234567890', b(n) = 'bcdefghijk'
   character(:), allocatable :: d, e(:)
   allocate (d, source=a)
   allocate (e, source=b)
   print *, len (d), len (e), size (e)
   call not_bindc_optional_deferred (d, e)
   deallocate (d, e)
contains
   subroutine not_bindc_optional_deferred (c5, c6)
     character(:), allocatable, optional :: c5, c6(:)
     if (.not. present (c5) .or. .not. present (c6)) stop 6
     print *, len (c5), len (c6), size (c6)
     if (len (c5) /= l .or. len (c6) /= l) stop 84
   end
end

Expected:

   10  10 100
   10  10 100

After above commit:

   10  10 100
   10   0 100
STOP 84


This is now tracked as::

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113911


Will have to wait until the cause is found and fixed...


As it is sufficient to disable the deferred-length test,
I've done that and pushed the amended patch as

https://gcc.gnu.org/g:f4935df217ad89f884f908f39086b322e80123d0

Thanks,
Harald




[patch, libgfortran] PR99210 X editing for reading file with encoding='utf-8'

2024-02-13 Thread Jerry DeLisle
The attached patch fixes the X editing.

Fairly self explanatory. I created the patch a few years back.

Regression tested on x86_64 and new test case.

OK for trunk?

Regards,

Jerrydiff --git a/gcc/testsuite/gfortran.dg/pr99210.f90 b/gcc/testsuite/gfortran.dg/pr99210.f90
new file mode 100644
index 000..9fd2fb468df
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr99210.f90
@@ -0,0 +1,29 @@
+! { dg-do run }
+! PR99210 X editing for reading file with encoding='utf-8'
+program test_bug_format_x
+  use iso_fortran_env
+  integer, parameter :: u = selected_char_kind('ISO_10646')
+
+  character(kind=u, len=1) a, b, a1, b1, b2
+
+  open(unit=10, file='test_bug_format_x.tmp', encoding='UTF-8')
+
+  a = char(int(z'03B1'), u)
+  b = char(int(z'03B2'), u)
+  write(10, '(a1, a1)') a, b
+
+  rewind(10)
+  read(10, '(a1, a1)') a1, b1
+
+  rewind(10)
+  read(10, '(1x, a1)') b2
+
+  close (10, status="delete")
+  if(a /= a1 .or. b /= b1) then
+error stop 1
+  end if
+
+  if(b /= b2) then
+error stop 2
+  end if
+end program test_bug_format_x
diff --git a/libgfortran/io/read.c b/libgfortran/io/read.c
index 0ffcf76fd38..e2d2f8be806 100644
--- a/libgfortran/io/read.c
+++ b/libgfortran/io/read.c
@@ -1307,6 +1307,23 @@ read_x (st_parameter_dt *dtp, size_t n)
 
   if (n == 0)
 return;
+
+  if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
+{
+  gfc_char4_t c;
+  size_t nbytes, j;
+
+  /* Proceed with decoding one character at a time.  */
+  for (j = 0; j < n; j++)
+	{
+	  c = read_utf8 (dtp, &nbytes);
+
+	  /* Check for a short read and if so, break out.  */
+	  if (nbytes == 0 || c == (gfc_char4_t)0)
+	break;
+	}
+  return;
+}
 
   length = n;