bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH 1/2] Port better to GCC under macOS
@ 2019-10-23 20:34 Paul Eggert
  2019-10-23 20:34 ` [PATCH 2/2] nstrftime: speed up integer overflow checking Paul Eggert
  0 siblings, 1 reply; 2+ messages in thread
From: Paul Eggert @ 2019-10-23 20:34 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Paul Eggert

Work around macOS header that has ‘#define __has_builtin(x) 0’
when compiled by GCC.  Apple really, really doesn’t want you to
use GCC, apparently.  Rroblem reported by Akim Demaille in:
https://lists.gnu.org/r/bug-bison/2019-10/msg00071.html
The fix is to not trust __has_builtin when being compiled by
recent-enough GCC.
* lib/intprops.h (__has_builtin)
(_GL_HAS___builtin_add_overflow, _GL_TEMPDEF___has_builtin):
* lib/verify.h (__has_builtin, _GL_HAS___builtin_unreachable)
(_GL_HAS___builtin_trap, _GL_TEMPDEF___has_builtin):
Remove.  All uses removed.
* lib/intprops.h (_GL_HAS_BUILTIN_ADD_OVERFLOW): Use __has_builtin
directly, if defined and if not newer GCC.
* lib/verify.h (_GL_HAS_BUILTIN_TRAP, _GL_HAS_BUILTIN_UNREACHABLE):
New macro, that use __has_builtin directly, if defined and if
not newer GCC.
(assume): Use them.
---
 ChangeLog      | 21 +++++++++++++++++++++
 lib/intprops.h | 22 +++-------------------
 lib/verify.h   | 37 ++++++++++++++++++-------------------
 3 files changed, 42 insertions(+), 38 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index fc438f503..103befc64 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,24 @@
+2019-10-23  Paul Eggert  <eggert@cs.ucla.edu>
+
+	Port better to GCC under macOS
+	Work around macOS header that has ‘#define __has_builtin(x) 0’
+	when compiled by GCC.  Apple really, really doesn’t want you to
+	use GCC, apparently.  Rroblem reported by Akim Demaille in:
+	https://lists.gnu.org/r/bug-bison/2019-10/msg00071.html
+	The fix is to not trust __has_builtin when being compiled by
+	recent-enough GCC.
+	* lib/intprops.h (__has_builtin)
+	(_GL_HAS___builtin_add_overflow, _GL_TEMPDEF___has_builtin):
+	* lib/verify.h (__has_builtin, _GL_HAS___builtin_unreachable)
+	(_GL_HAS___builtin_trap, _GL_TEMPDEF___has_builtin):
+	Remove.  All uses removed.
+	* lib/intprops.h (_GL_HAS_BUILTIN_ADD_OVERFLOW): Use __has_builtin
+	directly, if defined and if not newer GCC.
+	* lib/verify.h (_GL_HAS_BUILTIN_TRAP, _GL_HAS_BUILTIN_UNREACHABLE):
+	New macro, that use __has_builtin directly, if defined and if
+	not newer GCC.
+	(assume): Use them.
+
 2019-10-22  Akim Demaille  <akim@lrde.epita.fr>
 
 	maintainer-makefile: update rule for argmatch.
diff --git a/lib/intprops.h b/lib/intprops.h
index ffd737028..bc7e950a0 100644
--- a/lib/intprops.h
+++ b/lib/intprops.h
@@ -22,18 +22,6 @@
 
 #include <limits.h>
 
-/* If the compiler lacks __has_builtin, define it well enough for this
-   source file only.  */
-#ifndef __has_builtin
-# define __has_builtin(x) _GL_HAS_##x
-# if 5 <= __GNUC__ && !defined __ICC
-#  define _GL_HAS___builtin_add_overflow 1
-# else
-#  define _GL_HAS___builtin_add_overflow 0
-# endif
-# define _GL_TEMPDEF___has_builtin
-#endif
-
 /* Return a value with the common real type of E and V and the value of V.
    Do not evaluate E.  */
 #define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
@@ -234,8 +222,10 @@
 
 /* True if __builtin_add_overflow (A, B, P) and __builtin_sub_overflow
    (A, B, P) work when P is non-null.  */
-#if __has_builtin (__builtin_add_overflow)
+#if 5 <= __GNUC__ && !defined __ICC
 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 1
+#elif defined __has_builtin
+# define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow)
 #else
 # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
 #endif
@@ -586,10 +576,4 @@
          : (tmin) / (a) < (b)) \
       : (tmax) / (b) < (a)))
 
-#ifdef _GL_TEMPDEF___has_builtin
-# undef __has_builtin
-# undef _GL_HAS___builtin_add_overflow
-# undef _GL_TEMPDEF___has_builtin
-#endif
-
 #endif /* _GL_INTPROPS_H */
diff --git a/lib/verify.h b/lib/verify.h
index 06e975ebf..a58005c39 100644
--- a/lib/verify.h
+++ b/lib/verify.h
@@ -56,16 +56,6 @@
 # undef _Static_assert
 #endif
 
-/* If the compiler lacks __has_builtin, define it well enough for this
-   source file only.  */
-#ifndef __has_builtin
-# define __has_builtin(x) _GL_HAS_##x
-# define _GL_HAS___builtin_unreachable (4 < __GNUC__ + (5 <= __GNUC_MINOR__))
-# define _GL_HAS___builtin_trap \
-    (3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <= __GNUC_PATCHLEVEL__)))
-# define _GL_TEMPDEF___has_builtin
-#endif
-
 /* Each of these macros verifies that its argument R is nonzero.  To
    be portable, R should be an integer constant expression.  Unlike
    assert (R), there is no run-time overhead.
@@ -243,6 +233,22 @@ template <int w>
 
 /* @assert.h omit start@  */
 
+#if 3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <= __GNUC_PATCHLEVEL__))
+# define _GL_HAS_BUILTIN_TRAP 1
+#elif defined __has_builtin
+# define _GL_HAS_BUILTIN_TRAP __has_builtin (__builtin_trap)
+#else
+# define _GL_HAS_BUILTIN_TRAP 0
+#endif
+
+#if 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
+# define _GL_HAS_BUILTIN_UNREACHABLE 1
+#elif defined __has_builtin
+# define _GL_HAS_BUILTIN_UNREACHABLE __has_builtin (__builtin_unreachable)
+#else
+# define _GL_HAS_BUILTIN_UNREACHABLE 0
+#endif
+
 /* Each of these macros verifies that its argument R is nonzero.  To
    be portable, R should be an integer constant expression.  Unlike
    assert (R), there is no run-time overhead.
@@ -276,11 +282,11 @@ template <int w>
    can suffer if R uses hard-to-optimize features such as function
    calls not inlined by the compiler.  */
 
-#if __has_builtin (__builtin_unreachable)
+#if _GL_HAS_BUILTIN_UNREACHABLE
 # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
 #elif 1200 <= _MSC_VER
 # define assume(R) __assume (R)
-#elif (defined GCC_LINT || defined lint) && __has_builtin (__builtin_trap)
+#elif (defined GCC_LINT || defined lint) && _GL_HAS_BUILTIN_TRAP
   /* Doing it this way helps various packages when configured with
      --enable-gcc-warnings, which compiles with -Dlint.  It's nicer
      when 'assume' silences warnings even with older GCCs.  */
@@ -290,13 +296,6 @@ template <int w>
 # define assume(R) ((R) ? (void) 0 : /*NOTREACHED*/ (void) 0)
 #endif
 
-#ifdef _GL_TEMPDEF___has_builtin
-# undef __has_builtin
-# undef _GL_HAS___builtin_unreachable
-# undef _GL_HAS___builtin_trap
-# undef _GL_TEMPDEF___has_builtin
-#endif
-
 /* @assert.h omit end@  */
 
 #endif
-- 
2.21.0



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] nstrftime: speed up integer overflow checking
  2019-10-23 20:34 [PATCH 1/2] Port better to GCC under macOS Paul Eggert
@ 2019-10-23 20:34 ` Paul Eggert
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Eggert @ 2019-10-23 20:34 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Paul Eggert

