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

            Bug ID: 119419
           Summary: C prototype generation for functions returning
                    C_FUNPTR incorrect
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ryan.gambord at oregonstate dot edu
  Target Milestone: ---

Created attachment 60849
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=60849&action=edit
Collected source files

When compiled with -fc-prototypes, the emitted function signature is incorrect,

==> MRE.f90 <==
function f(idx) bind(C)
    use, intrinsic :: ISO_C_BINDING
    type(C_FUNPTR) :: f
    integer(C_INT) :: idx
end function f
======

Emits `int (*f()) (int)` rather than `int (*f(int)) ()`

I have attached a sample fortran "library" and C program that illustrates the
issue:

==> lookup.f90 <==
MODULE test
   USE, INTRINSIC :: ISO_C_BINDING
CONTAINS
   FUNCTION lookup(idx) BIND(C)
      type(C_FUNPTR) :: lookup
      integer(C_INT), VALUE :: idx
      SELECT CASE (idx)
      CASE (1)
         lookup = C_FUNLOC(x1)
      CASE (2)
         lookup = C_FUNLOC(x2)
      CASE DEFAULT
         lookup = C_NULL_FUNPTR
      end SELECT
   END FUNCTION lookup
   subroutine x1()
      print *, "x1"
   end subroutine x1
   subroutine x2()
      print *, "x2"
   end subroutine x2
END MODULE test


==> test.c <==
#include <stdio.h>
/* Incorrect prototype */
/* #include "lookup.h" */
/* Correct function prototype */
void (*lookup(int))();
int main()
{
  lookup(1)(); /* "x1" */
  lookup(2)(); /* "x2" */
}



The generated function prototype in lookup.h is: `int (*lookup()) (int idx);`

Reply via email to