c-common.c (builtin_define_type_max): Handle unsigned types too.
authorNeil Booth <neil@daikokuya.co.uk>
Wed, 11 Dec 2002 06:36:17 +0000 (06:36 +0000)
committerNeil Booth <neil@gcc.gnu.org>
Wed, 11 Dec 2002 06:36:17 +0000 (06:36 +0000)
* c-common.c (builtin_define_type_max): Handle unsigned
types too.
testsuite:
* gcc.dg/fshort-wchar: New test.

From-SVN: r60023

gcc/ChangeLog
gcc/c-common.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/fshort-wchar.c [new file with mode: 0644]

index d46e2df05fe77c6e9aba9976ebf38a51b323958d..3e0756a638a760d71ba2496e036e220c521d1e84 100644 (file)
@@ -1,3 +1,8 @@
+2002-12-11  Neil Booth  <neil@daikokuya.co.uk>
+
+       * c-common.c (builtin_define_type_max): Handle unsigned
+       types too.
+
 2002-12-10  Janis Johnson  <janis187@us.ibm.com>
 
        PR other/8882
index 35adf760020e316b442f9d189e1f3886dd34062f..76f79274861698539958192218236744fb14e920 100644 (file)
@@ -5106,8 +5106,9 @@ builtin_define_with_hex_fp_value (macro, type, digits, hex_str, fp_suffix)
   cpp_define (parse_in, buf);
 }
 
-/* Define MAX for TYPE based on the precision of the type, which is assumed
-   to be signed.  IS_LONG is 1 for type "long" and 2 for "long long".  */
+/* Define MAX for TYPE based on the precision of the type.  IS_LONG is
+   1 for type "long" and 2 for "long long".  We have to handle
+   unsigned types, since wchar_t might be unsigned.  */
 
 static void
 builtin_define_type_max (macro, type, is_long)
@@ -5115,41 +5116,37 @@ builtin_define_type_max (macro, type, is_long)
      tree type;
      int is_long;
 {
-  const char *value;
+  static const char *const values[]
+    = { "127", "255",
+       "32767", "65535",
+       "2147483647", "4294967295",
+       "9223372036854775807", "18446744073709551615",
+       "170141183460469231731687303715884105727",
+       "340282366920938463463374607431768211455" };
+  static const char *const suffixes[] = { "", "U", "L", "UL", "LL", "ULL" };
+
+  const char *value, *suffix;
   char *buf;
-  size_t mlen, vlen, extra;
+  size_t idx;
 
   /* Pre-rendering the values mean we don't have to futz with printing a
      multi-word decimal value.  There are also a very limited number of
      precisions that we support, so it's really a waste of time.  */
   switch (TYPE_PRECISION (type))
     {
-    case 8:
-      value = "127";
-      break;
-    case 16:
-      value = "32767";
-      break;
-    case 32:
-      value = "2147483647";
-      break;
-    case 64:
-      value = "9223372036854775807";
-      break;
-    case 128:
-      value = "170141183460469231731687303715884105727";
-      break;
-    default:
-      abort ();
+    case 8:    idx = 0; break;
+    case 16:   idx = 2; break;
+    case 32:   idx = 4; break;
+    case 64:   idx = 6; break;
+    case 128:  idx = 8; break;
+    default:    abort ();
     }
 
-  mlen = strlen (macro);
-  vlen = strlen (value);
-  extra = 2 + is_long;
-  buf = alloca (mlen + vlen + extra);
+  value = values[idx + TREE_UNSIGNED (type)];
+  suffix = suffixes[is_long * 2 + TREE_UNSIGNED (type)];
 
-  sprintf (buf, "%s=%s%s", macro, value,
-          (is_long == 1 ? "L" : is_long == 2 ? "LL" : ""));
+  buf = alloca (strlen (macro) + 1 + strlen (value) + strlen (suffix) + 1);
+  sprintf (buf, "%s=%s%s", macro, value, suffix);
 
   cpp_define (parse_in, buf);
 }
index f54e347f5f211aace7dd2425b5b439d0ad8c180c..f00da68cfc9192eca391b3654ffcd041d5372d80 100644 (file)
@@ -1,3 +1,7 @@
+2002-12-11  Neil Booth  <neil@daikokuya.co.uk>
+
+       * gcc.dg/fshort-wchar: New test.
+
 2002-12-10  Mark Mitchell  <mark@codesourcery.com>
 
        PR c++/8372
diff --git a/gcc/testsuite/gcc.dg/fshort-wchar.c b/gcc/testsuite/gcc.dg/fshort-wchar.c
new file mode 100644 (file)
index 0000000..074e872
--- /dev/null
@@ -0,0 +1,18 @@
+/* Copyright (C) 2002 Free Software Foundation, Inc.  */
+
+/* { dg-do run } */
+/* { dg-options "-fshort-wchar" } */
+
+/* Source: Neil Booth, 10 Dec 2002.
+
+   Test that __WCHAR_MAX__ is correct with -fshort-wchar.  */
+
+int main ()
+{
+  __WCHAR_TYPE__ w = ~(__WCHAR_TYPE__) 0;
+
+  if (w != __WCHAR_MAX__)
+    abort ();
+  return 0;
+}