[PATCH, fortran] tabs are evil

Steve Kargl sgk@troutmask.apl.washington.edu
Mon Mar 6 00:13:00 GMT 2006


The attached patch issues an error if one uses a 
tab in a context where a member of the Fortran
Character Set is expected.  By default gfortran
will accept a tab as whitespace, which is its
current behavior.  I have added the -ftabs option.
For -ftabs, we accept tabs as whitespace, and 
for -fno-tabs we issue an error.  Note that
-pedantic and/or -std=f95 will trigger the error.
Also, note that "-std=f95 -ftabs" accepts tabs
as whitespaces.


2006-03-05  Steven G. Kargl  <kargls@comcast.net>

	* gfortran.h: Wrap Copyright line.
	(gfc_option_t): add flag_tabs member.
	* lang.opt: Update Coyright year.  Add the ftabs.
	* scanner.c (gfc_gobble_whitespace): Use flag_tabs.
	(load_line): Add seen_comment.  Use it and flag_tabs.
	* options.c (gfc_init_options): Initialize flag_tabs.
	(gfc_post_options): Adjust flag_tabs depending on -pedantic.
	(gfc_handle_option):  Process command-line option -f[no-]tabs


I just realized that I didn't update gfortran.texi.  I'll
add a description of the new option before I commit.

-- 
Steve
-------------- next part --------------
Index: fortran/gfortran.h
===================================================================
--- fortran/gfortran.h	(revision 111742)
+++ fortran/gfortran.h	(working copy)
@@ -1,6 +1,6 @@
 /* gfortran header file
-   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software 
-   Foundation, Inc.
+   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
+   Free Software Foundation, Inc.
    Contributed by Andy Vaught
 
 This file is part of GCC.
@@ -1628,6 +1628,7 @@ typedef struct
   int flag_cray_pointer;
   int flag_d_lines;
   int flag_openmp;
+  int flag_tabs;
 
   int q_kind;
 
Index: fortran/lang.opt
===================================================================
--- fortran/lang.opt	(revision 111742)
+++ fortran/lang.opt	(working copy)
@@ -1,5 +1,5 @@
 ; Options for the Fortran 95 front end.
-; Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
+; Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
 ;
 ; This file is part of GCC.
 ;
@@ -180,6 +180,10 @@ Copy array sections into a contiguous bl
 fpreprocessed
 Fortran
 Treat the input file as preprocessed
+
+ftabs
+Fortran
+Tabs are allowed in the Fortran Character Set
 
 qkind=
 Fortran RejectNegative Joined UInteger
Index: fortran/scanner.c
===================================================================
--- fortran/scanner.c	(revision 111742)
+++ fortran/scanner.c	(working copy)
@@ -843,6 +843,10 @@ gfc_gobble_whitespace (void)
     {
       old_loc = gfc_current_locus;
       c = gfc_next_char_literal (0);
+      /* We use a fatal error here because the Fortran matchers will often
+         back up and the same line will be scanned multiple times.  */
+      if (!gfc_option.flag_tabs && c == '\t')
+	gfc_fatal_error ("Nonconforming tab character at %C");
     }
   while (gfc_is_whitespace (c));
 
@@ -866,7 +870,7 @@ static int
 load_line (FILE * input, char **pbuf, int *pbuflen)
 {
   int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
-  int trunc_flag = 0;
+  int trunc_flag = 0, seen_comment = 0;
   char *buffer;
 
   /* Determine the maximum allowed line length.
@@ -932,8 +936,19 @@ load_line (FILE * input, char **pbuf, in
 	  break;
 	}
 
+      /* Is this a fixed-form comment?  */
+      if (gfc_current_form == FORM_FIXED && i == 0
+	  && (c == '*' || c == 'c' || c == 'd'))
+	seen_comment = 1;
+
       if (gfc_current_form == FORM_FIXED && c == '\t' && i <= 6)
-	{			/* Tab expansion.  */
+	{
+	  /* The error machinery isn't available at this point, so we can't
+	     easily report line and column numbers consistent with other 
+	     parts of gfortran.  */
+	  if (!gfc_option.flag_tabs && seen_comment == 0)
+	    gfc_fatal_error ("Nonconforming tab character");
+
 	  while (i <= 6)
 	    {
 	      *buffer++ = ' ';
Index: fortran/options.c
===================================================================
--- fortran/options.c	(revision 111742)
+++ fortran/options.c	(working copy)
@@ -78,6 +78,7 @@ gfc_init_options (unsigned int argc ATTR
   gfc_option.flag_cray_pointer = 0;
   gfc_option.flag_d_lines = -1;
   gfc_option.flag_openmp = 0;
+  gfc_option.flag_tabs = 1;
 
   gfc_option.q_kind = gfc_default_double_kind;
 
@@ -271,6 +272,11 @@ gfc_post_options (const char **pfilename
   if (!gfc_option.flag_automatic)
     gfc_option.flag_max_stack_var_size = 0;
 
+  /* If -pedantic is given on the command line, then produce
+     errors for nonconforming tabs.  */
+  if (pedantic)
+    gfc_option.flag_tabs = 0;
+ 
   return false;
 }
 
@@ -530,6 +536,10 @@ gfc_handle_option (size_t scode, const c
       gfc_option.flag_default_double = value;
       break;
 
+    case OPT_ftabs:
+      gfc_option.flag_tabs = value;
+      break;
+
     case OPT_I:
       gfc_add_include_path (arg);
       break;
@@ -547,6 +557,7 @@ gfc_handle_option (size_t scode, const c
       gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77;
       gfc_option.warn_std = GFC_STD_F95_OBS;
       gfc_option.max_identifier_length = 31;
+      gfc_option.flag_tabs = 0;
       break;
 
     case OPT_std_f2003:


More information about the Fortran mailing list