From c656ed8a279d5002e6aa42fc1d02e244be89b67f Mon Sep 17 00:00:00 2001 From: Dana Robinson <43805+derobins@users.noreply.github.com> Date: Tue, 16 Sep 2025 20:05:55 -0700 Subject: [PATCH] Fix protection for __builtin_expect() in H5public.h (#5840) This feature is required to be in H5public.h, but is protected by an HDF5 feature test macro that will soon be private. There is also no guarantee that the compiler used to build the software has the same support for __builtin_expect() that the application compiler has. This PR updates the feature test checks for __builtin_expect() Partial fix for #5819 --- config/HDFTests.c | 15 --------------- src/H5pubconf.h.in | 3 --- src/H5public.h | 16 ++++++++++++++++ 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/config/HDFTests.c b/config/HDFTests.c index 33135f1a04e..f7ab8d916ce 100644 --- a/config/HDFTests.c +++ b/config/HDFTests.c @@ -27,21 +27,6 @@ main () #endif /* HAVE___FLOAT128 */ -#ifdef HAVE_BUILTIN_EXPECT - -int -main () -{ - void *ptr = (void*) 0; - - if (__builtin_expect (ptr != (void*) 0, 1)) - return 0; - - return 0; -} - -#endif /* HAVE_BUILTIN_EXPECT */ - #ifdef HAVE_ATTRIBUTE int diff --git a/src/H5pubconf.h.in b/src/H5pubconf.h.in index 9a87433e6b0..93b28b5327f 100644 --- a/src/H5pubconf.h.in +++ b/src/H5pubconf.h.in @@ -307,9 +307,6 @@ /* Define to 1 if you have the header file. */ #cmakedefine H5_HAVE_SZLIB_H @H5_HAVE_SZLIB_H@ -/* Define to 1 if the compiler supports the __builtin_expect() extension */ -#cmakedefine H5_HAVE_BUILTIN_EXPECT @H5_HAVE_BUILTIN_EXPECT@ - /* Define if we have thread support */ # cmakedefine H5_HAVE_THREADS @H5_HAVE_THREADS@ diff --git a/src/H5public.h b/src/H5public.h index dfad2b2d511..09c46e15f8e 100644 --- a/src/H5public.h +++ b/src/H5public.h @@ -461,6 +461,22 @@ typedef void (*H5_atclose_func_t)(void *ctx); * Does the compiler support the __builtin_expect() syntax? * It's not a problem if not. */ + +/* clang-format off */ +#if defined(__has_builtin) + /* clang extension to check for builtins. Do this first, because clang + * also defines __GNUC__ and didn't support __builtin_expect() until + * more recently. + */ +# if __has_builtin(__builtin_expect) +# define H5_HAVE_BUILTIN_EXPECT 1 +# endif +#elif defined(__GNUC__) + /* __builtin_expect() has been supported since 2.95 or 2.96 (circa 2000) */ +# define H5_HAVE_BUILTIN_EXPECT 1 +#endif +/* clang-format on */ + #if H5_HAVE_BUILTIN_EXPECT #define H5_LIKELY(expression) __builtin_expect(!!(expression), 1) #define H5_UNLIKELY(expression) __builtin_expect(!!(expression), 0)