Bug 35612 - testsuite ISO_C_BIND code error
Summary: testsuite ISO_C_BIND code error
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 35613 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-03-17 03:48 UTC by Daniel E. Platt
Modified: 2010-12-27 23:28 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2008-03-17 23:37:32


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel E. Platt 2008-03-17 03:48:22 UTC
bind_c_usage_8.f03 in gcc/testsuite/gfortran.dg contains:

! PR fortran/32797
!
MODULE ISO_C_UTILITIES
   USE ISO_C_BINDING
   implicit none
   CHARACTER(C_CHAR), DIMENSION(1), SAVE, TARGET, PRIVATE :: dummy_string="?"
CONTAINS
   FUNCTION C_F_STRING(CPTR) RESULT(FPTR)
     use, intrinsic :: iso_c_binding
      TYPE(C_PTR), INTENT(IN) :: CPTR ! The C address
      CHARACTER(KIND=C_CHAR), DIMENSION(:), POINTER :: FPTR
      INTERFACE
         FUNCTION strlen(string) RESULT(len) BIND(C,NAME="strlen") ! { dg-warning "Implicitly declared" }
            USE ISO_C_BINDING
            TYPE(C_PTR), VALUE :: string ! A C pointer
         END FUNCTION
      END INTERFACE
      CALL C_F_POINTER(FPTR=FPTR, CPTR=CPTR, SHAPE=[strlen(CPTR)])
   END FUNCTION
END MODULE ISO_C_UTILITIES
! { dg-final { cleanup-modules "iso_c_utilities" } }

The line:

TYPE(C_PTR), INTENT(IN) :: CPTR ! The C address

should be:

TYPE(C_PTR), VALUE, TARGET:: CPTR ! the C address
Comment 1 Toon Moene 2008-03-17 16:00:21 UTC
*** Bug 35613 has been marked as a duplicate of this bug. ***
Comment 2 Tobias Burnus 2008-03-17 23:37:32 UTC
> The line:
>   TYPE(C_PTR), INTENT(IN) :: CPTR ! The C address
> should be:C_F_STRING(CPTR)
>   TYPE(C_PTR), VALUE, TARGET:: CPTR ! the C address

You are right. The call to strlen is OK in either case, however, the call to C_F_POINTER() does not make sense for a pointer to a C string ("**char") only for the version with VALUE ("*char") as fptr should finally contain the string.
Comment 3 Daniel E. Platt 2008-03-18 01:22:57 UTC
Subject: Re:  testsuite ISO_C_BIND code error

FPTR finally should POINT at that string (same thing CSTR points at)  
with the correct type info/structure/array-shape set up correctly.

My primary reason to send this in is that these test code provides  
great source code examples to figure out how this stuff SHOULD  
work...  in this case, I found myself taking on the role of debugger  
when I wasn't quite sure how it was supposed to work in the first  
place.  I just figured it would help out...

Dan

On Mar 17, 2008, at 7:37 PM, burnus at gcc dot gnu dot org wrote:

>
>
> ------- Comment #2 from burnus at gcc dot gnu dot org  2008-03-17  
> 23:37 -------
>> The line:
>>   TYPE(C_PTR), INTENT(IN) :: CPTR ! The C address
>> should be:C_F_STRING(CPTR)
>>   TYPE(C_PTR), VALUE, TARGET:: CPTR ! the C address
>
> You are right. The call to strlen is OK in either case, however,  
> the call to
> C_F_POINTER() does not make sense for a pointer to a C string  
> ("**char") only
> for the version with VALUE ("*char") as fptr should finally contain  
> the string.
>
>
> -- 
>
> burnus at gcc dot gnu dot org changed:
>
>            What    |Removed                     |Added
> ---------------------------------------------------------------------- 
> ------
>              Status|UNCONFIRMED                 |NEW
>      Ever Confirmed|0                           |1
>    Last reconfirmed|0000-00-00 00:00:00         |2008-03-17 23:37:32
>                date|                            |
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35612
>
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.

Comment 4 Mikael Morin 2009-01-06 21:16:30 UTC
(In reply to comment #2)
> > The line:
> >   TYPE(C_PTR), INTENT(IN) :: CPTR ! The C address
> > should be:C_F_STRING(CPTR)
> >   TYPE(C_PTR), VALUE, TARGET:: CPTR ! the C address
> 
> You are right. The call to strlen is OK in either case, however, the call to
> C_F_POINTER() does not make sense for a pointer to a C string ("**char") only
> for the version with VALUE ("*char") as fptr should finally contain the string.
> 

For TARGET, I agree because the standard says about c_f_pointer:
   The value of CPTR shall not be the C address of a Fortran variable that does
   not have the TARGET attribute.

However, for VALUE, I fail to understand what is wrong.
Without value, the code generated is:
c_f_string (struct array1_unknown & __result, integer(kind=4) .__result, void * & cptr)
{
    (...)
    (*(integer(kind=4)[1] *) atmp.0.data)[0] = strlen (*cptr);
    (...)
    c_f_pointer_s0 (*cptr, (struct array1_unknown *) __result, &atmp.3, 1);
}

With value:
c_f_string (struct array1_unknown & __result, integer(kind=4) .__result, void * cptr)
{
    (...)
    (*(integer(kind=4)[1] *) atmp.0.data)[0] = strlen (cptr);
    (...)
    c_f_pointer_s0 (cptr, (struct array1_unknown *) __result, &atmp.3, 1);
}

Both seem equally good, aren't they?
Comment 5 Mikael Morin 2009-01-07 13:51:58 UTC
(In reply to comment #4)
> For TARGET, I agree because the standard says about c_f_pointer:
>    The value of CPTR shall not be the C address of a Fortran variable that does
>    not have the TARGET attribute.
> 
Hum, I don't agree with myself. Only the pointee shall have the TARGET attribute. 
Not the pointer. Thus, I think the testcase is OK as is.
Comment 6 Daniel Franke 2010-05-02 14:00:07 UTC
The testcase is still in its original form, the last attempt to get this changed was never commented on:
      http://gcc.gnu.org/ml/gcc-patches/2009-01/msg00118.html

Setting status to WAITING. To be closed as WORKSFORME(?) in 3 months form now if no further information comes forward.
Comment 7 Daniel Franke 2010-12-27 23:28:16 UTC
(In reply to comment #6)
> Setting status to WAITING. To be closed as WORKSFORME(?) in 3 months form now
> if no further information comes forward.

Closing.