Skip to content

Commit ec306a2

Browse files
vstinnerjmroot
andauthored
bpo-41617: Add _Py__has_builtin() macro (GH-23260) (GH-23262)
Fix building pycore_bitutils.h internal header on old clang version without __builtin_bswap16() (ex: Xcode 4.6.3 on Mac OS X 10.7). Add a new private _Py__has_builtin() macro to check for availability of a preprocessor builtin function. Co-Authored-By: Joshua Root <jmr@macports.org> Co-authored-by: Joshua Root <jmr@macports.org> (cherry picked from commit b3b9808)
1 parent e5729ae commit ec306a2

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

Include/internal/pycore_byteswap.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ extern "C" {
1515
# error "this header requires Py_BUILD_CORE define"
1616
#endif
1717

18-
#if ((defined(__GNUC__) \
19-
&& ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 8))) \
20-
|| (defined(__clang__) \
21-
&& (__clang_major__ >= 4 \
22-
|| (__clang_major__ == 3 && __clang_minor__ >= 2))))
23-
/* __builtin_bswap16() is available since GCC 4.8 and clang 3.2,
18+
#if defined(__GNUC__) \
19+
&& ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 8))
20+
/* __builtin_bswap16() is available since GCC 4.8,
2421
__builtin_bswap32() is available since GCC 4.3,
2522
__builtin_bswap64() is available since GCC 4.3. */
2623
# define _PY_HAVE_BUILTIN_BSWAP
@@ -34,7 +31,7 @@ extern "C" {
3431
static inline uint16_t
3532
_Py_bswap16(uint16_t word)
3633
{
37-
#ifdef _PY_HAVE_BUILTIN_BSWAP
34+
#if defined(_PY_HAVE_BUILTIN_BSWAP) || _Py__has_builtin(__builtin_bswap16)
3835
return __builtin_bswap16(word);
3936
#elif defined(_MSC_VER)
4037
Py_BUILD_ASSERT(sizeof(word) == sizeof(unsigned short));
@@ -49,7 +46,7 @@ _Py_bswap16(uint16_t word)
4946
static inline uint32_t
5047
_Py_bswap32(uint32_t word)
5148
{
52-
#ifdef _PY_HAVE_BUILTIN_BSWAP
49+
#if defined(_PY_HAVE_BUILTIN_BSWAP) || _Py__has_builtin(__builtin_bswap32)
5350
return __builtin_bswap32(word);
5451
#elif defined(_MSC_VER)
5552
Py_BUILD_ASSERT(sizeof(word) == sizeof(unsigned long));
@@ -66,7 +63,7 @@ _Py_bswap32(uint32_t word)
6663
static inline uint64_t
6764
_Py_bswap64(uint64_t word)
6865
{
69-
#ifdef _PY_HAVE_BUILTIN_BSWAP
66+
#if defined(_PY_HAVE_BUILTIN_BSWAP) || _Py__has_builtin(__builtin_bswap64)
7067
return __builtin_bswap64(word);
7168
#elif defined(_MSC_VER)
7269
return _byteswap_uint64(word);

Include/pyport.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,4 +863,16 @@ extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
863863
# define _Py_NO_RETURN
864864
#endif
865865

866+
867+
// Preprocessor check for a builtin preprocessor function. Always return 0
868+
// if __has_builtin() macro is not defined.
869+
//
870+
// __has_builtin() is available on clang and GCC 10.
871+
#ifdef __has_builtin
872+
# define _Py__has_builtin(x) __has_builtin(x)
873+
#else
874+
# define _Py__has_builtin(x) 0
875+
#endif
876+
877+
866878
#endif /* Py_PYPORT_H */
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix building ``pycore_bitutils.h`` internal header on old clang version
2+
without ``__builtin_bswap16()`` (ex: Xcode 4.6.3 on Mac OS X 10.7). Patch by
3+
Joshua Root and Victor Stinner.

0 commit comments

Comments
 (0)