Debian Codegraph @

gcc/testsuite/gfortran.dg/f_c_string2.f90

raw
1 ! { dg-do run }
2 ! See pr117643, this tests passing of the optional argument when it is
3 ! not present.
4
5 program foo
6 use iso_c_binding, only : c_null_char, c_char, f_c_string, c_size_t
7 implicit none
8
9 logical asis
10 character(len=6, kind=c_char) :: s1
11 character(len=:, kind=c_char), allocatable :: s2
12
13 interface
14 !
15 ! strlen() counts up to '\0', and excludes it from the count
16 !
17 function strlen(s) bind(c,name="strlen")
18 import c_char, c_size_t
19 integer(c_size_t) strlen
20 character(len=1,kind=c_char), intent(in) :: s(*)
21 end function strlen
22 end interface
23
24 s1 = 'abc '
25 asis = .true.
26 call check (asis) ! OK
27 asis = .false.
28 call check (asis) ! OK
29 call check () ! segfault fixed
30
31 contains
32
33 subroutine check (asis)
34 logical, optional, intent(in) :: asis
35
36 s2 = f_c_string(s1, asis)
37 if (present(asis)) then
38 if (asis) then
39 if (int(strlen(s2)) /= 6) then
40 stop 1
41 endif
42 else
43 if (len_trim(s1) /= int(strlen(s2))) then
44 stop 2
45 endif
46 endif
47 endif
48 end subroutine check
49
50 end program foo