Thanks for testing that. I replicated the problem on Solaris 10 with Sun C 5.11 and Solaris 8 with Sun C 5.8. The following patches fixed it for me. I can't easily test HP-UX and IRIX but I tried to fix all the problems I could deduce remotely. Apparently these compilers mishandle constants like 0u, so I rewrote the tests to use constant expressions like ((unsigned int) 0) instead.
>From 1039e7e89410a6cd9a68b7dee5857163eeee0adc Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Thu, 19 May 2011 01:34:14 -0700 Subject: [PATCH 1/3] intprops: TYPE_IS_INTEGER, TYPE_SIGNED not integer constant exprs * doc/intprops.texi (Integer Type Determination): Fix documentation for TYPE_IS_INTEGER: it returns an constant expression, not an integer constant expression. Fix doc for TYPE_SIGNED: it returns an integer constant expression only if its argument is an integer type. (TYPE_IS_INTEGER is the same, but is hardly worth documented that way....) --- ChangeLog | 10 ++++++++++ doc/intprops.texi | 7 ++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 04b2904..86001d5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2011-05-19 Paul Eggert <egg...@cs.ucla.edu> + + intprops: TYPE_IS_INTEGER, TYPE_SIGNED not integer constant exprs + * doc/intprops.texi (Integer Type Determination): Fix + documentation for TYPE_IS_INTEGER: it returns an constant + expression, not an integer constant expression. Fix doc for + TYPE_SIGNED: it returns an integer constant expression only if its + argument is an integer type. (TYPE_IS_INTEGER is the same, but is + hardly worth documented that way....) + 2011-05-18 Bruno Haible <br...@clisp.org> strerror_r: Avoid clobbering the strerror buffer when possible. diff --git a/doc/intprops.texi b/doc/intprops.texi index e8721d7..190afe4 100644 --- a/doc/intprops.texi +++ b/doc/intprops.texi @@ -57,14 +57,15 @@ while the second, for integer types, is easier to use. @subsection Integer Type Determination @findex TYPE_IS_INTEGER -@code{TYPE_IS_INTEGER (@var{t})} expands to an integer constant +@code{TYPE_IS_INTEGER (@var{t})} expands to a constant expression that is 1 if the arithmetic type @var{t} is a integer type. @code{_Bool} counts as an integer type. @findex TYPE_SIGNED -@code{TYPE_SIGNED (@var{t})} expands to an integer constant expression +@code{TYPE_SIGNED (@var{t})} expands to a constant expression that is 1 if the arithmetic type @var{t} is a signed integer type or a -floating type. +floating type. If @var{t} is an integer type, @code{TYPE_SIGNED (@var{t})} +expands to an integer constant expression. Example usage: -- 1.7.5.1 >From 726e8f9cfeefa95beef26cb41330880759776968 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Thu, 19 May 2011 01:36:25 -0700 Subject: [PATCH 2/3] intprops: work around C compiler bugs * lib/intprops.h (INT_MULTIPLY_RANGE_OVERFLOW): Work around compiler bug in Sun C 5.11 2010/08/13 and other compilers; see <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. --- ChangeLog | 5 +++++ lib/intprops.h | 17 +++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 86001d5..d12db03 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2011-05-19 Paul Eggert <egg...@cs.ucla.edu> + intprops: work around C compiler bugs + * lib/intprops.h (INT_MULTIPLY_RANGE_OVERFLOW): Work around compiler + bug in Sun C 5.11 2010/08/13 and other compilers; see + <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. + intprops: TYPE_IS_INTEGER, TYPE_SIGNED not integer constant exprs * doc/intprops.texi (Integer Type Determination): Fix documentation for TYPE_IS_INTEGER: it returns an constant diff --git a/lib/intprops.h b/lib/intprops.h index a84bd6a..9107a4b 100644 --- a/lib/intprops.h +++ b/lib/intprops.h @@ -179,16 +179,21 @@ : 0 < (a)) /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. - See above for restrictions. */ + See above for restrictions. Avoid && and || as they tickle + bugs in Sun C 5.11 2010/08/13 and other compilers; see + <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */ #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \ ((b) < 0 \ ? ((a) < 0 \ ? (a) < (max) / (b) \ - : (b) < -1 && (min) / (b) < (a)) \ - : (0 < (b) \ - && ((a) < 0 \ - ? (a) < (min) / (b) \ - : (max) / (b) < (a)))) + : (b) == -1 \ + ? 0 \ + : (min) / (b) < (a)) \ + : (b) == 0 \ + ? 0 \ + : ((a) < 0 \ + ? (a) < (min) / (b) \ + : (max) / (b) < (a))) /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic. See above for restrictions. Do not check for division by zero. */ -- 1.7.5.1 >From e9d47071a68819604a698a9ad5f188472e1c3792 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Thu, 19 May 2011 01:43:17 -0700 Subject: [PATCH 3/3] intprop-tests: port to older and more-pedantic compilers * modules/intprops-tests (Files): Add tests/macros.h. * tests/test-intprops.c: Include macros.h. (TYPE_IS_INTEGER): Use ASSERT, not verify, to test this macro, as it's no longer documented to expand to an integer constant expression. (TYPE_SIGNED): Use ASSERT, not verify, to test this macro when the argument is floating point, as it's no longer documented to expand to an integer constant expression in that case. (UINT_MAX, ULONG_MAX, UINTMAX_MAX): Redefine to work around compiler bugs reported by Bruno Haible. See <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. (U0, U1): New constants, to work around the same bugs. Also, in tests, use e.g., "(unsigned int) 39" rather than "39u". --- ChangeLog | 14 +++++ modules/intprops-tests | 1 + tests/test-intprops.c | 146 +++++++++++++++++++++++++++--------------------- 3 files changed, 97 insertions(+), 64 deletions(-) diff --git a/ChangeLog b/ChangeLog index d12db03..b21dc1b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,19 @@ 2011-05-19 Paul Eggert <egg...@cs.ucla.edu> + intprop-tests: port to older and more-pedantic compilers + * modules/intprops-tests (Files): Add tests/macros.h. + * tests/test-intprops.c: Include macros.h. + (TYPE_IS_INTEGER): Use ASSERT, not verify, to test this macro, as + it's no longer documented to expand to an integer constant expression. + (TYPE_SIGNED): Use ASSERT, not verify, to test this macro when the + argument is floating point, as it's no longer documented to expand + to an integer constant expression in that case. + (UINT_MAX, ULONG_MAX, UINTMAX_MAX): Redefine to work around + compiler bugs reported by Bruno Haible. See + <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. + (U0, U1): New constants, to work around the same bugs. Also, + in tests, use e.g., "(unsigned int) 39" rather than "39u". + intprops: work around C compiler bugs * lib/intprops.h (INT_MULTIPLY_RANGE_OVERFLOW): Work around compiler bug in Sun C 5.11 2010/08/13 and other compilers; see diff --git a/modules/intprops-tests b/modules/intprops-tests index 4aa04d9..f02b357 100644 --- a/modules/intprops-tests +++ b/modules/intprops-tests @@ -1,5 +1,6 @@ Files: tests/test-intprops.c +tests/macros.h Depends-on: inttypes diff --git a/tests/test-intprops.c b/tests/test-intprops.c index aae2614..9c2fe07 100644 --- a/tests/test-intprops.c +++ b/tests/test-intprops.c @@ -24,22 +24,19 @@ #include <stdbool.h> #include <inttypes.h> -/* TYPE_IS_INTEGER. */ -verify (TYPE_IS_INTEGER (bool)); -verify (TYPE_IS_INTEGER (char)); -verify (TYPE_IS_INTEGER (signed char)); -verify (TYPE_IS_INTEGER (unsigned char)); -verify (TYPE_IS_INTEGER (short int)); -verify (TYPE_IS_INTEGER (unsigned short int)); -verify (TYPE_IS_INTEGER (int)); -verify (TYPE_IS_INTEGER (unsigned int)); -verify (TYPE_IS_INTEGER (long int)); -verify (TYPE_IS_INTEGER (unsigned long int)); -verify (TYPE_IS_INTEGER (intmax_t)); -verify (TYPE_IS_INTEGER (uintmax_t)); -verify (! TYPE_IS_INTEGER (float)); -verify (! TYPE_IS_INTEGER (double)); -verify (! TYPE_IS_INTEGER (long double)); +#include "macros.h" + +/* Work around compiler bugs in HP-UX 11.23 cc, IRIX 6.5 cc, OSF/1 5.1 + cc, and Solaris 9 cc. See + <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */ +#undef UINT_MAX +#undef ULONG_MAX +#undef UINTMAX_MAX +#define UINT_MAX ((unsigned int) -1) +#define ULONG_MAX ((unsigned long int) -1) +#define UINTMAX_MAX ((uintmax_t) -1) +#define U0 ((unsigned int) 0) +#define U1 ((unsigned int) 1) /* Integer representation. */ verify (INT_MIN + INT_MAX < 0 @@ -60,9 +57,6 @@ verify (TYPE_SIGNED (long int)); verify (! TYPE_SIGNED (unsigned long int)); verify (TYPE_SIGNED (intmax_t)); verify (! TYPE_SIGNED (uintmax_t)); -verify (TYPE_SIGNED (float)); -verify (TYPE_SIGNED (double)); -verify (TYPE_SIGNED (long double)); /* TYPE_MINIMUM, TYPE_MAXIMUM. */ verify (TYPE_MINIMUM (char) == CHAR_MIN); @@ -118,23 +112,23 @@ verify (check_binop (ADD, INT_MAX, 1, INT_MIN, INT_MAX, true)); verify (check_binop (ADD, INT_MAX, -1, INT_MIN, INT_MAX, false)); verify (check_binop (ADD, INT_MIN, 1, INT_MIN, INT_MAX, false)); verify (check_binop (ADD, INT_MIN, -1, INT_MIN, INT_MAX, true)); -verify (check_binop (ADD, UINT_MAX, 1u, 0u, UINT_MAX, true)); -verify (check_binop (ADD, 0u, 1u, 0u, UINT_MAX, false)); +verify (check_binop (ADD, UINT_MAX, U1, U0, UINT_MAX, true)); +verify (check_binop (ADD, U0, U1, U0, UINT_MAX, false)); verify (check_binop (SUBTRACT, INT_MAX, 1, INT_MIN, INT_MAX, false)); verify (check_binop (SUBTRACT, INT_MAX, -1, INT_MIN, INT_MAX, true)); verify (check_binop (SUBTRACT, INT_MIN, 1, INT_MIN, INT_MAX, true)); verify (check_binop (SUBTRACT, INT_MIN, -1, INT_MIN, INT_MAX, false)); -verify (check_binop (SUBTRACT, UINT_MAX, 1u, 0u, UINT_MAX, false)); -verify (check_binop (SUBTRACT, 0u, 1u, 0u, UINT_MAX, true)); +verify (check_binop (SUBTRACT, UINT_MAX, U1, U0, UINT_MAX, false)); +verify (check_binop (SUBTRACT, U0, U1, U0, UINT_MAX, true)); verify (check_unop (NEGATE, INT_MIN, INT_MIN, INT_MAX, TYPE_TWOS_COMPLEMENT (int))); verify (check_unop (NEGATE, 0, INT_MIN, INT_MAX, false)); verify (check_unop (NEGATE, INT_MAX, INT_MIN, INT_MAX, false)); -verify (check_unop (NEGATE, 0u, 0u, UINT_MAX, false)); -verify (check_unop (NEGATE, 1u, 0u, UINT_MAX, true)); -verify (check_unop (NEGATE, UINT_MAX, 0u, UINT_MAX, true)); +verify (check_unop (NEGATE, U0, U0, UINT_MAX, false)); +verify (check_unop (NEGATE, U1, U0, UINT_MAX, true)); +verify (check_unop (NEGATE, UINT_MAX, U0, UINT_MAX, true)); verify (check_binop (MULTIPLY, INT_MAX, INT_MAX, INT_MIN, INT_MAX, true)); verify (check_binop (MULTIPLY, INT_MAX, INT_MIN, INT_MIN, INT_MAX, true)); @@ -149,17 +143,17 @@ verify (check_binop (DIVIDE, INT_MIN, -1, INT_MIN, INT_MAX, INT_NEGATE_OVERFLOW (INT_MIN))); verify (check_binop (DIVIDE, INT_MAX, 1, INT_MIN, INT_MAX, false)); verify (check_binop (DIVIDE, (unsigned int) INT_MIN, - -1u, 0u, UINT_MAX, false)); + -U1, U0, UINT_MAX, false)); verify (check_binop (REMAINDER, INT_MIN, -1, INT_MIN, INT_MAX, INT_NEGATE_OVERFLOW (INT_MIN))); verify (check_binop (REMAINDER, INT_MAX, 1, INT_MIN, INT_MAX, false)); verify (check_binop (REMAINDER, (unsigned int) INT_MIN, - -1u, 0u, UINT_MAX, false)); + -U1, U0, UINT_MAX, false)); -verify (check_binop (LEFT_SHIFT, UINT_MAX, 1, 0u, UINT_MAX, true)); -verify (check_binop (LEFT_SHIFT, UINT_MAX / 2 + 1, 1, 0u, UINT_MAX, true)); -verify (check_binop (LEFT_SHIFT, UINT_MAX / 2, 1, 0u, UINT_MAX, false)); +verify (check_binop (LEFT_SHIFT, UINT_MAX, 1, U0, UINT_MAX, true)); +verify (check_binop (LEFT_SHIFT, UINT_MAX / 2 + 1, 1, U0, UINT_MAX, true)); +verify (check_binop (LEFT_SHIFT, UINT_MAX / 2, 1, U0, UINT_MAX, false)); /* INT_<op>_OVERFLOW with mixed types. */ #define check_sum(a, b, overflow) \ @@ -167,51 +161,51 @@ verify (check_binop (LEFT_SHIFT, UINT_MAX / 2, 1, 0u, UINT_MAX, false)); verify (INT_ADD_OVERFLOW (b, a) == (overflow)) check_sum (-1, LONG_MIN, true); check_sum (-1, UINT_MAX, false); -check_sum (-1L, INT_MIN, INT_MIN == LONG_MIN); -check_sum (0u, -1, true); -check_sum (0u, 0, false); -check_sum (0u, 1, false); +check_sum (- (long) 1, INT_MIN, INT_MIN == LONG_MIN); +check_sum (U0, -1, true); +check_sum (U0, 0, false); +check_sum (U0, 1, false); check_sum (1, LONG_MAX, true); check_sum (1, UINT_MAX, true); -check_sum (1L, INT_MAX, INT_MAX == LONG_MAX); -check_sum (1u, INT_MAX, INT_MAX == UINT_MAX); -check_sum (1u, INT_MIN, true); +check_sum ((long) 1, INT_MAX, INT_MAX == LONG_MAX); +check_sum (U1, INT_MAX, INT_MAX == UINT_MAX); +check_sum (U1, INT_MIN, true); -verify (! INT_SUBTRACT_OVERFLOW (INT_MAX, 1u)); +verify (! INT_SUBTRACT_OVERFLOW (INT_MAX, U1)); verify (! INT_SUBTRACT_OVERFLOW (UINT_MAX, 1)); -verify (! INT_SUBTRACT_OVERFLOW (0u, -1)); +verify (! INT_SUBTRACT_OVERFLOW (U0, -1)); verify (INT_SUBTRACT_OVERFLOW (UINT_MAX, -1)); -verify (INT_SUBTRACT_OVERFLOW (INT_MIN, 1u)); -verify (INT_SUBTRACT_OVERFLOW (-1, 0u)); +verify (INT_SUBTRACT_OVERFLOW (INT_MIN, U1)); +verify (INT_SUBTRACT_OVERFLOW (-1, U0)); #define check_product(a, b, overflow) \ verify (INT_MULTIPLY_OVERFLOW (a, b) == (overflow)); \ verify (INT_MULTIPLY_OVERFLOW (b, a) == (overflow)) -check_product (-1, 1u, true); +check_product (-1, U1, true); check_product (-1, INT_MIN, INT_NEGATE_OVERFLOW (INT_MIN)); check_product (-1, UINT_MAX, true); check_product (-12345, LONG_MAX / -12345 - 1, true); check_product (-12345, LONG_MAX / -12345, false); check_product (0, -1, false); check_product (0, 0, false); -check_product (0, 0u, false); +check_product (0, U0, false); check_product (0, 1, false); check_product (0, INT_MAX, false); check_product (0, INT_MIN, false); check_product (0, UINT_MAX, false); -check_product (0u, -1, false); -check_product (0u, 0, false); -check_product (0u, 0u, false); -check_product (0u, 1, false); -check_product (0u, INT_MAX, false); -check_product (0u, INT_MIN, false); -check_product (0u, UINT_MAX, false); +check_product (U0, -1, false); +check_product (U0, 0, false); +check_product (U0, U0, false); +check_product (U0, 1, false); +check_product (U0, INT_MAX, false); +check_product (U0, INT_MIN, false); +check_product (U0, UINT_MAX, false); check_product (1, INT_MAX, false); check_product (1, INT_MIN, false); check_product (1, UINT_MAX, false); -check_product (1u, INT_MIN, true); -check_product (1u, INT_MAX, UINT_MAX < INT_MAX); +check_product (U1, INT_MIN, true); +check_product (U1, INT_MAX, UINT_MAX < INT_MAX); check_product (INT_MAX, UINT_MAX, true); check_product (INT_MAX, ULONG_MAX, true); check_product (INT_MIN, LONG_MAX / INT_MIN - 1, true); @@ -219,19 +213,19 @@ check_product (INT_MIN, LONG_MAX / INT_MIN, false); check_product (INT_MIN, UINT_MAX, true); check_product (INT_MIN, ULONG_MAX, true); -verify (INT_DIVIDE_OVERFLOW (INT_MIN, -1L) +verify (INT_DIVIDE_OVERFLOW (INT_MIN, - (long) 1) == (TYPE_TWOS_COMPLEMENT (long int) && INT_MIN == LONG_MIN)); verify (! INT_DIVIDE_OVERFLOW (INT_MIN, UINT_MAX)); verify (! INT_DIVIDE_OVERFLOW (INTMAX_MIN, UINTMAX_MAX)); verify (! INT_DIVIDE_OVERFLOW (INTMAX_MIN, UINT_MAX)); -verify (INT_DIVIDE_OVERFLOW (-11, 10u)); -verify (INT_DIVIDE_OVERFLOW (-10, 10u)); -verify (! INT_DIVIDE_OVERFLOW (-9, 10u)); -verify (INT_DIVIDE_OVERFLOW (11u, -10)); -verify (INT_DIVIDE_OVERFLOW (10u, -10)); +verify (INT_DIVIDE_OVERFLOW (-11, (unsigned int) 10)); +verify (INT_DIVIDE_OVERFLOW (-10, (unsigned int) 10)); +verify (! INT_DIVIDE_OVERFLOW (-9, (unsigned int) 10)); +verify (INT_DIVIDE_OVERFLOW ((unsigned int) 11, -10)); +verify (INT_DIVIDE_OVERFLOW ((unsigned int) 10, -10)); verify (! INT_DIVIDE_OVERFLOW (9u, -10)); -verify (INT_REMAINDER_OVERFLOW (INT_MIN, -1L) +verify (INT_REMAINDER_OVERFLOW (INT_MIN, - (long) 1) == (TYPE_TWOS_COMPLEMENT (long int) && INT_MIN == LONG_MIN)); verify (INT_REMAINDER_OVERFLOW (-1, UINT_MAX)); verify (INT_REMAINDER_OVERFLOW ((intmax_t) -1, UINTMAX_MAX)); @@ -240,14 +234,38 @@ verify (INT_REMAINDER_OVERFLOW (INTMAX_MIN, UINT_MAX) && - (unsigned int) INTMAX_MIN % UINT_MAX != 0)); verify (INT_REMAINDER_OVERFLOW (INT_MIN, ULONG_MAX) == (INT_MIN % ULONG_MAX != 1)); -verify (! INT_REMAINDER_OVERFLOW (1u, -1)); -verify (! INT_REMAINDER_OVERFLOW (37*39u, -39)); -verify (INT_REMAINDER_OVERFLOW (37*39u + 1, -39)); -verify (INT_REMAINDER_OVERFLOW (37*39u - 1, -39)); +verify (! INT_REMAINDER_OVERFLOW (U1, -1)); +verify (! INT_REMAINDER_OVERFLOW (37 * (unsigned int) 39, -39)); +verify (INT_REMAINDER_OVERFLOW (37 * (unsigned int) 39 + 1, -39)); +verify (INT_REMAINDER_OVERFLOW (37 * (unsigned int) 39 - 1, -39)); verify (! INT_REMAINDER_OVERFLOW (LONG_MAX, -INT_MAX)); int main (void) { + /* Test expressions that might not be integer constant expressions. */ + + /* TYPE_IS_INTEGER. */ + ASSERT (TYPE_IS_INTEGER (bool)); + ASSERT (TYPE_IS_INTEGER (char)); + ASSERT (TYPE_IS_INTEGER (signed char)); + ASSERT (TYPE_IS_INTEGER (unsigned char)); + ASSERT (TYPE_IS_INTEGER (short int)); + ASSERT (TYPE_IS_INTEGER (unsigned short int)); + ASSERT (TYPE_IS_INTEGER (int)); + ASSERT (TYPE_IS_INTEGER (unsigned int)); + ASSERT (TYPE_IS_INTEGER (long int)); + ASSERT (TYPE_IS_INTEGER (unsigned long int)); + ASSERT (TYPE_IS_INTEGER (intmax_t)); + ASSERT (TYPE_IS_INTEGER (uintmax_t)); + ASSERT (! TYPE_IS_INTEGER (float)); + ASSERT (! TYPE_IS_INTEGER (double)); + ASSERT (! TYPE_IS_INTEGER (long double)); + + /* TYPE_SIGNED. */ + ASSERT (TYPE_SIGNED (float)); + ASSERT (TYPE_SIGNED (double)); + ASSERT (TYPE_SIGNED (long double)); + return 0; } -- 1.7.5.1