cpplib: tidy "char" signedness

Neil Booth NeilB@earthling.net
Tue Mar 7 05:34:00 GMT 2000


This tidies up cpplib's handling of the 3 character types to reduce
casting and potential for signedness bugs across architectures with
char types.  In particular, as priorities were "char" and were being
incremented and compared, make them unsigned.  It also adds casts to
some uncasted allocations in mkdeps.c.

gcc bootstraps with this applied.

Neil.

	* cppexp.c (struct operation, left_shift, right_shift,
	cpp_parse_expr) Change some "char"s to "U_CHAR"s.
	* cpplib.c (detect_if_not_defined, do_assert, do_unassert)
	Similarly.
	* cpplib.h Update for above.
	* mkdeps.c (deps_init, deps_calc_target) Cast pointers
	returned from allocations.

Index: cppexp.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cppexp.c,v
retrieving revision 1.35
diff -u -p -r1.35 cppexp.c
--- cppexp.c	2000/03/04 01:42:56	1.35
+++ cppexp.c	2000/03/07 13:17:45
@@ -77,8 +77,8 @@ Written by Per Bothner 1994.  */
 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
 
 static void integer_overflow PARAMS ((cpp_reader *));
-static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT, int, unsigned HOST_WIDEST_INT));
-static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT, int, unsigned HOST_WIDEST_INT));
+static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT, unsigned int, unsigned HOST_WIDEST_INT));
+static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT, unsigned int, unsigned HOST_WIDEST_INT));
 static struct operation parse_number PARAMS ((cpp_reader *, U_CHAR *, U_CHAR *));
 static struct operation parse_charconst PARAMS ((cpp_reader *, U_CHAR *, U_CHAR *));
 static struct operation parse_defined PARAMS ((cpp_reader *));
@@ -109,9 +109,9 @@ static HOST_WIDEST_INT cpp_parse_escape 
 
 struct operation {
     short op;
-    char rprio; /* Priority of op (relative to it right operand).  */
-    char flags;
-    char unsignedp;    /* true if value should be treated as unsigned */
+    U_CHAR rprio; /* Priority of op (relative to it right operand).  */
+    U_CHAR flags;
+    U_CHAR unsignedp;    /* true if value should be treated as unsigned */
     HOST_WIDEST_INT value;        /* The value logically "right" of op.  */
 };
 
@@ -620,7 +620,7 @@ static HOST_WIDEST_INT
 left_shift (pfile, a, unsignedp, b)
      cpp_reader *pfile;
      HOST_WIDEST_INT a;
