PATCH: more read_integral_parameter'ization

Theodore Papadopoulo Theodore.Papadopoulo@sophia.inria.fr
Sun Feb 28 18:15:00 GMT 1999


> In message < 9906.918020444@hurl.cygnus.com >, you wrote:
> You might want to do a grep for atoi for other places that could use this
> patch.  For example there's a couple places which deal with -Wblah-blah-N
> in toplev.c and one hunk of code in haifa-sched.c.  There may be others, I
> didn't look too closely.

Here is a patch that generalizes the use of 
read_integral_parameter in toplevel.c. It is now applied to
options -Oxxx -g*xxxx -Wid-clash-<num> -Wlarger-than-<number>.
I have tried to be very conservative (appart for the addition of new 
error messages) in the way the options are treated. In particular, I 
have tried to not change the values that are set when an error is 
encountered (except for the -g4 -g case).

This has needed some minor tweak in 
read_integral_parameter to avoid the error message in some situations
(when pname is 0) and to verify the option syntax more stricly (-O45f 
would not have been refused with the previous version). One other
reason why this has been necessary is 
that many options set both a flag and a value which is not exactly 
the model adopted in read_integral_parameter. In some situations, 
these two could have been combined into a single integer (encoding 
the false value of flag as a negative integer) but this was 
conflicting with a minimal change.

I have departed a little from conservatism for the -gxxx options.
I have tried to do an ``almost'' minimal cleaning that allowed the use of
read_integral_parameter and improved error messages slightly
(see the comment on -g4 -g). IMHO it should be cleaned even more...
I'll wait for reactions before starting sthg.

I have checked that the behaviour of option parsing 
remains essentially the same but I certainly have missed some variants
(particularly in the -gxxxx case).
 
ChangeLog

Thu Feb 25 21:49:55 1999  Theodore Papadopoulo  <Theodore.Papadopoulo@sophia.inria.fr>

	* toplev.h (read_integral_parameter): Constify.
	* toplev.c (read_integral_parameter): Constify. Better control of error messages.
	 (main): Use read_integral_parameter.


Index: gcc/toplev.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/toplev.c,v
retrieving revision 1.154
diff -c -3 -p -r1.154 toplev.c
*** toplev.c	1999/02/15 14:13:20	1.154
--- toplev.c	1999/02/25 21:49:03
*************** FILE *rtl_dump_file = NULL;
*** 1244,1270 ****
  
  /* Decode the string P as an integral parameter.
     If the string is indeed an integer return its numeric value else
!    issue an Invalid Option error for the option PNAME and return DEFVAL. */
     
  int
  read_integral_parameter (p, pname, defval)
!      char *p;
!      char *pname;
!      int  defval;
  {
!   char *endp = p;
  
    while (*endp)
      {
        if (*endp >= '0' && *endp <= '9')
  	endp++;
        else
! 	{
! 	  error ("Invalid option `%s'", pname);
! 	  return defval;
! 	}
      }
  
    return atoi (p);
  }
  
--- 1244,1275 ----
  
  /* Decode the string P as an integral parameter.
     If the string is indeed an integer return its numeric value else
!    issue an Invalid Option error for the option PNAME and return DEFVAL.
!    If PNAME is zero just return DEFVAL, do not call error.               */
     
  int
  read_integral_parameter (p, pname, defval)
!      const char *p;
!      const char *pname;
!      const int  defval;
  {
!   const char *endp = p;
  
    while (*endp)
      {
        if (*endp >= '0' && *endp <= '9')
  	endp++;
        else
! 	break;
      }
  
+   if (*endp != 0)
+     {
+       if (pname != 0)
+ 	error ("Invalid option `%s'", pname);
+       return defval;
+     }
+ 
    return atoi (p);
  }
  
*************** main (argc, argv)
*** 4646,4652 ****
  	{
  	  /* Handle -Os, -O2, -O3, -O69, ...  */
  	  char *p = &argv[i][2];
- 	  int c;
  	  
  	  if ((p[0] == 's') && (p[1] == 0))
  	    {
--- 4651,4656 ----
*************** main (argc, argv)
*** 4657,4668 ****
  	    }
  	  else
  	    {	    
! 	      while ((c = *p++))
! 		if (! (c >= '0' && c <= '9'))
! 		  break;
! 	      if (c == 0)
  		{
! 		  optimize = atoi (&argv[i][2]);
  		  optimize_size = 0;
  		}
  	    }
--- 4661,4670 ----
  	    }
  	  else
  	    {	    
! 	      const int optimize_val = read_integral_parameter(p, p - 2, -1);
! 	      if (optimize_val != -1)
  		{
! 		  optimize = optimize_val;
  		  optimize_size = 0;
  		}
  	    }
