[PATCH,gfortran] Fix for PR 17941

Steve Kargl sgk@troutmask.apl.washington.edu
Fri Dec 31 18:55:00 GMT 2004


The attached patch fixes PR 17941.  The attached program
is a test program suitable for inclusion in the gfortran
testsuite.

Briefly, gfortran could not deal with spaces between an
uniary plus/minus sign in a complex constant.  That is,
"complex, parameter :: c = (-   1.0,   +  2)" would invoke
an error.

This patch addresses 2 problems.  First, the parsing of the
complex constant did not account for the space.  Second, 
mpfr_set_str() does not like spaces in its input string.

Bubblestrapped and regression tested on i386-*-freebsd6.0

2004-12-30  Steven G. Kargl  <kargls@comcast.net>

       * arith.c: Include stdlib.h and string.h
       (gfc_convert_real): Use alloca and strlen; account for whitespace.
       * primary.c (match_const_complex_part): Account for whitespace

2004-12-30  Steven G. Kargl  <kargls@comcast.net>

       * cmplx_constant.f90: new test case

-- 
Steve
-------------- next part --------------
Index: arith.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fortran/arith.c,v
retrieving revision 1.18
diff -u -b -u -b -B -r1.18 arith.c
--- arith.c	8 Nov 2004 14:56:37 -0000	1.18
+++ arith.c	31 Dec 2004 05:35:31 -0000
@@ -25,6 +25,9 @@
    would evaluate them.  We use the GNU MP library to do arithmetic,
    and this file provides the interface.  */
 
+#include <stdlib.h> /* Needed for alloca.  */
+#include <string.h> /* Needed for strlen.  */
+
 #include "config.h"
 #include "system.h"
 #include "flags.h"
@@ -1929,6 +1932,7 @@
 {
   gfc_expr *e;
   const char *t;
+  char *u, *u1;
 
   e = gfc_constant_result (BT_REAL, kind, where);
   /* A leading plus is allowed in Fortran, but not by mpfr_set_str */
@@ -1936,7 +1940,21 @@
     t = buffer + 1;
   else
     t = buffer;
-  mpfr_set_str (e->value.real, t, 10, GFC_RND_MODE);
+
+  /* Spaces are not allowed in the string passed to mpfr_set_str.  The 
+     parsing of complex constants may have spaces, so copy the t to u
+     where we remove spaces.  */
+  u = alloca (strlen (t) + 1);
+  u1 = u;
+  while (*t != '\0')
+    {
+      if (*t != ' ' && *t != '\t')
+	*u++ = *t;
+      *t++;
+    }
+    *u = '\0';
+
+  mpfr_set_str (e->value.real, u1, 10, GFC_RND_MODE);
 
   return e;
 }
Index: primary.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fortran/primary.c,v
retrieving revision 1.14
diff -u -b -u -b -B -r1.14 primary.c
--- primary.c	8 Nov 2004 14:56:39 -0000	1.14
+++ primary.c	31 Dec 2004 05:35:32 -0000
@@ -1026,6 +1026,13 @@
       count++;
     }
 
+  /* Account for whitespace between the uniary minus/plus and the number.  */
+  while (c == ' ' || c == '\t')
+    {
+      c = gfc_next_char ();
+      count++;
+    }
+
   for (;; c = gfc_next_char (), count++)
     {
       if (c == '.')
-------------- next part --------------
!
! Test program for PR 17941.
!
program gfcbug17
  complex, parameter :: c0 = (-0.5, -     0.5)
  complex, parameter :: c1 = (-     0.5, +     0.5)
  complex, parameter :: c2 = (-    0.5E2, +0.5)
  complex, parameter :: c3 = (-0.5, +     0.5E-2)
  if (abs(c0 - cmplx(-0.5,-0.5)) > 1.E-4) call abort
  if (abs(c1 - cmplx(-0.5,+0.5)) > 1.E-4) call abort
  if (abs(c2 - cmplx(-0.5E2,+0.5)) > 1.E-4) call abort
  if (abs(c3 - cmplx(-0.5,+0.5E-2)) > 1.E-4) call abort
end program gfcbug17



More information about the Fortran mailing list