On 9/17/20 8:30 AM, Nelson H. F. Beebe wrote:
On OpenMandriva Lx 4.1 (Mercury), with /usr/bin/cc == clang version 9.0.1,
I get

   CC       dfa.o
In file included from dfa.c:61:
./xalloc.h:94:7: error: use of unknown builtin '__builtin_mul_overflow_p' 
[-Wimplicit-function-declaration]
   if (xalloc_oversized (n, s))
       ^
./xalloc-oversized.h:46:4: note: expanded from macro 'xalloc_oversized'
    __builtin_mul_overflow_p (n, s, (__xalloc_count_type) 1)
    ^
... long cascade of similar errors ...

Thanks for reporting that. I guess Clang 9.0.1 defines __GNUC__ to be >=7 even though it doesn't implement all GCC 7 features. I installed the attached patch into Gnulib to try to work around this glitch, and this patch should appear in Grep the next time it syncs with Gnulib. cc'ing to bug-gnulib.
>From 494aaa530b32b4290df9a31d123bf1825eda7217 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Thu, 17 Sep 2020 12:17:15 -0700
Subject: [PATCH] intprops, xalloc: avoid __builtin_mul_overflow_p with Clang

Problem reported by Nelson H. F. Beebe for clang 9.0.1 in:
https://lists.gnu.org/r/grep-devel/2020-09/msg00028.html
* lib/intprops.h (_GL_HAS_BUILTIN_OVERFLOW_P) [__clang__]:
Define to 0.
* lib/xalloc-oversized.h (xalloc_oversized) [__clang__]:
Do not use __builtin_mul_overflow_p.
---
 ChangeLog              | 8 ++++++++
 lib/intprops.h         | 8 +++++++-
 lib/xalloc-oversized.h | 2 +-
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b484c8dec..a3a8ab1e2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2020-09-17  Paul Eggert  <egg...@cs.ucla.edu>
 
+	intprops, xalloc: avoid __builtin_mul_overflow_p with Clang
+	Problem reported by Nelson H. F. Beebe for clang 9.0.1 in:
+	https://lists.gnu.org/r/grep-devel/2020-09/msg00028.html
+	* lib/intprops.h (_GL_HAS_BUILTIN_OVERFLOW_P) [__clang__]:
+	Define to 0.
+	* lib/xalloc-oversized.h (xalloc_oversized) [__clang__]:
+	Do not use __builtin_mul_overflow_p.
+
 	libc-config: port __THROW to Ubuntu 4
 	* lib/cdefs.h (__THROW): Do not use __attribute__ ((__nothrow__))
 	for GCC 3.3.  Problem reported by Jeffrey Walton in:
diff --git a/lib/intprops.h b/lib/intprops.h
index df66a3877..cb086715b 100644
--- a/lib/intprops.h
+++ b/lib/intprops.h
@@ -244,7 +244,13 @@
 
 /* True if __builtin_add_overflow_p (A, B, C) works, and similarly for
    __builtin_sub_overflow_p and __builtin_mul_overflow_p.  */
-#define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
+#ifdef __clang__
+/* Clang 9 lacks __builtin_mul_overflow_p, and even if it did it would
+   presumably run afoul of Clang bug 16404.  */
+# define _GL_HAS_BUILTIN_OVERFLOW_P 0
+#else
+# define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
+#endif
 
 /* The _GL*_OVERFLOW macros have the same restrictions as the
    *_RANGE_OVERFLOW macros, except that they do not assume that operands
diff --git a/lib/xalloc-oversized.h b/lib/xalloc-oversized.h
index 13ee23031..7cd4a74f0 100644
--- a/lib/xalloc-oversized.h
+++ b/lib/xalloc-oversized.h
@@ -41,7 +41,7 @@ typedef size_t __xalloc_count_type;
    positive and N must be nonnegative.  This is a macro, not a
    function, so that it works correctly even when SIZE_MAX < N.  */
 
-#if 7 <= __GNUC__
+#if 7 <= __GNUC__ && !defined __clang__
 # define xalloc_oversized(n, s) \
    __builtin_mul_overflow_p (n, s, (__xalloc_count_type) 1)
 #elif 5 <= __GNUC__ && !defined __ICC && !__STRICT_ANSI__
-- 
2.17.1

Reply via email to