Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3b9808

Browse files
vstinnerjmroot
andauthored
bpo-41617: Add _Py__has_builtin() macro (GH-23260)
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 <[email protected]> Co-authored-by: Joshua Root <[email protected]>
1 parent d96a7a8 commit b3b9808

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

Include/internal/pycore_bitutils.h

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

20-
#if ((defined(__GNUC__) \
21-
&& ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 8))) \
22-
|| (defined(__clang__) \
23-
&& (__clang_major__ >= 4 \
24-
|| (__clang_major__ == 3 && __clang_minor__ >= 2))))
25-
/* __builtin_bswap16() is available since GCC 4.8 and clang 3.2,
20+
#if defined(__GNUC__) \
21+
&& ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 8))
22+
/* __builtin_bswap16() is available since GCC 4.8,
2623
__builtin_bswap32() is available since GCC 4.3,
2724
__builtin_bswap64() is available since GCC 4.3. */
2825
# define _PY_HAVE_BUILTIN_BSWAP
@@ -36,7 +33,7 @@ extern "C" {
3633
static inline uint16_t
3734
_Py_bswap16(uint16_t word)
3835
{
39-
#ifdef _PY_HAVE_BUILTIN_BSWAP
36+
#if defined(_PY_HAVE_BUILTIN_BSWAP) || _Py__has_builtin(__builtin_bswap16)
4037
return __builtin_bswap16(word);
4138
#elif defined(_MSC_VER)
4239
Py_BUILD_ASSERT(sizeof(word) == sizeof(unsigned short));
@@ -51,7 +48,7 @@ _Py_bswap16(uint16_t word)
5148
static inline uint32_t
5249
_Py_bswap32(uint32_t word)
5350
{
54-
#ifdef _PY_HAVE_BUILTIN_BSWAP
51+
#if defined(_PY_HAVE_BUILTIN_BSWAP) || _Py__has_builtin(__builtin_bswap32)
5552
return __builtin_bswap32(word);
5653
#elif defined(_MSC_VER)
5754
Py_BUILD_ASSERT(sizeof(word) == sizeof(unsigned long));
@@ -68,7 +65,7 @@ _Py_bswap32(uint32_t word)
6865
static inline uint64_t
6966
_Py_bswap64(uint64_t word)
7067
{
71-
#ifdef _PY_HAVE_BUILTIN_BSWAP
68+
#if defined(_PY_HAVE_BUILTIN_BSWAP) || _Py__has_builtin(__builtin_bswap64)
7269
return __builtin_bswap64(word);
7370
#elif defined(_MSC_VER)
7471
return _byteswap_uint64(word);

Include/pyport.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,4 +869,16 @@ extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
869869
# define _Py_NO_RETURN
870870
#endif
871871

872+
873+
// Preprocessor check for a builtin preprocessor function. Always return 0
874+
// if __has_builtin() macro is not defined.
875+
//
876+
// __has_builtin() is available on clang and GCC 10.
877+
#ifdef __has_builtin
878+
# define _Py__has_builtin(x) __has_builtin(x)
879+
#else
880+
# define _Py__has_builtin(x) 0
881+
#endif
882+
883+
872884
#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)