public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [gfortran,patch] PR32594: clean up a few substring features
@ 2007-08-13 23:47 FX Coudert
  2007-08-14 10:12 ` Tobias Burnus
  0 siblings, 1 reply; 2+ messages in thread
From: FX Coudert @ 2007-08-13 23:47 UTC (permalink / raw)
  To: GNU Fortran, gcc-patches list

[-- Attachment #1: Type: text/plain, Size: 1190 bytes --]

Hi all,

Attached simple patch fixes two different bugs pertaining to PR32594,  
because a few cases of substrings weren't handled. These cases are  
when the start or end of the substring is not specified: string(2:)  
or string(:7), which result in the substrings (gfc_ref * ref)- 
 >u.ss.start and gfc_ref * ref)->u.ss.end being NULL, respectively.  
This case was simplified correctly when the base string is a  
constant: this is what the fix in gfc_simplify_expr() does.

The second corner case is the one of string(:). It may not be the  
most used and useful substring, but it exists and the front-end  
signals this case by setting the substring's expr->ref to NULL. There  
are actually a few places in the front-end which didn't deal with  
this at all: one is gfc_simplify_expr(), fixed at the same time as  
above, the others are gfc_is_constant_expr and  
gfc_conv_substring_expr. The first is fixed simply because string(:)  
is constant if and only if string itself is constant. The second is  
fixed by building a copy of the string as substring, without calling  
gfc_conv_substring.

Regtested on x86_64-linux, comes with a testcase, OK to commit?




:ADDPATCH fortran:

[-- Attachment #2: pr32594.ChangeLog --]
[-- Type: application/octet-stream, Size: 478 bytes --]

2007-08-14  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>

	PR fortran/32594
	* trans-expr.c (gfc_conv_substring_expr): Only call
	gfc_conv_substring if expr->ref is not NULL.
	* expr.c (gfc_is_constant_expr): If e->ref is NULL, the substring
	expression might be a constant.
	(gfc_simplify_expr): Handle missing start and end, as well as
	missing ref.


2007-08-14  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>

	PR fortran/32594
	* gfortran.dg/substr_5.f90: New test.


[-- Attachment #3: pr32594.diff --]
[-- Type: application/octet-stream, Size: 3813 bytes --]

Index: gcc/fortran/trans-expr.c
===================================================================
--- gcc/fortran/trans-expr.c	(revision 127394)
+++ gcc/fortran/trans-expr.c	(working copy)
@@ -3243,14 +3243,15 @@ gfc_conv_substring_expr (gfc_se * se, gf
 
   ref = expr->ref;
 
-  gcc_assert (ref->type == REF_SUBSTRING);
+  gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
 
-  se->expr = gfc_build_string_const(expr->value.character.length,
-                                    expr->value.character.string);
+  se->expr = gfc_build_string_const (expr->value.character.length,
+				     expr->value.character.string);
   se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
-  TYPE_STRING_FLAG (TREE_TYPE (se->expr))=1;
+  TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
 
-  gfc_conv_substring(se,ref,expr->ts.kind,NULL,&expr->where);
+  if (ref)
+    gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
 }
 
 
Index: gcc/fortran/expr.c
===================================================================
--- gcc/fortran/expr.c	(revision 127394)
+++ gcc/fortran/expr.c	(working copy)
@@ -766,8 +766,8 @@ gfc_is_constant_expr (gfc_expr *e)
       break;
 
     case EXPR_SUBSTRING:
-      rv = (gfc_is_constant_expr (e->ref->u.ss.start)
-	    && gfc_is_constant_expr (e->ref->u.ss.end));
+      rv = e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
+			      && gfc_is_constant_expr (e->ref->u.ss.end));
       break;
 
     case EXPR_STRUCTURE:
@@ -1542,9 +1542,19 @@ gfc_simplify_expr (gfc_expr *p, int type
 	  char *s;
 	  int start, end;
 
-	  gfc_extract_int (p->ref->u.ss.start, &start);
-	  start--;  /* Convert from one-based to zero-based.  */
-	  gfc_extract_int (p->ref->u.ss.end, &end);
+	  if (p->ref && p->ref->u.ss.start)
+	    {
+	      gfc_extract_int (p->ref->u.ss.start, &start);
+	      start--;  /* Convert from one-based to zero-based.  */
+	    }
+	  else
+	    start = 0;
+
+	  if (p->ref && p->ref->u.ss.end)
+	    gfc_extract_int (p->ref->u.ss.end, &end);
+	  else
+	    end = p->value.character.length;
+
 	  s = gfc_getmem (end - start + 2);
 	  memcpy (s, p->value.character.string + start, end - start);
 	  s[end - start + 1] = '\0';  /* TODO: C-style string.  */
Index: gcc/testsuite/gfortran.dg/substr_5.f90
===================================================================
--- gcc/testsuite/gfortran.dg/substr_5.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/substr_5.f90	(revision 0)
@@ -0,0 +1,44 @@
+! { dg-do run }
+!
+  character(*), parameter  :: chrs = '-+.0123456789eEdD'
+  character(*), parameter  :: expr = '-+.0123456789eEdD'
+  integer :: i
+
+  if (index(chrs(:), expr) /= 1) call abort
+  if (index(chrs(14:), expr) /= 0) call abort
+  if (index(chrs(:12), expr) /= 0) call abort
+  if (index(chrs, expr(:)) /= 1) call abort
+  if (index(chrs, expr(1:)) /= 1) call abort
+  if (index(chrs, expr(:1)) /= 1) call abort
+
+  if (foo(expr) /= 1) call abort
+  if (foo(expr) /= 1) call abort
+  if (foo(expr) /= 1) call abort
+  if (foo(expr(:)) /= 1) call abort
+  if (foo(expr(1:)) /= 1) call abort
+  if (foo(expr(:1)) /= 1) call abort
+
+  call bar(expr)
+
+contains
+  subroutine bar(expr)
+    character(*), intent(in) :: expr
+    character(*), parameter  :: chrs = '-+.0123456789eEdD'
+    integer :: foo
+
+    if (index(chrs(:), expr) /= 1) call abort
+    if (index(chrs(14:), expr) /= 0) call abort
+    if (index(chrs(:12), expr) /= 0) call abort
+    if (index(chrs, expr(:)) /= 1) call abort
+    if (index(chrs, expr(1:)) /= 1) call abort
+    if (index(chrs, expr(:1)) /= 1) call abort
+  end subroutine bar
+
+  integer function foo(expr)
+    character(*), intent(in) :: expr
+    character(*), parameter  :: chrs = '-+.0123456789eEdD'
+
+    foo = index(chrs, expr)
+  end function foo
+
+end

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [gfortran,patch] PR32594: clean up a few substring features
  2007-08-13 23:47 [gfortran,patch] PR32594: clean up a few substring features FX Coudert
@ 2007-08-14 10:12 ` Tobias Burnus
  0 siblings, 0 replies; 2+ messages in thread
From: Tobias Burnus @ 2007-08-14 10:12 UTC (permalink / raw)
  To: FX Coudert; +Cc: GNU Fortran, gcc-patches list

:REVIEWMAIL:

FX Coudert wrote:
> Regtested on x86_64-linux, comes with a testcase, OK to commit?
OK.

Tobias

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-08-14 10:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-13 23:47 [gfortran,patch] PR32594: clean up a few substring features FX Coudert
2007-08-14 10:12 ` Tobias Burnus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).