-     int unsignedp;
+     unsigned int unsignedp;
      unsigned HOST_WIDEST_INT b;
 {
   if (b >= HOST_BITS_PER_WIDEST_INT)
@@ -644,7 +644,7 @@ static HOST_WIDEST_INT
 right_shift (pfile, a, unsignedp, b)
      cpp_reader *pfile ATTRIBUTE_UNUSED;
      HOST_WIDEST_INT a;
-     int unsignedp;
+     unsigned int unsignedp;
      unsigned HOST_WIDEST_INT b;
 {
   if (b >= HOST_BITS_PER_WIDEST_INT)
@@ -699,7 +699,7 @@ cpp_parse_expr (pfile)
   struct operation *stack = init_stack;
   struct operation *limit = stack + INIT_STACK_SIZE;
   register struct operation *top = stack;
-  int lprio, rprio = 0;
+  unsigned int lprio, rprio = 0;
   int skip_evaluation = 0;
 
   top->rprio = 0;
@@ -707,7 +707,7 @@ cpp_parse_expr (pfile)
   for (;;)
     {
       struct operation op;
-      char flags = 0;
+      U_CHAR flags = 0;
 
       /* Read a token */
       op =  cpp_lex (pfile, skip_evaluation);
@@ -790,7 +790,8 @@ cpp_parse_expr (pfile)
       while (top->rprio > lprio)
 	{
 	  HOST_WIDEST_INT v1 = top[-1].value, v2 = top[0].value;
-	  int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
+	  unsigned int unsigned1 = top[-1].unsignedp;
+	  unsigned int unsigned2 = top[0].unsignedp;
 	  top--;
 	  if ((top[1].flags & LEFT_OPERAND_REQUIRED)
 	      && ! (top[0].flags & HAVE_VALUE))
@@ -1027,7 +1028,7 @@ cpp_parse_expr (pfile)
 	  else
 	    {
 	      new_stack = (struct operation *) xmalloc (new_size);
-	      bcopy ((char *) stack, (char *) new_stack, old_size);
+	      memcpy (new_stack, stack, old_size);
 	    }
 	  stack = new_stack;
 	  top = (struct operation *) ((char *) new_stack + old_size);
Index: cpplib.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpplib.c,v
retrieving revision 1.125
diff -u -p -r1.125 cpplib.c
--- cpplib.c	2000/03/04 19:42:04	1.125
+++ cpplib.c	2000/03/07 13:17:50
@@ -1814,7 +1814,7 @@ detect_if_not_defined (pfile)
 
   if (pfile->only_seen_white == 2)
     {
-      char *ident;
+      U_CHAR *ident;
       enum cpp_token token;
       int base_offset;
       int token_offset;
@@ -2275,7 +2275,7 @@ do_endif (pfile, keyword)
 	      for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
 		if (ip->fname != NULL)
 		  break;
-	      ip->ihash->control_macro = (char *) temp->control_macro;
+	      ip->ihash->control_macro = temp->control_macro;
 	    }
         }
       free (temp);
@@ -3056,7 +3056,7 @@ do_assert (pfile, keyword)
      cpp_reader *pfile;
      const struct directive *keyword ATTRIBUTE_UNUSED;
 {
-  char *sym;
+  U_CHAR *sym;
   int ret, c;
   HASHNODE *base, *this;
   int baselen, thislen;
@@ -3065,7 +3065,7 @@ do_assert (pfile, keyword)
     cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
 
   cpp_skip_hspace (pfile);
-  sym = (char *) CPP_PWRITTEN (pfile);	/* remember where it starts */
+  sym = CPP_PWRITTEN (pfile);	/* remember where it starts */
   ret = parse_assertion (pfile);
   if (ret == 0)
     goto error;
@@ -3084,7 +3084,7 @@ do_assert (pfile, keyword)
     }
 
   thislen = strlen (sym);
-  baselen = index (sym, '(') - sym;
+  baselen = (U_CHAR *) index (sym, '(') - sym;
   this = cpp_lookup (pfile, sym, thislen);
   if (this)
     {
@@ -3106,12 +3106,12 @@ do_assert (pfile, keyword)
 		      (char *)base->value.aschain);
   base->value.aschain = this;
   
-  pfile->limit = (unsigned char *) sym; /* Pop */
+  pfile->limit = sym;		/* Pop */
   return 0;
 
  error:
   skip_rest_of_line (pfile);
-  pfile->limit = (unsigned char *) sym; /* Pop */
+  pfile->limit = sym;		/* Pop */
   return 0;
 }
 
@@ -3121,7 +3121,7 @@ do_unassert (pfile, keyword)
      const struct directive *keyword ATTRIBUTE_UNUSED;
 {
   int c, ret;
-  char *sym;
+  U_CHAR *sym;
   long baselen, thislen;
   HASHNODE *base, *this, *next;
   
@@ -3130,7 +3130,7 @@ do_unassert (pfile, keyword)
 
   cpp_skip_hspace (pfile);
 
-  sym = (char *) CPP_PWRITTEN (pfile);	/* remember where it starts */
+  sym = CPP_PWRITTEN (pfile);	/* remember where it starts */
   ret = parse_assertion (pfile);
   if (ret == 0)
     goto error;
@@ -3158,7 +3158,7 @@ do_unassert (pfile, keyword)
     }
   else
     {
-      baselen = index (sym, '(') - sym;
+      baselen = (U_CHAR *) index (sym, '(') - sym;
       base = cpp_lookup (pfile, sym, baselen);
       if (! base) goto error;
       this = cpp_lookup (pfile, sym, thislen);
@@ -3175,11 +3175,11 @@ do_unassert (pfile, keyword)
 	delete_macro (base);  /* Last answer for this predicate deleted. */
     }
   
-  pfile->limit = (unsigned char *) sym; /* Pop */
+  pfile->limit = sym;		/* Pop */
   return 0;
  error:
   skip_rest_of_line (pfile);
-  pfile->limit = (unsigned char *) sym; /* Pop */
+  pfile->limit = sym;		/* Pop */
   return 0;
 }
 
Index: cpplib.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpplib.h,v
retrieving revision 1.64
diff -u -p -r1.64 cpplib.h
--- cpplib.h	2000/03/04 19:42:04	1.64
+++ cpplib.h	2000/03/07 13:17:51
@@ -541,7 +540,7 @@ struct include_hash
   struct file_name_list *foundhere;
   const char *name;		/* (partial) pathname of file */
   const char *nshort;		/* name of file as referenced in #include */
-  const char *control_macro;	/* macro, if any, preventing reinclusion -
+  const U_CHAR *control_macro;	/* macro, if any, preventing reinclusion -
 				   see redundant_include_p */
   char *buf, *limit;		/* for file content cache,
 				   not yet implemented */
@@ -630,7 +629,7 @@ struct if_stack {
   int lineno;			/* similarly */
   int if_succeeded;		/* true if a leg of this if-group
 				    has been passed through rescan */
-  unsigned char *control_macro;	/* For #ifndef at start of file,
+  U_CHAR *control_macro;	/* For #ifndef at start of file,
 				   this is the macro name tested.  */
   enum node_type type;		/* type of last directive seen in this group */
 };
Index: mkdeps.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/mkdeps.c,v
retrieving revision 1.4
diff -u -p -r1.4 mkdeps.c
--- mkdeps.c	2000/03/07 11:41:19	1.4
+++ mkdeps.c	2000/03/07 13:17:56
@@ -135,8 +135,8 @@ deps_init ()
 
   /* Allocate space for the vectors now.  */
 
-  d->targetv = xmalloc (2 * sizeof (const char *));
-  d->depv = xmalloc (8 * sizeof (const char *));
+  d->targetv = (const char**) xmalloc (2 * sizeof (const char *));
+  d->depv = (const char**) xmalloc (8 * sizeof (const char *));
 
   d->ntargets = 0;
   d->targets_size = 2;
@@ -188,7 +188,7 @@ deps_calc_target (d, t)
   char *o, *suffix;
 
   t = base_name (t);
-  o = alloca (strlen (t) + 8);
+  o = (const char *) alloca (strlen (t) + 8);
 
   strcpy (o, t);
   suffix = strrchr (o, '.');



More information about the Gcc-patches mailing list