* lib/nstrftime.c: Include intprops.h.
(INT_STRLEN_BOUND): Remove, as we can use intprops.h’s defn.
(__strftime_internal): Use INT_MULTIPLY_WRAPV and INT_ADD_WRAPV
instead of doing it by hand.
* modules/nstrftime (Depends-on): Add intprops.
---
 ChangeLog         |  7 +++++++
 lib/nstrftime.c   | 19 ++++---------------
 modules/nstrftime |  1 +
 3 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 103befc64..804294150 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2019-10-23  Paul Eggert  <eggert@cs.ucla.edu>
 
+	nstrftime: speed up integer overflow checking
+	* lib/nstrftime.c: Include intprops.h.
+	(INT_STRLEN_BOUND): Remove, as we can use intprops.h’s defn.
+	(__strftime_internal): Use INT_MULTIPLY_WRAPV and INT_ADD_WRAPV
+	instead of doing it by hand.
+	* modules/nstrftime (Depends-on): Add intprops.
+
 	Port better to GCC under macOS
 	Work around macOS header that has ‘#define __has_builtin(x) 0’
 	when compiled by GCC.  Apple really, really doesn’t want you to
diff --git a/lib/nstrftime.c b/lib/nstrftime.c
index 01db02bfe..11ad00b10 100644
--- a/lib/nstrftime.c
+++ b/lib/nstrftime.c
@@ -68,6 +68,8 @@ extern char *tzname[];
 #include <string.h>
 #include <stdbool.h>
 
+#include <intprops.h>
+
 #ifndef FALLTHROUGH
 # if __GNUC__ < 7
 #  define FALLTHROUGH ((void) 0)
@@ -113,13 +115,6 @@ extern char *tzname[];
    ? (a) >> (b)         \
    : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
 
-/* Bound on length of the string representing an integer type or expression T.
-   Subtract 1 for the sign bit if t is signed; log10 (2.0) < 146/485;
-   add 1 for integer division truncation; add 1 more for a minus sign
-   if needed.  */
-#define INT_STRLEN_BOUND(t) \
-  ((sizeof (t) * CHAR_BIT - 1) * 146 / 485 + 2)
-
 #define TM_YEAR_BASE 1900
 
 #ifndef __isleap
@@ -704,15 +699,9 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
           width = 0;
           do
             {
-              if (width > INT_MAX / 10
-                  || (width == INT_MAX / 10 && *f - L_('0') > INT_MAX % 10))
-                /* Avoid overflow.  */
+              if (INT_MULTIPLY_WRAPV (width, 10, &width)
+                  || INT_ADD_WRAPV (width, *f - L_('0'), &width))
                 width = INT_MAX;
-              else
-                {
-                  width *= 10;
-                  width += *f - L_('0');
-                }
               ++f;
             }
           while (ISDIGIT (*f));
diff --git a/modules/nstrftime b/modules/nstrftime
index b559b5e20..a1ce1b33f 100644
--- a/modules/nstrftime
+++ b/modules/nstrftime
@@ -9,6 +9,7 @@ m4/nstrftime.m4
 
 Depends-on:
 extensions
+intprops
 stdbool
 time_rz
 
-- 
2.21.0



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-10-23 22:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-23 20:34 [PATCH 1/2] Port better to GCC under macOS Paul Eggert
2019-10-23 20:34 ` [PATCH 2/2] nstrftime: speed up integer overflow checking Paul Eggert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).