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]

Predefines for intmax_t


Though implementing <stdint.h> is a long way down my C standards
roadmap <http://www.srcf.ucam.org/~jsm28/gcc/#stdc>, having added an
ad hoc intmax_t definition to yet another testcase (builtin-attr-1.c)
it seemed appropriate to define the necessary predefined macros to
communicate the system's idea of intmax_t and its limits, that
eventually would be used in <stdint.h> and for now can be used in the
testsuite.  I also added a testcase following wint_t-1.c and
wchar_t-1.c to verify that GCC's notions of intmax_t and uintmax_t
agree with those in the system <inttypes.h>; this will need to be
XFAILed for platforms lacking that header.  (<inttypes.h> used rather
than <stdint.h> in the testcase because of systems based on old C9X
drafts that only had <inttypes.h>, not <stdint.h>.)

Bootstrapped with no regressions on i686-pc-linux-gnu.  Applied to
mainline.

-- 
Joseph S. Myers               http://www.srcf.ucam.org/~jsm28/gcc/
    jsm@polyomino.org.uk (personal mail)
    jsm28@gcc.gnu.org (Bugzilla assignments and CCs)

2004-08-05  Joseph S. Myers  <jsm@polyomino.org.uk>

	* c-common.c (c_stddef_cpp_builtins): Define __INTMAX_TYPE__ and
	__UINTMAX_TYPE__.
	* c-cppbuiltin.c (builtin_define_stdint_macros): New.  Define
	__INTMAX_MAX__.
	(c_cpp_builtins): Call it.
	* doc/cpp.texi: Update.

testsuite:
2004-08-05  Joseph S. Myers  <jsm@polyomino.org.uk>

	* gcc.c-torture/execute/builtins/abs-2.c,
	gcc.c-torture/execute/builtins/abs-3.c,
	gcc.c-torture/execute/builtins/lib/abs.c, gcc.dg/format/format.h,
	gcc.dg/torture/builtin-attr-1.c: Use predefined macros for
	intmax_t, uintmax_t and their limits.
	* gcc.dg/intmax_t-1.c: New test.

diff -rupN GCC.orig/gcc/c-common.c GCC/gcc/c-common.c
--- GCC.orig/gcc/c-common.c	2004-08-03 22:05:12.000000000 +0000
+++ GCC/gcc/c-common.c	2004-08-04 09:15:00.000000000 +0000
@@ -3883,6 +3883,8 @@ c_stddef_cpp_builtins(void)
   builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
   builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
   builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
+  builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
+  builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
 }
 
 static void
diff -rupN GCC.orig/gcc/c-cppbuiltin.c GCC/gcc/c-cppbuiltin.c
--- GCC.orig/gcc/c-cppbuiltin.c	2004-06-21 12:29:14.000000000 +0000
+++ GCC/gcc/c-cppbuiltin.c	2004-08-04 10:13:58.000000000 +0000
@@ -54,6 +54,7 @@ static void builtin_define_with_int_valu
 static void builtin_define_with_hex_fp_value (const char *, tree,
 					      int, const char *,
 					      const char *);
+static void builtin_define_stdint_macros (void);
 static void builtin_define_type_max (const char *, tree, int);
 static void builtin_define_type_precision (const char *, tree);
 static void builtin_define_float_constants (const char *, const char *,
@@ -293,6 +294,23 @@ define__GNUC__ (void)
     abort ();
 }
 
+/* Define macros used by <stdint.h>.  Currently only defines limits
+   for intmax_t, used by the testsuite.  */
+static void
+builtin_define_stdint_macros (void)
+{
+  int intmax_long;
+  if (intmax_type_node == long_long_integer_type_node)
+    intmax_long = 2;
+  else if (intmax_type_node == long_integer_type_node)
+    intmax_long = 1;
+  else if (intmax_type_node == integer_type_node)
+    intmax_long = 0;
+  else
+    abort ();
+  builtin_define_type_max ("__INTMAX_MAX__", intmax_type_node, intmax_long);
+}
+
 /* Hook that registers front end and target-specific built-ins.  */
 void
 c_cpp_builtins (cpp_reader *pfile)
@@ -354,6 +372,9 @@ c_cpp_builtins (cpp_reader *pfile)
 
   builtin_define_type_precision ("__CHAR_BIT__", char_type_node);
 
