diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 1afc85d1a6c0cde927e55ecf9b4657f80d109695..0f5964196cef0741497c17c7253299dbab2b77c7 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,13 @@ +2007-01-24 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__, __SIZEOF_POINTER__, + __SIZEOF_FLOAT__, __SIZEOF_DOUBLE__, __SIZEOF_LONG_DOUBLE__, + __SIZEOF_SIZE_T__, __SIZEOF_WCHAR_T__, __SIZEOF_WINT_T__ and + __SIZEOF_PTRDIFF_T__. + * doc/cpp.texi: Documentation for the new builtin macros added. + 2007-01-24 Uros Bizjak <ubizjak@gmail.com> * config/i386/i386.md (tanxf2, tan<mode>2, atan<mode>2, log<mode>2, diff --git a/gcc/c-cppbuiltin.c b/gcc/c-cppbuiltin.c index 4eca2d4674f5385fc4fad5ff64e708a2f7e0f513..729cb0b7ed97f337b42cc1da20c0382f1d0e7ee3 100644 --- a/gcc/c-cppbuiltin.c +++ b/gcc/c-cppbuiltin.c @@ -58,6 +58,7 @@ static void builtin_define_with_hex_fp_value (const char *, tree, 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 *, @@ -71,6 +72,14 @@ builtin_define_type_precision (const char *name, tree type) 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_low_cst (TYPE_SIZE_UNIT (type), 1)); +} + /* Define the float.h constants for TYPE using NAME_PREFIX, FP_SUFFIX, and FP_CAST. */ static void @@ -549,6 +558,24 @@ c_cpp_builtins (cpp_reader *pfile) 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); + builtin_define_type_sizeof ("__SIZEOF_FLOAT__", float_type_node); + builtin_define_type_sizeof ("__SIZEOF_DOUBLE__", double_type_node); + builtin_define_type_sizeof ("__SIZEOF_LONG_DOUBLE__", long_double_type_node); + builtin_define_type_sizeof ("__SIZEOF_SIZE_T__", size_type_node); + builtin_define_type_sizeof ("__SIZEOF_WCHAR_T__", wchar_type_node); + builtin_define_type_sizeof ("__SIZEOF_WINT_T__", wint_type_node); + builtin_define_type_sizeof ("__SIZEOF_PTRDIFF_T__", + unsigned_ptrdiff_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_POINTER__", + 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) diff --git a/gcc/doc/cpp.texi b/gcc/doc/cpp.texi index 1b7b86700861d325723a586593f958426d94e9e2..90ff2304f7b757769b9613dc1857bef2a5bfa5d5 100644 --- a/gcc/doc/cpp.texi +++ b/gcc/doc/cpp.texi @@ -2079,6 +2079,23 @@ 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. +@item __SIZEOF_INT__ +@itemx __SIZEOF_LONG__ +@itemx __SIZEOF_LONG_LONG__ +@itemx __SIZEOF_SHORT__ +@itemx __SIZEOF_POINTER__ +@itemx __SIZEOF_FLOAT__ +@itemx __SIZEOF_DOUBLE__ +@itemx __SIZEOF_LONG_DOUBLE__ +@itemx __SIZEOF_SIZE_T__ +@itemx __SIZEOF_WCHAR_T__ +@itemx __SIZEOF_WINT_T__ +@itemx __SIZEOF_PTRDIFF_T__ +Defined to the number of bytes of the C standard data types: @code{int}, +@code{long}, @code{long long}, @code{short}, @code{void *}, @code{float}, +@code{double}, @code{long double}, @code{size_t}, @code{wchar_t}, @code{wint_t} +and @code{ptrdiff_t}. + @item __DEPRECATED This macro is defined, with value 1, when compiling a C++ source file with warnings about deprecated constructs enabled. These warnings are diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 10cc9718c3900b8a06dd9d6042b9b8f00580a76c..65fbbfb829570eb9717b156e34c7a2de0e266e2d 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2007-01-24 Andreas Krebbel <krebbel1@de.ibm.com> + + * gcc.c-torture/compile/sizeof-macros-1.c: New testcase. + 2007-01-23 Andrew Pinski <pinskia@gmail.com> PR objc/27438 diff --git a/gcc/testsuite/gcc.c-torture/compile/sizeof-macros-1.c b/gcc/testsuite/gcc.c-torture/compile/sizeof-macros-1.c new file mode 100644 index 0000000000000000000000000000000000000000..80618cc8dccd4e40eba492931cba992b5d61034c --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/sizeof-macros-1.c @@ -0,0 +1,15 @@ +/* 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_POINTER__ ? 1 : -1]; +int f[sizeof(float) == __SIZEOF_FLOAT__ ? 1 : -1]; +int g[sizeof(double) == __SIZEOF_DOUBLE__ ? 1 : -1]; +int h[sizeof(long double) == __SIZEOF_LONG_DOUBLE__ ? 1 : -1]; +int i[sizeof(__SIZE_TYPE__) == __SIZEOF_SIZE_T__ ? 1 : -1]; +int j[sizeof(__WCHAR_TYPE__) == __SIZEOF_WCHAR_T__ ? 1 : -1]; +int k[sizeof(__WINT_TYPE__) == __SIZEOF_WINT_T__ ? 1 : -1]; +int l[sizeof(__PTRDIFF_TYPE__) == __SIZEOF_PTRDIFF_T__ ? 1 : -1];