Hi Steve,

thanks for the draft patch.

I haven't looked close enough, but you may have to add support
for 'asis' being an optional dummy variable.  The following
example crashes here with a segfault:

program foo
   use iso_c_binding, only : c_null_char, c_char, f_c_string, c_size_t
   implicit none

   logical asis
   character(len=6, kind=c_char) :: s1
   character(len=:, kind=c_char), allocatable :: s2

   interface
      !
      ! strlen() counts up to '\0', and excludes it from the count
      !
      function strlen(s) bind(c,name="strlen")
         import c_char, c_size_t
         integer(c_size_t) strlen
         character(len=1,kind=c_char), intent(in) :: s(*)
      end function strlen
   end interface

   s1 = 'abc   '
   asis = .true.
   call check (asis) ! OK
   asis = .false.
   call check (asis) ! OK
   call check ()     ! segfault

 contains

   subroutine check (asis)
     logical, optional, intent(in) :: asis

     s2 = f_c_string(s1, asis)
     print *, len_trim(s1), int(strlen(s2))

   end subroutine check

end program foo


There are other intrinsics with optional arguments that are inlined
and where we need to check the case of optional dummies.
Look for uses of gfc_conv_expr_present().

Harald

Am 18.12.24 um 05:20 schrieb Steve Kargl:
All,

First, I would like to thank both mikael and fx for providing
help in my debugging of the in-lining in trans-intrinsic.cc.
It seems I have forgotten much of what I once knew about trees.

I have attached a patch that implements F2023 F_C_STRING() to

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

and to this email.  This function is provided by ISO_C_BINDING,
and the current implementation in-lines the function when the
intrinsic module is used.

On x86_64-*-freebsd, I have

                  === gfortran Summary ===

# of expected passes            71796
# of unexpected failures        24
# of expected failures          274
# of unsupported tests          87
/usr/home/kargl/gcc/obj/gcc/gfortran  version 15.0.0 20241217
(experimental) (GCC)

The unexpected failures are all ASAN or LTO related.  Jerryd has
indicated that the patch bootstraps on x86_64_linux_gnu and also
regression tests cleanly.  The specific changes are

* check.cc (gfc_check_f_c_string): Check arguments of f_c_string().
* gfortran.h: New symbol GFC_ISYM_F_C_STRING.
* intrinsic.cc (add_functions): Add the ISO C Binding routine f_c_string().
   Wrap nearby long line to less than 80 characters.
* intrinsic.h: Prototype for gfc_check_f_c_string().
* iso-c-binding.def: Declare for ISO C Binding routine f_c_string().
* primary.cc (gfc_match_rvalue): Fix comment that has been untrue since 2011.
   Add ISOCBINDING_F_C_STRING to conditional.
* trans-intrinsic.cc(conv_trim): Specialized version of trim() for
   f_c_string().
   (conv_isocbinding_function): Inline implementation of f_c_string().
   (gfc_conv_intrinsic_function): Use GFC_ISYM_F_C_STRING to trigger in-lining.

* gfortran.dg/f_c_string1.f90: New testcase.



Reply via email to