+  /* stdint.h (eventually) and the testsuite need to know these.  */
+  builtin_define_stdint_macros ();
+
   /* float.h needs to know these.  */
 
   builtin_define_with_int_value ("__FLT_EVAL_METHOD__",
diff -rupN GCC.orig/gcc/doc/cpp.texi GCC/gcc/doc/cpp.texi
--- GCC.orig/gcc/doc/cpp.texi	2004-07-05 08:55:06.000000000 +0000
+++ GCC/gcc/doc/cpp.texi	2004-08-04 10:14:14.000000000 +0000
@@ -2053,8 +2053,11 @@ OSF/rose @option{-mno-underscores} optio
 @itemx __PTRDIFF_TYPE__
 @itemx __WCHAR_TYPE__
 @itemx __WINT_TYPE__
+@itemx __INTMAX_TYPE__
+@itemx __UINTMAX_TYPE__
 These macros are defined to the correct underlying types for the
-@code{size_t}, @code{ptrdiff_t}, @code{wchar_t}, and @code{wint_t}
+@code{size_t}, @code{ptrdiff_t}, @code{wchar_t}, @code{wint_t},
+@code{intmax_t}, and @code{uintmax_t}
 typedefs, respectively.  They exist to make the standard header files
 @file{stddef.h} and @file{wchar.h} work correctly.  You should not use
 these macros directly; instead, include the appropriate headers and use
@@ -2072,9 +2075,11 @@ this macro directly; instead, include th
 @itemx __INT_MAX__
 @itemx __LONG_MAX__
 @itemx __LONG_LONG_MAX__
+@itemx __INTMAX_MAX__
 Defined to the maximum value of the @code{signed char}, @code{wchar_t},
 @code{signed short},
-@code{signed int}, @code{signed long}, and @code{signed long long} types
+@code{signed int}, @code{signed long}, @code{signed long long}, and
+@code{intmax_t} types
 respectively.  They exist to make the standard header given numerical limits
 work correctly.  You should not use these macros directly; instead, include
 the appropriate headers.
diff -rupN GCC.orig/gcc/testsuite/gcc.c-torture/execute/builtins/abs-2.c GCC/gcc/testsuite/gcc.c-torture/execute/builtins/abs-2.c
--- GCC.orig/gcc/testsuite/gcc.c-torture/execute/builtins/abs-2.c	2004-07-03 02:16:49.000000000 +0000
+++ GCC/gcc/testsuite/gcc.c-torture/execute/builtins/abs-2.c	2004-08-04 22:11:50.000000000 +0000
@@ -1,20 +1,9 @@
 /* Test for builtin abs, labs, llabs, imaxabs.  */
 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
 
-/* These next definitions are kludges.  When GCC has a <stdint.h> it
-   should be used.
-*/
 #include <limits.h>
-#if INT_MAX == __LONG_LONG_MAX__
-typedef int intmax_t;
-#define INTMAX_MAX INT_MAX
-#elif LONG_MAX == __LONG_LONG_MAX__
-typedef long intmax_t;
-#define INTMAX_MAX LONG_MAX
-#else
-typedef long long intmax_t;
-#define INTMAX_MAX __LONG_LONG_MAX__
-#endif
+typedef __INTMAX_TYPE__ intmax_t;
+#define INTMAX_MAX __INTMAX_MAX__
 
 extern int abs (int);
 extern long labs (long);
diff -rupN GCC.orig/gcc/testsuite/gcc.c-torture/execute/builtins/abs-3.c GCC/gcc/testsuite/gcc.c-torture/execute/builtins/abs-3.c
--- GCC.orig/gcc/testsuite/gcc.c-torture/execute/builtins/abs-3.c	2004-07-03 02:16:49.000000000 +0000
+++ GCC/gcc/testsuite/gcc.c-torture/execute/builtins/abs-3.c	2004-08-04 22:11:55.000000000 +0000
@@ -1,20 +1,9 @@
 /* Test for builtin abs, labs, llabs, imaxabs.  Test for __builtin versions. */
 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
 
-/* These next definitions are kludges.  When GCC has a <stdint.h> it
-   should be used.
-*/
 #include <limits.h>
-#if INT_MAX == __LONG_LONG_MAX__
-typedef int intmax_t;
-#define INTMAX_MAX INT_MAX
-#elif LONG_MAX == __LONG_LONG_MAX__
-typedef long intmax_t;
-#define INTMAX_MAX LONG_MAX
-#else
-typedef long long intmax_t;
-#define INTMAX_MAX __LONG_LONG_MAX__
-#endif
+typedef __INTMAX_TYPE__ intmax_t;
+#define INTMAX_MAX __INTMAX_MAX__
 
 extern void abort (void);
 extern void link_error (void);
diff -rupN GCC.orig/gcc/testsuite/gcc.c-torture/execute/builtins/lib/abs.c GCC/gcc/testsuite/gcc.c-torture/execute/builtins/lib/abs.c
--- GCC.orig/gcc/testsuite/gcc.c-torture/execute/builtins/lib/abs.c	2004-07-03 02:16:50.000000000 +0000
+++ GCC/gcc/testsuite/gcc.c-torture/execute/builtins/lib/abs.c	2004-08-04 09:27:50.000000000 +0000
@@ -6,20 +6,7 @@ extern void abort (void);
 #define ABORT_INSIDE_MAIN do { } while (0)
 #endif
 
-/* These next definitions are kludges.  When GCC has a <stdint.h> it
-   should be used.
-*/
-#include <limits.h>
-#if INT_MAX == __LONG_LONG_MAX__
-typedef int intmax_t;
-#define INTMAX_MAX INT_MAX
-#elif LONG_MAX == __LONG_LONG_MAX__
-typedef long intmax_t;
-#define INTMAX_MAX LONG_MAX
-#else
-typedef long long intmax_t;
-#define INTMAX_MAX __LONG_LONG_MAX__
-#endif
+typedef __INTMAX_TYPE__ intmax_t;
 
 int
 abs (int x)
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/format/format.h GCC/gcc/testsuite/gcc.dg/format/format.h
--- GCC.orig/gcc/testsuite/gcc.dg/format/format.h	2001-12-21 15:02:31.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/format/format.h	2004-08-04 23:34:45.000000000 +0000
@@ -28,31 +28,8 @@ __extension__ typedef unsigned long long
 typedef llong quad_t;
 typedef ullong u_quad_t;
 
-/* This next definition is a kludge.  When GCC has a <stdint.h> it
-   should be used.
-*/
-/* (T *) if E is zero, (void *) otherwise.  */
-#define type_if_not(T, E) __typeof__(0 ? (T *)0 : (void *)(E))
-
-/* (T *) if E is nonzero, (void *) otherwise.  */
-#define type_if(T, E) type_if_not(T, !(E))
-
-/* Combine pointer types, all but one (void *).  */
-#define type_comb2(T1, T2) __typeof__(0 ? (T1)0 : (T2)0)
-#define type_comb3(T1, T2, T3) type_comb2(T1, type_comb2(T2, T3))
-
-#define maybe_int_ptr type_if(int, sizeof(int) == sizeof(llong))
-#define maybe_uint_ptr type_if(unsigned int, sizeof(unsigned int) == sizeof(ullong))
-#define maybe_long_ptr type_if(long, sizeof(long) == sizeof(llong) && sizeof(long) > sizeof(int))
-#define maybe_ulong_ptr type_if(unsigned long, sizeof(unsigned long) == sizeof(ullong) && sizeof(unsigned long) > sizeof(unsigned int))
-#define maybe_long_long_ptr type_if(llong, sizeof(llong) > sizeof(long))
-#define maybe_ulong_long_ptr type_if(ullong, sizeof(ullong) > sizeof(unsigned long))
-
-#define intmax_type_ptr type_comb3(maybe_int_ptr, maybe_long_ptr, maybe_long_long_ptr)
-#define uintmax_type_ptr type_comb3(maybe_uint_ptr, maybe_ulong_ptr, maybe_ulong_long_ptr)
-
-typedef __typeof__(*((intmax_type_ptr)0)) intmax_t;
-typedef __typeof__(*((uintmax_type_ptr)0)) uintmax_t;
+__extension__ typedef __INTMAX_TYPE__ intmax_t;
+__extension__ typedef __UINTMAX_TYPE__ uintmax_t;
 
 #if __STDC_VERSION__ < 199901L
 #define restrict /* "restrict" not in old C standard.  */
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/intmax_t-1.c GCC/gcc/testsuite/gcc.dg/intmax_t-1.c
--- GCC.orig/gcc/testsuite/gcc.dg/intmax_t-1.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/intmax_t-1.c	2004-08-04 09:37:42.000000000 +0000
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+
+/* Compile with -Wall to get a warning if built-in and system intmax_t don't
+   match.  */
+
+#include <inttypes.h>
+
+__INTMAX_TYPE__ __im_t__;
+__UINTMAX_TYPE__ __uim_t__;
+intmax_t *im_t_p;
+uintmax_t *uim_t_p;
+
+void
+imt (void)
+{
+  im_t_p = &__im_t__;
+}
+
+void
+uimt (void)
+{
+  uim_t_p = &__uim_t__;
+}
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/torture/builtin-attr-1.c GCC/gcc/testsuite/gcc.dg/torture/builtin-attr-1.c
--- GCC.orig/gcc/testsuite/gcc.dg/torture/builtin-attr-1.c	2004-08-03 08:19:04.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/torture/builtin-attr-1.c	2004-08-04 09:27:20.000000000 +0000
@@ -404,16 +404,7 @@ CPTEST1 (csqrt)
 CPTEST1 (ctan)
 CPTEST1 (ctanh)
 
-/* These next definitions are kludges.  When GCC has a <stdint.h> it
-   should be used.
-*/
-#if __INT_MAX__ == __LONG_LONG_MAX__
-typedef int intmax_t;
-#elif __LONG_MAX__ == __LONG_LONG_MAX__
-typedef long intmax_t;
-#else
-typedef long long intmax_t;
-#endif
+typedef __INTMAX_TYPE__ intmax_t;
 
 /* Various other const builtins.  */
 TEST1         (abs, int, int)


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