[Patch, fortran] PR64120

2023-10-31 Thread Paul Richard Thomas
I found this 'obvious' fix, while going through PRs assigned to me.

Regtests. OK for mainline?

Cheers

Paul


Fortran: Allocatable automatic charlen must not be saved [PR64120].

2023-10-31  Paul Thomas  

gcc/fortran
PR fortran/64120
* trans-decl.cc (gfc_trans_deferred_vars): Detect automatic
character length and allow allocatable variants to be nullified
on scope entry and freed on scope exit. Remove trailing white
space.

gcc/testsuite/
PR fortran/64120
* gfortran.dg/pr64120_2.f90: New test.
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index a3f037bd07b..5e0e78ace40 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -4689,9 +4689,14 @@ gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
 && (sym->ts.u.derived->attr.alloc_comp
 || gfc_is_finalizable (sym->ts.u.derived,
 			   NULL));
+  bool automatic_char_len;
   if (sym->assoc)
 	continue;
 
+  automatic_char_len = sym->ts.type == BT_CHARACTER
+			   && sym->ts.u.cl && sym->ts.u.cl->length
+			   && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE;
+
   /* Set the vptr of unlimited polymorphic pointer variables so that
 	 they do not cause segfaults in select type, when the selector
 	 is an intrinsic type.  */
@@ -4951,7 +4956,8 @@ gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
 		|| (sym->ts.type == BT_CLASS
 			&& CLASS_DATA (sym)->attr.allocatable)))
 	{
-	  if (!sym->attr.save && flag_max_stack_var_size != 0)
+	  if ((!sym->attr.save || automatic_char_len)
+	   && flag_max_stack_var_size != 0)
 	{
 	  tree descriptor = NULL_TREE;
 
@@ -5210,8 +5216,8 @@ gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
 	tree tmp = lookup_attribute ("omp allocate",
  DECL_ATTRIBUTES (n->sym->backend_decl));
 	tmp = TREE_VALUE (tmp);
-	TREE_PURPOSE (tmp) = se.expr;	
-	TREE_VALUE (tmp) = align;	
+	TREE_PURPOSE (tmp) = se.expr;
+	TREE_VALUE (tmp) = align;
 	TREE_PURPOSE (TREE_CHAIN (tmp)) = init_stmtlist;
 	TREE_VALUE (TREE_CHAIN (tmp)) = cleanup_stmtlist;
   }
! { dg-do compile }
! { dg-options "-fdump-tree-original" }
!
! Test fix of second testcase in PR64120.
! The first testcase is allocatable_scalar_14.f90.
!
! Contributed by Francois-Xavier Coudert  
!
program test
   logical :: L
   L = g(1)
   write(*,*) L
   L = g(2)
   write(*,*) L
contains
  logical function g(x)
  integer :: x
  character(len=x), allocatable :: s
  save
  if(.NOT.allocated(s)) then
allocate(s)
g = .FALSE.
  else
g = .TRUE.
  end if
  write(*,*) len(s)
  end function g
end
! { dg-final { scan-tree-dump-times "s = 0B;" 2 "original" } }
! { dg-final { scan-tree-dump-times "__builtin_free" 1 "original" } }


Re: [Patch, fortran] PR64120

2023-10-31 Thread Steve Kargl
On Tue, Oct 31, 2023 at 02:11:08PM +, Paul Richard Thomas wrote:
> I found this 'obvious' fix, while going through PRs assigned to me.
> 
> Regtests. OK for mainline?
> 

Yes.  Fell free to backport if you have time and desire.

-- 
Steve


[Patch, fortran] PR64120 - [F03] Wrong handling of allocatable character string

2018-09-17 Thread Paul Richard Thomas
This patch is relatively trivial. This initialization of the string
length was not being done.

Bootstraps and regtests on FC28/x86_64. OK for trunk?

Paul


2018-09-17  Paul Thomas  

PR fortran/64120
* trans-decl.c (gfc_get_symbol_decl): Flag allocatable, scalar
characters with a variable length expression for deferred init.
(gfc_trans_deferred_vars): Perform the assignment for these
symbols by calling gfc_conv_string_length.

2018-09-17  Paul Thomas  

PR fortran/64120
* gfortran.dg/allocatable_scalar_14.f90 : New test.
Index: gcc/fortran/trans-decl.c
===
*** gcc/fortran/trans-decl.c	(revision 264358)
--- gcc/fortran/trans-decl.c	(working copy)
*** gfc_get_symbol_decl (gfc_symbol * sym)
*** 1745,1750 
--- 1745,1757 
  	  && !(sym->attr.use_assoc && !intrinsic_array_parameter)))
  gfc_defer_symbol_init (sym);
  
+   if (sym->ts.type == BT_CHARACTER
+   && sym->attr.allocatable
+   && !sym->attr.dimension
+   && sym->ts.u.cl && sym->ts.u.cl->length
+   && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE)
+ gfc_defer_symbol_init (sym);
+ 
/* Associate names can use the hidden string length variable
   of their associated target.  */
if (sym->ts.type == BT_CHARACTER
*** gfc_trans_deferred_vars (gfc_symbol * pr
*** 4603,4608 
--- 4610,4622 
  	  gfc_set_backend_locus (&sym->declared_at);
  	  gfc_start_block (&init);
  
+ 	  if (sym->ts.type == BT_CHARACTER
+ 		  && sym->attr.allocatable
+ 		  && !sym->attr.dimension
+ 		  && sym->ts.u.cl && sym->ts.u.cl->length
+ 		  && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE)
+ 		gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
+ 
  	  if (!sym->attr.pointer)
  		{
  		  /* Nullify and automatic deallocation of allocatable
Index: gcc/testsuite/gfortran.dg/allocatable_scalar_14.f90
===
*** gcc/testsuite/gfortran.dg/allocatable_scalar_14.f90	(nonexistent)
--- gcc/testsuite/gfortran.dg/allocatable_scalar_14.f90	(working copy)
***
*** 0 
--- 1,17 
+ ! { dg-do run }
+ !
+ ! Test the fix for PR64120 in which the initialisation of the
+ ! string length of 's' was not being done.
+ !
+ ! Contributed by Francois-Xavier Coudert  
+ !
+call g(1)
+call g(2)
+ contains
+   subroutine g(x)
+   integer :: x
+   character(len=x), allocatable :: s
+   allocate(s)
+   if (len(s) .ne. x) stop x
+   end subroutine
+ end


Re: [Patch, fortran] PR64120 - [F03] Wrong handling of allocatable character string

2018-09-17 Thread Janne Blomqvist
On Mon, Sep 17, 2018 at 1:10 PM Paul Richard Thomas <
paul.richard.tho...@gmail.com> wrote:

> This patch is relatively trivial. This initialization of the string
> length was not being done.
>
> Bootstraps and regtests on FC28/x86_64. OK for trunk?
>

Ok.

-- 
Janne Blomqvist


Re: [Patch, fortran] PR64120 - [F03] Wrong handling of allocatable character string

2018-09-17 Thread Paul Richard Thomas
Hi Janne,

Thanks. Committed as revision 264365.

Paul


On 17 September 2018 at 11:34, Janne Blomqvist
 wrote:
> On Mon, Sep 17, 2018 at 1:10 PM Paul Richard Thomas
>  wrote:
>>
>> This patch is relatively trivial. This initialization of the string
>> length was not being done.
>>
>> Bootstraps and regtests on FC28/x86_64. OK for trunk?
>
>
> Ok.
>
> --
> Janne Blomqvist



-- 
"If you can't explain it simply, you don't understand it well enough"
- Albert Einstein