From 775838ec012c1eec28a3b93d759ef3810954b54f Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 27 Nov 2025 18:18:38 -0800 Subject: [PATCH] compilersupport: fix compilation in C23 mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GCC15 added support for C23's `unreachable()`, causing a warning: ``` src/compilersupport_p.h:215:11: warning: ‘unreachable’ redefined 215 | # define unreachable() __builtin_unreachable() | ^~~~~~~~~~~ stddef.h:468:9: note: this is the previous definition 468 | #define unreachable() (__builtin_unreachable ()) | ^~~~~~~~~~~ ``` Signed-off-by: Thiago Macieira --- src/compilersupport_p.h | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/compilersupport_p.h b/src/compilersupport_p.h index 92517997..e41a2dee 100644 --- a/src/compilersupport_p.h +++ b/src/compilersupport_p.h @@ -52,14 +52,10 @@ # define cbor_static_assert(x) ((void)sizeof(char[2*!!(x) - 1])) #endif -#if defined(__has_cpp_attribute) && defined(__cplusplus) // C++17 +#if defined(__has_cpp_attribute) // C23 and C++17 # if __has_cpp_attribute(fallthrough) # define CBOR_FALLTHROUGH [[fallthrough]] # endif -#elif defined(__has_c_attribute) && !defined(__cplusplus) // C23 -# if __has_c_attribute(fallthrough) -# define CBOR_FALLTHROUGH [[fallthrough]] -# endif #endif #ifndef CBOR_FALLTHROUGH # ifdef __GNUC__ @@ -209,22 +205,22 @@ # define CBOR_NULLPTR NULL #endif -#ifdef __GNUC__ -#ifndef likely +#ifdef likely +/* something has already defined likely(), accept it */ +#elif defined(__GNUC__) # define likely(x) __builtin_expect(!!(x), 1) -#endif -#ifndef unlikely # define unlikely(x) __builtin_expect(!!(x), 0) +#else +# define likely(x) (x) +# define unlikely(x) (x) #endif + +#ifdef unreachable +/* C23 has unreachable() */ +#elif defined(__GNUC__) # define unreachable() __builtin_unreachable() #elif defined(_MSC_VER) -# define likely(x) (x) -# define unlikely(x) (x) # define unreachable() __assume(0) -#else -# define likely(x) (x) -# define unlikely(x) (x) -# define unreachable() do {} while (0) #endif static inline bool add_check_overflow(size_t v1, size_t v2, size_t *r)