Bug 21459 - strings of different length in a single character(len=*) declaration
Summary: strings of different length in a single character(len=*) declaration
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.1.0
: P2 normal
Target Milestone: 4.0.3
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: Fortran_character
  Show dependency treegraph
 
Reported: 2005-05-09 03:25 UTC by Kamaraju Kusumanchi
Modified: 2005-10-18 14:21 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-10-02 18:10:25


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kamaraju Kusumanchi 2005-05-09 03:25:52 UTC
Consider the following test code

program format_string
  implicit none
  character(len=*), parameter :: rform='(F15.5)', &
  cform="(' (', F15.5, ',' F15.5, ') ')"

  call print_a_number(cform)
contains
subroutine print_a_number(style)
  character(len=*) :: style
  write(*, style) cmplx(0.0, 0.0)
end subroutine print_a_number
end program format_string

This program compiles fine. But gives a runtime error.

$gfortran-20050507-1 -v
Using built-in specs.
Target: i486-linux
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f95,objc,ada,treelang
--prefix=/usr/lib/gcc-snapshot --enable-shared --with-system-zlib --disable-nls
--program-suffix=-20050507-1 --enable-__cxa_atexit
--enable-libstdcxx-allocator=mt --enable-clocale=gnu --enable-libstdcxx-debug
--enable-java-gc=boehm --enable-java-awt=gtk --enable-mpfr --disable-werror
--disable-werror i486-linux
Thread model: posix
gcc version 4.1.0 20050507 (experimental)

$gfortran format_string.f90 -o format_string

