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]

[patch, fortran] PR39229 No warning of truncated lines if a continuation line follows


Hi folks,

The attached patch changes the approach to handling truncation warnings.
  One of the problems we have is that the truncation is detected at the
scanner level and the truncation flag was being cleared if a comment was
found.  Unfortunately, at that level, the error handling mechanisms such
as current locus are not available yet.  However we can read check the
compile options.

This patch puts the check for -Wline_truncation at the scanner level at
the point of detecting truncation.  The truncation flag is then allowed
to be handed up to the parse level where it is checked at each call to
next statement.  At that point the error infrastructure is working and
we use it to get a locus on it.  I temporarily adjusted the locus to the
 maxval of the line which is where the truncation occurs before
issueing the warning, and then adjust it back.

[aside:  I see enough instances where I have used this trick with
offsetting the locus that I plan to introduce a new function to just do
this and always leave the locus where it was found.  This will give us a
consistent approach to fine tuning the error loci as we move forward.  I
will do that as a separate patch.]

Regarding the test case. You may notice that since line 10 is continued to line 11, next_statement does not see the truncation of line 11 because on its next call, it has moved on to line 12. This means that this patch is a vast improvement but not ideal. I would like to get this much in for now and will leave the PR open for now while I study the situation some more.

Regression tested on x86-64.

OK for trunk.

Regards,

Jerry

2009-08-24 Jerry DeLisle <jvdelisle@gcc.gnu.org>

	PR fortran/39229
	* scanner.c (next_char): Fix typo in comment.
	(restart:): Don't clear truncate flag.
	(load_line): Set seen_comment for '!'. Set truncation flag only if
	option -Wline-truncation is given.
	* parse.c (next_statement): Issue a warning if the truncate flag is set.
	Remember to adjust the locus for a meaningful message.

Index: scanner.c
===================================================================
--- scanner.c	(revision 151156)
+++ scanner.c	(working copy)
@@ -614,7 +614,7 @@ next_char (void)
 
 /* Skip a comment.  When we come here the parse pointer is positioned
    immediately after the comment character.  If we ever implement
-   compiler directives withing comments, here is where we parse the
+   compiler directives within comments, here is where we parse the
    directive.  */
 
 static void
@@ -1023,9 +1023,6 @@ restart:
 	    }
 	  while (c != '\n');
 
-	  /* Avoid truncation warnings for comment ending lines.  */
-	  gfc_current_locus.lb->truncated = 0;
-
 	  goto done;
 	}
 
@@ -1150,9 +1147,6 @@ restart:
 	      c = next_char ();
 	    }
 	  while (c != '\n');
-
-	  /* Avoid truncation warnings for comment ending lines.  */
-	  gfc_current_locus.lb->truncated = 0;
 	}
 
       if (c != '\n')
@@ -1467,6 +1461,8 @@ load_line (FILE *input, gfc_char_t **pbuf, int *pb
       if (gfc_current_form == FORM_FIXED && i == 0
 	  && (c == '*' || c == 'c' || c == 'd'))
 	seen_comment = 1;
+      if (c == '!')
+        seen_comment = 1;
 
       /* Vendor extension: "<tab>1" marks a continuation line.  */
       if (found_tab)
@@ -1526,7 +1522,8 @@ load_line (FILE *input, gfc_char_t **pbuf, int *pb
 	      if (c == '\n' || c == EOF)
 		break;
 
-	      trunc_flag = 1;
+	      if (gfc_option.warn_line_truncation && !seen_comment)
+		trunc_flag = 1;
 	    }
 
 	  c = '\n';
Index: parse.c
===================================================================
--- parse.c	(revision 151156)
+++ parse.c	(working copy)
@@ -862,6 +862,8 @@ next_statement (void)
 {
   gfc_statement st;
   locus old_locus;
+  int maxlen = 72;
+
   gfc_new_block = NULL;
 
   gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
@@ -871,13 +873,19 @@ next_statement (void)
       gfc_buffer_error (1);
 
       if (gfc_at_eol ())
+	gfc_advance_line ();
+
+      if (gfc_current_locus.lb != NULL && gfc_current_locus.lb->truncated)
 	{
-	  if ((gfc_option.warn_line_truncation || gfc_current_form == FORM_FREE)
-	      && gfc_current_locus.lb
-	      && gfc_current_locus.lb->truncated)
-	    gfc_warning_now ("Line truncated at %C");
+	  /* Determine the maximum allowed line length.  */
+	  if (gfc_current_form == FORM_FREE)
+	    maxlen = gfc_option.free_line_length;
+	  else if (gfc_current_form == FORM_FIXED)
+	    maxlen = gfc_option.fixed_line_length;
 
-	  gfc_advance_line ();
+	  gfc_current_locus.nextc += maxlen;
+	  gfc_warning_now ("Line truncated at %L", &gfc_current_locus);
+	  gfc_current_locus.nextc -= maxlen;
 	}
 
       gfc_skip_comments ();
! { dg-do compile }
! { dg-options "-ffixed-form -Wline-truncation" }
! PR39229 No warning of truncated lines if a continuation line follows 
      ! expected: no warning by default (as column 73+ is often used for )
      ! comments in fixed-form source code.
      ! however, with -wline-truncation there shall be a warning.
      implicit none
        call foo([11, 22, 33, 44, 55, 66, 770, 9900, 1100, 1100, 120], 12 warn
     &          , 'hello')
      print *, min(1                                                     warn
     &, 2)                                                               nowarn
      contains
      subroutine foo(a,n,s)
        integer :: a(*), n, i
        character(len=*) :: s
        do i = 1, n
          print *, s, a(i)
        end do
      end subroutine foo
      end
! { dg-warning "Line truncated" " " { target *-*-* } 8 }
! { dg-warning "Line truncated" " " { target *-*-* } 10 }

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