aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert2019-08-14 18:13:27 -0700
committerPaul Eggert2019-08-14 18:13:54 -0700
commitaa1411b20fba73ca6fde90fc9ce62cc8a854bf20 (patch)
treed390dc726f47658c684d3b2cc1e1b6137b198c16 /lib
parent370f07046b13035948655d450ed1b58d20a0cdd4 (diff)
downloademacs-aa1411b20fba73ca6fde90fc9ce62cc8a854bf20.tar.gz
emacs-aa1411b20fba73ca6fde90fc9ce62cc8a854bf20.zip
Update from Gnulib
This incorporates: 2019-08-14 intprops: pacify picky GCC 2019-08-14 intprops: support unsigned *_WRAPV results 2019-08-12 verify: improve diagnostic quality in recent GCC * lib/intprops.h, lib/verify.h: Copy from Gnulib.
Diffstat (limited to 'lib')
-rw-r--r--lib/intprops.h156
-rw-r--r--lib/verify.h33
2 files changed, 147 insertions, 42 deletions
diff --git a/lib/intprops.h b/lib/intprops.h
index 1a44ae55653..d1785ac6f16 100644
--- a/lib/intprops.h
+++ b/lib/intprops.h
@@ -111,8 +111,8 @@
111 Subtract 1 for the sign bit if T is signed, and then add 1 more for 111 Subtract 1 for the sign bit if T is signed, and then add 1 more for
112 a minus sign if needed. 112 a minus sign if needed.
113 113
114 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is 114 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 1 when its argument is
115 signed, this macro may overestimate the true bound by one byte when 115 unsigned, this macro may overestimate the true bound by one byte when
116 applied to unsigned types of size 2, 4, 16, ... bytes. */ 116 applied to unsigned types of size 2, 4, 16, ... bytes. */
117#define INT_STRLEN_BOUND(t) \ 117#define INT_STRLEN_BOUND(t) \
118 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \ 118 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
@@ -281,7 +281,9 @@
281 281
282 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators 282 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
283 might not yield numerically correct answers due to arithmetic overflow. 283 might not yield numerically correct answers due to arithmetic overflow.
284 The INT_<op>_WRAPV macros also store the low-order bits of the answer. 284 The INT_<op>_WRAPV macros compute the low-order bits of the sum,
285 difference, and product of two C integers, and return 1 if these
286 low-order bits are not numerically correct.
285 These macros work correctly on all known practical hosts, and do not rely 287 These macros work correctly on all known practical hosts, and do not rely
286 on undefined behavior due to signed arithmetic overflow. 288 on undefined behavior due to signed arithmetic overflow.
287 289
@@ -309,9 +311,12 @@
309 arguments should not have side effects. 311 arguments should not have side effects.
310 312
311 The WRAPV macros are not constant expressions. They support only 313 The WRAPV macros are not constant expressions. They support only
312 +, binary -, and *. The result type must be signed. 314 +, binary -, and *. The result type must be either signed, or an
315 unsigned type that is 'unsigned int' or wider. Because the WRAPV
316 macros convert the result, the report overflow in different
317 circumstances than the OVERFLOW macros do.
313 318
314 These macros are tuned for their last argument being a constant. 319 These macros are tuned for their last input argument being a constant.
315 320
316 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, 321 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
317 A % B, and A << B would overflow, respectively. */ 322 A % B, and A << B would overflow, respectively. */
@@ -348,11 +353,21 @@
348/* Store the low-order bits of A + B, A - B, A * B, respectively, into *R. 353/* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
349 Return 1 if the result overflows. See above for restrictions. */ 354 Return 1 if the result overflows. See above for restrictions. */
350#define INT_ADD_WRAPV(a, b, r) \ 355#define INT_ADD_WRAPV(a, b, r) \
351 _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, INT_ADD_OVERFLOW) 356 _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, \
357 _GL_INT_ADD_RANGE_OVERFLOW)
352#define INT_SUBTRACT_WRAPV(a, b, r) \ 358#define INT_SUBTRACT_WRAPV(a, b, r) \
353 _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW) 359 _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, \
360 _GL_INT_SUBTRACT_RANGE_OVERFLOW)
354#define INT_MULTIPLY_WRAPV(a, b, r) \ 361#define INT_MULTIPLY_WRAPV(a, b, r) \
355 _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW) 362 _GL_INT_OP_WRAPV (a, b, r, *, _GL_BUILTIN_MUL_OVERFLOW, \
363 _GL_INT_MULTIPLY_RANGE_OVERFLOW)
364
365/* Like __builtin_mul_overflow, but work around GCC bug 91450. */
366#define _GL_BUILTIN_MUL_OVERFLOW(a, b, r) \
367 ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && EXPR_SIGNED (a) && EXPR_SIGNED (b) \
368 && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \
369 ? ((void) __builtin_mul_overflow (a, b, r), 1) \
370 : __builtin_mul_overflow (a, b, r))
356 371
357/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See: 372/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
358 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193 373 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
@@ -379,41 +394,79 @@
379 signed char: \ 394 signed char: \
380 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 395 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
381 signed char, SCHAR_MIN, SCHAR_MAX), \ 396 signed char, SCHAR_MIN, SCHAR_MAX), \
397 unsigned char: \
398 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
399 unsigned char, 0, UCHAR_MAX), \
382 short int: \ 400 short int: \
383 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 401 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
384 short int, SHRT_MIN, SHRT_MAX), \ 402 short int, SHRT_MIN, SHRT_MAX), \
403 unsigned short int: \
404 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
405 unsigned short int, 0, USHRT_MAX), \
385 int: \ 406 int: \
386 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 407 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
387 int, INT_MIN, INT_MAX), \ 408 int, INT_MIN, INT_MAX), \
409 unsigned int: \
410 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
411 unsigned int, 0, UINT_MAX), \
388 long int: \ 412 long int: \
389 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ 413 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
390 long int, LONG_MIN, LONG_MAX), \ 414 long int, LONG_MIN, LONG_MAX), \
415 unsigned long int: \
416 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
417 unsigned long int, 0, ULONG_MAX), \
391 long long int: \ 418 long long int: \
392 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ 419 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
393 long long int, LLONG_MIN, LLONG_MAX))) 420 long long int, LLONG_MIN, LLONG_MAX),
421 unsigned long long int: \
422 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
423 unsigned long long int, ULLONG_MIN, ULLONG_MAX)))
394#else 424#else
425/* This fallback implementation uses _GL_SIGNED_TYPE_OR_EXPR, and so
426 may guess wrong on some non-GNU pre-C11 compilers when the type of
427 *R is unsigned char or unsigned short. This is why the
428 documentation for INT_ADD_WRAPV says that the result type, if
429 unsigned, should be unsigned int or wider. */
395# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \ 430# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
396 (sizeof *(r) == sizeof (signed char) \ 431 (sizeof *(r) == sizeof (signed char) \
397 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 432 ? (_GL_SIGNED_TYPE_OR_EXPR (*(r)) \
398 signed char, SCHAR_MIN, SCHAR_MAX) \ 433 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
434 signed char, SCHAR_MIN, SCHAR_MAX) \
435 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
436 unsigned char, 0, UCHAR_MAX)) \
399 : sizeof *(r) == sizeof (short int) \ 437 : sizeof *(r) == sizeof (short int) \
400 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 438 ? (_GL_SIGNED_TYPE_OR_EXPR (*(r)) \
401 short int, SHRT_MIN, SHRT_MAX) \ 439 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
440 short int, SHRT_MIN, SHRT_MAX) \
441 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
442 unsigned short int, 0, USHRT_MAX)) \
402 : sizeof *(r) == sizeof (int) \ 443 : sizeof *(r) == sizeof (int) \
403 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 444 ? (EXPR_SIGNED (*(r)) \
404 int, INT_MIN, INT_MAX) \ 445 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
446 int, INT_MIN, INT_MAX) \
447 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
448 unsigned int, 0, UINT_MAX)) \
405 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow)) 449 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
406# ifdef LLONG_MAX 450# ifdef LLONG_MAX
407# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ 451# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
408 (sizeof *(r) == sizeof (long int) \ 452 (sizeof *(r) == sizeof (long int) \
409 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ 453 ? (EXPR_SIGNED (*(r)) \
410 long int, LONG_MIN, LONG_MAX) \ 454 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
411 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ 455 long int, LONG_MIN, LONG_MAX) \
412 long long int, LLONG_MIN, LLONG_MAX)) 456 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
457 unsigned long int, 0, ULONG_MAX)) \
458 : (EXPR_SIGNED (*(r)) \
459 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
460 long long int, LLONG_MIN, LLONG_MAX) \
461 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
462 unsigned long long int, 0, ULLONG_MAX)))
413# else 463# else
414# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ 464# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
415 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ 465 (EXPR_SIGNED (*(r)) \
416 long int, LONG_MIN, LONG_MAX) 466 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
467 long int, LONG_MIN, LONG_MAX) \
468 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
469 unsigned long int, 0, ULONG_MAX))
417# endif 470# endif
418#endif 471#endif
419 472
@@ -422,13 +475,7 @@
422 overflow problems. *R's type is T, with extrema TMIN and TMAX. 475 overflow problems. *R's type is T, with extrema TMIN and TMAX.
423 T must be a signed integer type. Return 1 if the result overflows. */ 476 T must be a signed integer type. Return 1 if the result overflows. */
424#define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \ 477#define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
425 (sizeof ((a) op (b)) < sizeof (t) \ 478 (overflow (a, b, tmin, tmax) \
426 ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
427 : _GL_INT_OP_CALC1 (a, b, r, op, overflow, ut, t, tmin, tmax))
428#define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \
429 ((overflow (a, b) \
430 || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
431 || (tmax) < ((a) op (b))) \
432 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \ 479 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
433 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0)) 480 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
434 481
@@ -452,4 +499,57 @@
452#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \ 499#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
453 ((t) ((ut) (a) op (ut) (b))) 500 ((t) ((ut) (a) op (ut) (b)))
454 501
502/* Return true if the numeric values A + B, A - B, A * B fall outside
503 the range TMIN..TMAX. Arguments should be integer expressions
504 without side effects. TMIN should be signed and nonpositive.
505 TMAX should be positive, and should be signed unless TMIN is zero. */
506#define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
507 ((b) < 0 \
508 ? (((tmin) \
509 ? ((EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
510 && (a) < (tmin) - (b)) \
511 : (a) <= -1 - (b)) \
512 || ((EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
513 : (a) < 0 \
514 ? (((tmin) \
515 ? ((EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
516 && (b) < (tmin) - (a)) \
517 : (b) <= -1 - (a)) \
518 || ((EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
519 && (tmax) < (a) + (b))) \
520 : (tmax) < (b) || (tmax) - (b) < (a))
521#define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
522 (((a) < 0) == ((b) < 0) \
523 ? ((a) < (b) \
524 ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
525 : (tmax) < (a) - (b)) \
526 : (a) < 0 \
527 ? ((!EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
528 || (a) - (tmin) < (b)) \
529 : ((! (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
530 && EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
531 && (tmax) <= -1 - (b)) \
532 || (tmax) + (b) < (a)))
533#define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
534 ((b) < 0 \
535 ? ((a) < 0 \
536 ? (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
537 ? (a) < (tmax) / (b) \
538 : ((INT_NEGATE_OVERFLOW (b) \
539 ? _GL_INT_CONVERT (b, tmax) >> (TYPE_WIDTH (b) - 1) \
540 : (tmax) / -(b)) \
541 <= -1 - (a))) \
542 : INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
543 ? (EXPR_SIGNED (a) \
544 ? 0 < (a) + (tmin) \
545 : 0 < (a) && -1 - (tmin) < (a) - 1) \
546 : (tmin) / (b) < (a)) \
547 : (b) == 0 \
548 ? 0 \
549 : ((a) < 0 \
550 ? (INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
551 ? (EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
552 : (tmin) / (a) < (b)) \
553 : (tmax) / (b) < (a)))
554
455#endif /* _GL_INTPROPS_H */ 555#endif /* _GL_INTPROPS_H */
diff --git a/lib/verify.h b/lib/verify.h
index 9b8e1ed20fa..afdc1ad81f1 100644
--- a/lib/verify.h
+++ b/lib/verify.h
@@ -175,9 +175,11 @@
175#define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER) 175#define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
176 176
177/* Verify requirement R at compile-time, as an integer constant expression 177/* Verify requirement R at compile-time, as an integer constant expression
178 that returns 1. If R is false, fail at compile-time. */ 178 that returns 1. If R is false, fail at compile-time, preferably
179 with a diagnostic that includes the string-literal DIAGNOSTIC. */
179 180
180#define _GL_VERIFY_TRUE(R) (!!sizeof (_GL_VERIFY_TYPE (R))) 181#define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
182 (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
181 183
182#ifdef __cplusplus 184#ifdef __cplusplus
183# if !GNULIB_defined_struct__gl_verify_type 185# if !GNULIB_defined_struct__gl_verify_type
@@ -187,15 +189,16 @@ template <int w>
187 }; 189 };
188# define GNULIB_defined_struct__gl_verify_type 1 190# define GNULIB_defined_struct__gl_verify_type 1
189# endif 191# endif
190# define _GL_VERIFY_TYPE(R) _gl_verify_type<(R) ? 1 : -1> 192# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
191#elif defined _GL_HAVE__STATIC_ASSERT1 193 _gl_verify_type<(R) ? 1 : -1>
192# define _GL_VERIFY_TYPE(R) \ 194#elif defined _GL_HAVE__STATIC_ASSERT
195# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
193 struct { \ 196 struct { \
194 _Static_assert (R); \ 197 _Static_assert (R, DIAGNOSTIC); \
195 int _gl_dummy; \ 198 int _gl_dummy; \
196 } 199 }
197#else 200#else
198# define _GL_VERIFY_TYPE(R) \ 201# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
199 struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; } 202 struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
200#endif 203#endif
201 204
@@ -214,7 +217,7 @@ template <int w>
214#else 217#else
215# define _GL_VERIFY(R, DIAGNOSTIC, ...) \ 218# define _GL_VERIFY(R, DIAGNOSTIC, ...) \
216 extern int (*_GL_GENSYM (_gl_verify_function) (void)) \ 219 extern int (*_GL_GENSYM (_gl_verify_function) (void)) \
217 [_GL_VERIFY_TRUE (R)] 220 [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
218#endif 221#endif
219 222
220/* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */ 223/* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */
@@ -242,17 +245,19 @@ template <int w>
242/* Verify requirement R at compile-time. Return the value of the 245/* Verify requirement R at compile-time. Return the value of the
243 expression E. */ 246 expression E. */
244 247
245#define verify_expr(R, E) (_GL_VERIFY_TRUE (R) ? (E) : (E)) 248#define verify_expr(R, E) \
249 (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
246 250
247/* Verify requirement R at compile-time, as a declaration without a 251/* Verify requirement R at compile-time, as a declaration without a
248 trailing ';'. verify (R) acts like static_assert (R) except that 252 trailing ';'. verify (R) acts like static_assert (R) except that
249 it is portable to C11/C++14 and earlier, and its name is shorter 253 it is portable to C11/C++14 and earlier, it can issue better
250 and may be more convenient. */ 254 diagnostics, and its name is shorter and may be more convenient. */
251 255
252#ifdef _GL_HAVE__STATIC_ASSERT1 256#ifdef __PGI
253# define verify(R) _Static_assert (R) 257/* PGI barfs if R is long. */
254#else
255# define verify(R) _GL_VERIFY (R, "verify (...)", -) 258# define verify(R) _GL_VERIFY (R, "verify (...)", -)
259#else
260# define verify(R) _GL_VERIFY (R, "verify (" #R ")", -)
256#endif 261#endif
257 262
258#ifndef __has_builtin 263#ifndef __has_builtin