Bug 32732 - [Bind C] Character scalars are passed as arrays
Summary: [Bind C] Character scalars are passed as arrays
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks: 32630
  Show dependency treegraph
 
Reported: 2007-07-11 21:20 UTC by Tobias Burnus
Modified: 2007-08-07 00:29 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2007-07-11 21:20:22 UTC
|  subroutine foo(a) bind(c)
|    character(len=1,kind=c_char), value :: a

Has a single, scalar argument a; in C notation:
| void foo(char a)

gfortran, however, uses
| void foo(char a[]) 
with length one.

This causes problems on IA-64 HP-UX, see thread starting at
http://gcc.gnu.org/ml/fortran/2007-07/msg00210.html

(If "a" is a scalar, one needs to audit also all character uses to make sure that one does use it as such (and does not compile Fortran "if(a /= 5)" into C "if(*a != 5) as "a" is not a pointer.)



Additionally, If the function is called, the length is additionally passed to the function:
| call foo("a")
produces:
| foo("a", 1) // 'a' array, not zero terminated
instead of
| foo('a')

As the length arguments come always last and are not used, this seems to be harmless, but there might be platforms or scenarios where they may cause problems.
Comment 1 patchapp@dberlin.org 2007-07-18 18:10:55 UTC
Subject: Bug number PR 32732

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-07/msg01542.html
Comment 2 Tobias Burnus 2007-07-23 09:03:43 UTC
Subject: Bug 32732

Author: burnus
Date: Mon Jul 23 09:03:30 2007
New Revision: 126836

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=126836
Log:
2007-07-23  Christopher D. Rickett  <crickett@lanl.gov>

       PR fortran/32732
       * trans-decl.c (generate_local_decl): Convert the TREE_TYPE for by
       value character dummy args of BIND(C) procedures.
       * trans-expr.c (gfc_conv_variable): Do not build address
       expression for BT_CHARACTER dummy args.

2007-07-23  Christopher D. Rickett  <crickett@lanl.gov>

       PR fortran/32732
       * gfortran.dg/c_char_tests.f03: New test case.
       * gfortran.dg/c_char_driver.c: Driver for c_char_tests.f03.
       * gfortran.dg/c_char_tests_2.f03: New test case.
       * gfortran.dg/value_6.f03: Ditto.
       * gfortran.dg/value_7.f03: Ditto.


Added:
    trunk/gcc/testsuite/gfortran.dg/c_char_driver.c
    trunk/gcc/testsuite/gfortran.dg/c_char_tests.f03
    trunk/gcc/testsuite/gfortran.dg/c_char_tests_2.f03
    trunk/gcc/testsuite/gfortran.dg/value_6.f03
    trunk/gcc/testsuite/gfortran.dg/value_7.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog

Comment 3 Tobias Burnus 2007-07-23 09:08:16 UTC
FIXED. Steve, please check under IA64 HP-UX whether it works now.
Comment 4 Steve Ellcey 2007-07-25 22:56:07 UTC
The test case is still not working for me on IA64 HP-UX.  I also still see gfortran.dg/c_kind_params.f90 failing at all optimization levels.  c_char_tests.f03 also fails
Comment 5 kargl 2007-08-07 00:27:38 UTC
Subject: Bug 32732

Author: kargl
Date: Tue Aug  7 00:27:25 2007
New Revision: 127265

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=127265
Log:
2007-08-06  Christopher D. Rickett  <crickett@lanl.gov>

        PR fortran/32732
        * trans-expr.c (gfc_conv_scalar_char_value): Convert the tree and
        actual arg expressions for scalar characters passed by-value to
        bind(c) routines.
        (gfc_conv_function_call): Call gfc_conv_scalar_char_value.
        * trans.h: Add prototype for gfc_conv_scalar_char_value.
        * trans-decl.c (generate_local_decl): Convert by-value character
        dummy args of bind(c) procedures using
        gfc_conv_scalar_char_value.


Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans.h

Comment 6 kargl 2007-08-07 00:29:12 UTC
Hopefully, Chris' patch fixed this bug.  If problems
re-appear we can also re-open the PR.