*************** main (argc, argv)
*** 4925,4940 ****
  	    }
  	  else if (str[0] == 'O')
  	    {
! 	      register char *p = str+1;
! 	      if (*p == 's')
! 		p++;
! 	      else
! 		while (*p && *p >= '0' && *p <= '9')
! 		  p++;
! 	      if (*p == '\0')
! 		;
! 	      else
! 		error ("Invalid option `%s'", argv[i]);
  	    }
  	  else if (!strcmp (str, "pedantic"))
  	    pedantic = 1;
--- 4927,4933 ----
  	    }
  	  else if (str[0] == 'O')
  	    {
! 	      /* Already been treated above. Do nothing.  */
  	    }
  	  else if (!strcmp (str, "pedantic"))
  	    pedantic = 1;
*************** main (argc, argv)
*** 4987,5025 ****
  		;
  	      else if (!strncmp (p, "id-clash-", 9))
  		{
! 		  char *endp = p + 9;
! 
! 		  while (*endp)
  		    {
! 		      if (*endp >= '0' && *endp <= '9')
! 			endp++;
! 		      else
! 			{
! 			  error ("Invalid option `%s'", argv[i]);
! 			  goto id_clash_lose;
! 			}
  		    }
- 		  warn_id_clash = 1;
- 		  id_clash_len = atoi (str + 10);
- 		id_clash_lose: ;
  		}
  	      else if (!strncmp (p, "larger-than-", 12))
  		{
! 		  char *endp = p + 12;
! 
! 		  while (*endp)
  		    {
! 		      if (*endp >= '0' && *endp <= '9')
! 			endp++;
! 		      else
! 			{
! 			  error ("Invalid option `%s'", argv[i]);
! 			  goto larger_than_lose;
! 			}
  		    }
- 		  warn_larger_than = 1;
- 		  larger_than_size = atoi (str + 13);
- 		larger_than_lose: ;
  		}
  	      else
  		error ("Invalid option `%s'", argv[i]);
--- 4980,5000 ----
  		;
  	      else if (!strncmp (p, "id-clash-", 9))
  		{
! 		  const int id_clash_val = read_integral_parameter(p + 9, p - 2, -1);
! 		  if (id_clash_val != -1)
  		    {
! 		      id_clash_len = id_clash_val;
! 		      warn_id_clash = 1;
  		    }
  		}
  	      else if (!strncmp (p, "larger-than-", 12))
  		{
! 		  const int larger_than_val = read_integral_parameter(p + 12, p - 2, -1);
! 		  if (larger_than_val != -1)
  		    {
! 		      larger_than_size = larger_than_val;
! 		      warn_larger_than = 1;
  		    }
  		}
  	      else
  		error ("Invalid option `%s'", argv[i]);
