This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [RFC,PATCH] Builtin macros for sizes of standard types
Hi,
here is the revised version. I've tried to address everything you mentioned.
Re-tested on i686 and s390x.
OK for mainline?
Bye,
-Andreas-
2007-01-11 Andreas Krebbel <krebbel1@de.ibm.com>
* c-cppbuiltin.c (builtin_define_type_sizeof): New function.
(c_cpp_builtins): New builtin macros: __SIZEOF_INT__, __SIZEOF_LONG__,
__SIZEOF_LONG_LONG__, __SIZEOF_SHORT__ and __SIZEOF_VOID_P__.
* doc/cpp.texi: Documentation for the new builtin macros added.
2007-01-11 Andreas Krebbel <krebbel1@de.ibm.com>
* gcc.c-torture/compile/sizeof-macros-1.c: New testcase.
Index: gcc/c-cppbuiltin.c
===================================================================
*** gcc/c-cppbuiltin.c.orig 2007-01-08 08:22:07.000000000 +0100
--- gcc/c-cppbuiltin.c 2007-01-11 09:46:11.000000000 +0100
*************** static void builtin_define_with_hex_fp_v
*** 58,63 ****
--- 58,64 ----
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_type_sizeof (const char *, tree);
static void builtin_define_float_constants (const char *,
const char *,
const char *,
*************** builtin_define_type_precision (const cha
*** 71,76 ****
--- 72,85 ----
builtin_define_with_int_value (name, TYPE_PRECISION (type));
}
+ /* Define NAME with value TYPE size_unit. */
+ static void
+ builtin_define_type_sizeof (const char *name, tree type)
+ {
+ builtin_define_with_int_value (name,
+ TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type)));
+ }
+
/* Define the float.h constants for TYPE using NAME_PREFIX, FP_SUFFIX,
and FP_CAST. */
static void
*************** c_cpp_builtins (cpp_reader *pfile)
*** 549,554 ****
--- 558,574 ----
if (flag_openmp)
cpp_define (pfile, "_OPENMP=200505");
+ builtin_define_type_sizeof ("__SIZEOF_INT__", integer_type_node);
+ builtin_define_type_sizeof ("__SIZEOF_LONG__", long_integer_type_node);
+ builtin_define_type_sizeof ("__SIZEOF_LONG_LONG__",
+ long_long_integer_type_node);
+ builtin_define_type_sizeof ("__SIZEOF_SHORT__", short_integer_type_node);
+
+ /* ptr_type_node can't be used here since ptr_mode is only set when
+ toplev calls backend_init which is not done with -E switch. */
+ builtin_define_with_int_value ("__SIZEOF_VOID_P__",
+ POINTER_SIZE / BITS_PER_UNIT);
+
/* A straightforward target hook doesn't work, because of problems
linking that hook's body when part of non-C front ends. */
# define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
Index: gcc/doc/cpp.texi
===================================================================
*** gcc/doc/cpp.texi.orig 2007-01-08 08:21:52.000000000 +0100
--- gcc/doc/cpp.texi 2007-01-11 09:47:42.000000000 +0100
*************** respectively. They exist to make the st
*** 2079,2084 ****
--- 2079,2092 ----
work correctly. You should not use these macros directly; instead, include
the appropriate headers.
+ @item __SIZEOF_INT__
+ @itemx __SIZEOF_LONG__
+ @itemx __SIZEOF_LONG_LONG__
+ @itemx __SIZEOF_SHORT__
+ @itemx __SIZEOF_VOID_P__
+ Defined to the number of bytes of the C standard data types: @code{int},
+ @code{long}, @code{long long}, @code{short} and @code{void *}.
+
@item __DEPRECATED
This macro is defined, with value 1, when compiling a C++ source file
with warnings about deprecated constructs enabled. These warnings are
Index: gcc/testsuite/gcc.c-torture/compile/sizeof-macros-1.c
===================================================================
*** /dev/null 1970-01-01 00:00:00.000000000 +0000
--- gcc/testsuite/gcc.c-torture/compile/sizeof-macros-1.c 2007-01-11 09:53:46.000000000 +0100
***************
*** 0 ****
--- 1,8 ----
+ /* This checks the gcc builtin macros defined to the byte
+ sizes of C standard types. */
+
+ int a[sizeof(int) == __SIZEOF_INT__ ? 1 : -1];
+ int b[sizeof(long) == __SIZEOF_LONG__ ? 1 : -1];
+ int c[sizeof(long long) == __SIZEOF_LONG_LONG__ ? 1 : -1];
+ int d[sizeof(short) == __SIZEOF_SHORT__ ? 1 : -1];
+ int e[sizeof(void *) == __SIZEOF_VOID_P__ ? 1 : -1];