$./a.out
At line 10 of file format_string.f90
Fortran runtime error: Unexpected end of format string
(' (',
      ^

I am using Debian Sid distribution, gcc-snapshot 20050507-1 package.

The above program compiles and runs fine with Absoft Fortran 90/95 compiler and
with intel fortran compiler 8.1 Build 20040921. The interaction with Absoft
compiler looks as follows

$f90 format_string.f90
rajulocal@kusumanchi:~/work/fortran/gfortran_bugs 28  527 11:21 PM
$./a.out
 (        0.00000,        0.00000)
Comment 1 Francois-Xavier Coudert 2005-05-09 11:15:32 UTC
Reduced testcase is following. The root of the problem is that when we declare
strings of different length in the same "character(len=*)" declaration, all
strings are truncated to the length of the first one. Example:

$ cat pr21459.f90
  character(len=*) :: c1='12345', c2='123456789'
  print *, c1
  print *, c2
  end
$ gfortran pr21459.f90 && ./a.out
 12345
 12345
Comment 2 Tobias Schlüter 2005-05-11 20:24:25 UTC
The problem is that all characters declared on the same line share the same
gfc_charlen structure.  This should be fixed during resolution, I'll have a look
at this.
Comment 3 Tobias Schlüter 2005-05-11 20:45:09 UTC
This patch passes the testsuite and fixes the bug, but creates a memory leak:

       * decl.c (add_init_expr_to_sym): Create new gfc_charlen for each
       item with length = (*)

Index: decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fortran/decl.c,v
retrieving revision 1.35
diff -u -p -r1.35 decl.c
--- decl.c      29 Apr 2005 15:31:37 -0000      1.35
+++ decl.c      11 May 2005 20:39:13 -0000
@@ -740,6 +740,11 @@ add_init_expr_to_sym (const char *name,
          /* Update symbol character length according initializer.  */
          if (sym->ts.cl->length == NULL)
            {
+             /* If there are multiple CHARACTER variables declared on
+                the same line, we don't want them to share the same
+                length.  */
+             sym->ts.cl = gfc_get_charlen ();
+
              if (init->expr_type == EXPR_CONSTANT)
                sym->ts.cl->length =
                        gfc_int_expr (init->value.character.length);

In order to evade the memory leak, we should instead of setting
sym->ts.cl->length = NULL, set sym->ts.cl = &gfc_unknown_charlen, where the
latter is a new global placeholder variable, and then fill in sym->ts.cl as I
did in the patch.  I've seen something like this in g95, but I don't remember if
this was what it was used for there.
Comment 4 GCC Commits 2005-10-17 20:52:45 UTC
Subject: Bug 21459

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	pault@gcc.gnu.org	2005-10-17 20:52:37

Modified files:
	gcc/fortran    : ChangeLog gfortran.h resolve.c expr.c decl.c 
	                 match.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gfortran.dg: host_dummy_index_1.f90 
	                           automatic_char_len_2.f90 
	                           recursive_statement_functions.f90 
	                           assumed_size_dt_dummy.f90 
	                           external_initializer.f90 
	                           non_module_public.f90 

Log message:
	2005-10-17  Paul Thomas  <pault@gcc.gnu.org>
	
	PR fortran/23446
	* gfortran.h: Primitive for gfc_is_formal_arg.
	* resolve.c(gfc_is_formal_arg): New function to signal across
	several function calls that formal argument lists are being
	processed.
	(resolve_formal_arglist): Set/reset the flag for gfc_is_formal_arg.
	*expr.c(check_restricted): Add check, via gfc_is_formal_arg, if
	symbol is part of an formal argument declaration.
	
	PR fortran/21459
	* decl.c (add_init_expr_to_sym): Make a new character
	length for each variable, when the expression is NULL
	and link to cl_list.
	
	PR fortran/20866
	* match.c (recursive_stmt_fcn): New function that tests if
	a statement function resurses through itself or other other
	statement functions.
	(gfc_match_st_function): Call recursive_stmt_fcn to check
	if this is recursive and to raise error if so.
	
	PR fortran/20849
	PR fortran/20853
	* resolve.c (resolve_symbol): Errors for assumed size arrays
	with default initializer and for external objects with an
	initializer.
	
	PR fortran/20837
	* decl.c (match_attr_spec): Prevent PUBLIC from being used
	outside a module.
	
	2005-10-17  Paul Thomas  <pault@gcc.gnu.org>
	
	PR fortran/23446
	* gfortran.dg/host_dummy_index_1.f90: New test.
	
	PR fortran/21459
	gfortran.dg/automatic_char_len_2.f90: New test.
	
	PR fortran/20866
	gfortran.dg/recursive_statement_functions.f90: New test.
	
	PR fortran/20853
	gfortran.dg/assumed_size_dt_dummy.f90: New test.
	
	PR fortran/20849
	gfortran.dg/external_initializer.f90: New test.
	
	PR fortran/20837
	non_module_public.f90: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/ChangeLog.diff?cvsroot=gcc&r1=1.587&r2=1.588
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/gfortran.h.diff?cvsroot=gcc&r1=1.89&r2=1.90
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/resolve.c.diff?cvsroot=gcc&r1=1.59&r2=1.60
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/expr.c.diff?cvsroot=gcc&r1=1.30&r2=1.31
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/decl.c.diff?cvsroot=gcc&r1=1.42&r2=1.43
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/match.c.diff?cvsroot=gcc&r1=1.46&r2=1.47
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.6205&r2=1.6206
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/host_dummy_index_1.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/automatic_char_len_2.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/recursive_statement_functions.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/assumed_size_dt_dummy.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/external_initializer.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/non_module_public.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 5 GCC Commits 2005-10-18 05:55:34 UTC
Subject: Bug 21459

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	pault@gcc.gnu.org	2005-10-18 05:55:23

Modified files:
	gcc/fortran    : ChangeLog gfortran.h resolve.c expr.c decl.c 
	                 match.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gfortran.dg: host_dummy_index_1.f90 
	                           automatic_char_len_2.f90 
	                           recursive_statement_functions.f90 
	                           assumed_size_dt_dummy.f90 
	                           external_initializer.f90 
	                           non_module_public.f90 

Log message:
	2005-10-18  Paul Thomas  <pault@gcc.gnu.org>
	
	PR fortran/23446
	* gfortran.h: Primitive for gfc_is_formal_arg.
	* resolve.c(gfc_is_formal_arg): New function to signal across
	several function calls that formal argument lists are being
	processed.
	(resolve_formal_arglist): Set/reset the flag for gfc_is_formal_arg.
	*expr.c(check_restricted): Add check, via gfc_is_formal_arg, if
	symbol is part of an formal argument declaration.
	
	PR fortran/21459
	* decl.c (add_init_expr_to_sym): Make a new character
	length for each variable, when the expression is NULL
	and link to cl_list.
	
	PR fortran/20866
	* match.c (recursive_stmt_fcn): New function that tests if
	a statement function resurses through itself or other other
	statement functions.
	(gfc_match_st_function): Call recursive_stmt_fcn to check
	if this is recursive and to raise error if so.
	
	PR fortran/20849
	PR fortran/20853
	* resolve.c (resolve_symbol): Errors for assumed size arrays
	with default initializer and for external objects with an
	initializer.
	
	PR fortran/20837
	* decl.c (match_attr_spec): Prevent PUBLIC from being used
	outside a module.
	
	2005-10-18  Paul Thomas  <pault@gcc.gnu.org>
	
	PR fortran/23446
	* gfortran.dg/host_dummy_index_1.f90: New test.
	
	PR fortran/21459
	gfortran.dg/automatic_char_len_2.f90: New test.
	
	PR fortran/20866
	gfortran.dg/recursive_statement_functions.f90: New test.
	
	PR fortran/20853
	gfortran.dg/assumed_size_dt_dummy.f90: New test.
	
	PR fortran/20849
	gfortran.dg/external_initializer.f90: New test.
	
	PR fortran/20837
	non_module_public.f90: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.335.2.130&r2=1.335.2.131
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/gfortran.h.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.58.2.18&r2=1.58.2.19
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/resolve.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.34.2.18&r2=1.34.2.19
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/expr.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.23.2.4&r2=1.23.2.5
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/decl.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.31.2.5&r2=1.31.2.6
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/match.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.31.8.13&r2=1.31.8.14
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.5084.2.465&r2=1.5084.2.466
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/host_dummy_index_1.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/automatic_char_len_2.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/recursive_statement_functions.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/assumed_size_dt_dummy.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/external_initializer.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/non_module_public.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1

Comment 6 Andrew Pinski 2005-10-18 14:21:42 UTC
Fixed.