This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[fortran] patch for PR28039 --Warn when ignoring extra characters in the format specification


Below is a another attempt at adding this compile time format check.  A previous attempt was reverted (because it broke a lot of stuff) months ago.

I also added to the test suite another piece of code that was brought to my attention that helped highlight problems with the initial attempt.

The test case X-FAIL has been removed.  No new regressions shown in the testsuite after applying this patch.  Tested i686/gnu/linux FC12.

I expect to hold on to this and commit it when stage 1 opens for 4.6.  This is not a regression and is not a real important feature.


--bud

2009-12-29 Bud Davis <bdavis9659@sbcglobal.net>
           
           PR fortran/28039
           * io.c (check_format_string):  Added check for additional non 
           blank characters after the format string was successfully 
           parsed.
           * io.c (check_format): Changed the error messages for positive
           int required and period required to drop through the error logic
           and report with gfc_error instead of gfc_error_now.  Corrected
           format postion for hollerith strings.

Index: gcc/gcc/testsuite/gfortran.dg/fmt_with_extra.f
===================================================================
--- gcc/gcc/testsuite/gfortran.dg/fmt_with_extra.f	(revision 155511)
+++ gcc/gcc/testsuite/gfortran.dg/fmt_with_extra.f	(working copy)
@@ -4,5 +4,25 @@
        implicit none
        real :: r
        r = 1.0
-       write(*,'(a),f)') 'Hello', r   ! { dg-warning "Extraneous characters in format at" "PR28039" { xfail *-*-* } }
+       write(*,'(a),f)') 'Hello', r   ! { dg-warning "Extraneous characters in format at" }
        end
+! Below routine was also submitted by tobias.burnus@physik.fu-berlin.de
+! It showed up some problems with the initial implementation of this
+! feature.
+! This routine should compile without complaint or warning.
+      SUBROUTINE rw_inp()
+      CHARACTER(len=100) :: line
+      integer :: i5
+      character(100), parameter :: subchapter =
+     &        '(79("-"),/,5("-")," ",A,/,79("-"),/)'
+      i5 = 1
+
+      READ(*,FMT="(4x,a)") line
+ 7182 FORMAT (a3)
+ 7130 FORMAT (i3)
+
+      WRITE (6,'(//'' icorr is not correctly transferred.  icorr='',i5)
+     &    ') 42
+
+      write(*,subchapter) 'test'
+      END SUBROUTINE rw_inp
Index: gcc/gcc/fortran/io.c
===================================================================
--- gcc/gcc/fortran/io.c	(revision 155511)
+++ gcc/gcc/fortran/io.c	(working copy)
@@ -850,11 +850,11 @@
       if (u != FMT_POSINT)
 	{
 	  format_locus.nextc += format_string_pos;
-	  gfc_error_now ("Positive width required in format "
+	  gfc_error ("Positive width required in format "
 			 "specifier %s at %L", token_to_string (t),
 			 &format_locus);
 	  saved_token = u;
-	  goto finished;
+	  goto fail;
 	}
 
       u = format_lex ();
@@ -866,11 +866,11 @@
 	  format_locus.nextc += format_string_pos;
 	  if (gfc_option.warn_std != 0)
 	    {
-	      gfc_error_now ("Period required in format "
+	      gfc_error ("Period required in format "
 			     "specifier %s at %L", token_to_string (t),
 			     &format_locus);
 	      saved_token = u;
-	      goto finished;
+              goto fail;
 	    }
 	  else
 	    gfc_warning ("Period required in format "
@@ -970,11 +970,11 @@
 	  gfc_warning ("The H format specifier at %L is"
 		       " a Fortran 95 deleted feature", &format_locus);
 	}
-
       if (mode == MODE_STRING)
 	{
 	  format_string += value;
 	  format_length -= value;
+          format_string_pos += repeat;
 	}
       else
 	{
@@ -1152,6 +1152,8 @@
 static gfc_try
 check_format_string (gfc_expr *e, bool is_input)
 {
+  gfc_try rv;
+  int i;
   if (!e || e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
     return SUCCESS;
 
@@ -1162,8 +1164,20 @@
      format string that has been calculated, but that's probably not worth the
      effort.  */
   format_locus = e->where;
-
-  return check_format (is_input);
+  rv = check_format (is_input);
+  /* check for extraneous characters at the end of an otherwise valid format
+     string, like '(A10,I3)F5'
+     start at the end and move back to the last character processed,
+     spaces are OK */
+  if (rv == SUCCESS && e->value.character.length > format_string_pos)
+    for (i=e->value.character.length-1;i>format_string_pos-1;i--)
+      if (e->value.character.string[i] != ' ')
+        {
+          format_locus.nextc += format_length + 1; 
+          gfc_warning ("Extraneous characters in format at %L", &format_locus); 
+          break;
+        }
+  return rv;
 }






Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]