*************** main (argc, argv)
*** 5047,5053 ****
  	    }
  	  else if (str[0] == 'g')
  	    {
- 	      unsigned len;
  	      unsigned level;
  	      /* A lot of code assumes write_symbols == NO_DEBUG if the
  		 debugging level is 0 (thus -gstabs1 -gstabs0 would lose track
--- 5022,5027 ----
*************** main (argc, argv)
*** 5065,5112 ****
  		"none", "stabs", "coff", "dwarf-1", "dwarf-2", "xcoff"
  	      };
  
  	      /* Look up STR in the table.  */
  	      for (da = debug_args; da->arg; da++)
  		{
! 		  if (! strncmp (str, da->arg, strlen (da->arg)))
  		    {
  		      enum debug_info_type type = da->debug_type;
- 		      char *p, *q;
  
! 		      p = str + strlen (da->arg);
  		      if (*p && (*p < '0' || *p > '9'))
  			continue;
! 		      len = p - str;
! 		      q = p;
! 		      while (*q && (*q >= '0' && *q <= '9'))
! 			q++;
! 		      if (*p)
  			{
! 			  level = atoi (p);
! 			  if (len > 1 && !strncmp (str, "gdwarf", len))
! 			    {
! 			      error ("use -gdwarf -g%d for DWARF v1, level %d",
! 				       level, level);
! 			      if (level == 2)
! 				error ("use -gdwarf-2   for DWARF v2");
! 			    }
  			}
! 		      else
! 			level = 2;	/* default debugging info level */
! 		      if (*q || level > 3)
  			{
! 			  warning ("invalid debug level specification in option: `-%s'",
! 				   str);
! 			  /* ??? This error message is incorrect in the case of
! 			     -g4 -g.  */
! 			  warning ("no debugging information will be generated");
! 			  level = 0;
  			}
  
  		      if (type == NO_DEBUG)
  			{
  			  type = PREFERRED_DEBUGGING_TYPE;
! 			  if (len > 1 && strncmp (str, "ggdb", len) == 0)
  			    {
  #if defined (DWARF2_DEBUGGING_INFO) && !defined (LINKER_DOES_NOT_WORK_WITH_DWARF2)
  			      type = DWARF2_DEBUG;
--- 5039,5079 ----
  		"none", "stabs", "coff", "dwarf-1", "dwarf-2", "xcoff"
  	      };
  
+ 	      /* The maximum admissible debug level value.  */
+ 	      static const unsigned max_debug_level = 3;
+ 
  	      /* Look up STR in the table.  */
  	      for (da = debug_args; da->arg; da++)
  		{
! 		  const int da_len = strlen (da->arg);
! 		  if (! strncmp (str, da->arg, da_len))
  		    {
  		      enum debug_info_type type = da->debug_type;
  
! 		      const char *p = str + da_len;
  		      if (*p && (*p < '0' || *p > '9'))
  			continue;
! 		      
! 		      level = read_integral_parameter(p, 0, max_debug_level + 1);
! 		      if (da_len > 1 && !strncmp (str, "gdwarf", da_len))
  			{
! 			  error ("use -gdwarf -g%d for DWARF v1, level %d",
! 				   level, level);
! 			  if (level == 2)
! 			    error ("use -gdwarf-2   for DWARF v2");
  			}
! 
! 		      if (level > max_debug_level)
  			{
! 			  warning ("ignoring option `%s' due to invalid debug level specification",
! 				   str - 1);
! 			  level = debug_info_level;
  			}
  
  		      if (type == NO_DEBUG)
  			{
  			  type = PREFERRED_DEBUGGING_TYPE;
! 			  if (da_len > 1 && strncmp (str, "ggdb", da_len) == 0)
  			    {
  #if defined (DWARF2_DEBUGGING_INFO) && !defined (LINKER_DOES_NOT_WORK_WITH_DWARF2)
  			      type = DWARF2_DEBUG;
*************** main (argc, argv)
*** 5160,5167 ****
  	    }
  	  else if (str[0] == 'G')
  	    {
! 	      g_switch_set = TRUE;
! 	      g_switch_value = atoi ((str[1] != '\0') ? str+1 : argv[++i]);
  	    }
  	  else if (!strncmp (str, "aux-info", 8))
  	    {
--- 5127,5145 ----
  	    }
  	  else if (str[0] == 'G')
  	    {
! 	      const int g_switch_val = (str[1] != '\0') ?
! 	                               read_integral_parameter(str + 1, 0, -1) :
! 			               read_integral_parameter(argv[++i], 0, -1);
! 	      
! 	      if (g_switch_val != -1)
! 	        {
! 		  g_switch_set = TRUE;
! 		  g_switch_value = g_switch_val;
! 		}
! 	      else
! 	        {
! 		  error("Invalid option `-%s'",str);
! 		}
  	    }
  	  else if (!strncmp (str, "aux-info", 8))
  	    {
Index: gcc/toplev.h
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/toplev.h,v
retrieving revision 1.18
diff -c -3 -p -r1.18 toplev.h
*** toplev.h	1999/02/02 21:43:19	1.18
--- toplev.h	1999/02/25 21:49:03
*************** union tree_node;
*** 26,32 ****
  struct rtx_def;
  #endif
  
! extern int read_integral_parameter	PROTO ((char *, char *, int));
  extern int count_error			PROTO ((int));
  extern void strip_off_ending		PROTO ((char *, int));
  extern void print_time			PROTO ((const char *, int));
--- 26,33 ----
  struct rtx_def;
  #endif
  
! extern int read_integral_parameter	PROTO ((const char *, const char *,
! 						const int));
  extern int count_error			PROTO ((int));
  extern void strip_off_ending		PROTO ((char *, int));
  extern void print_time			PROTO ((const char *, int));

 --------------------------------------------------------------------
 Theodore Papadopoulo
 Email: Theodore.Papadopoulo@sophia.inria.fr Tel: (33) 04 92 38 76 01
 --------------------------------------------------------------------






More information about the Gcc-patches mailing list