build: Add unreachable()

unreachable() is used to hint to the compiler that a certain branch
cannot ever be reached. The implementation is taken from Mesa.

Signed-off-by: Daniel Stone <daniels@collabora.com>
This commit is contained in:
Daniel Stone 2022-12-29 20:06:07 +00:00 committed by Marius Vlad
parent 35f04139c8
commit ef87ad2237
2 changed files with 22 additions and 1 deletions

View File

@ -79,7 +79,8 @@ elif cc.has_header_symbol('sys/mkdev.h', 'major')
endif
optional_libc_funcs = [
'mkostemp', 'strchrnul', 'initgroups', 'posix_fallocate', 'memfd_create'
'mkostemp', 'strchrnul', 'initgroups', 'posix_fallocate',
'memfd_create', 'unreachable',
]
foreach func : optional_libc_funcs
if cc.has_function(func)

View File

@ -179,6 +179,26 @@ u64_from_u32s(uint32_t hi, uint32_t lo)
return ((uint64_t)hi << 32) + lo;
}
#ifndef __has_builtin
# define __has_builtin(x) 0
#endif
#if defined(HAVE_UNREACHABLE) || __has_builtin(__builtin_unreachable)
#define unreachable(str) \
do { \
assert(!str); \
__builtin_unreachable(); \
} while (0)
#elif defined (_MSC_VER)
#define unreachable(str) \
do { \
assert(!str); \
__assume(0); \
} while (0)
#else
#define unreachable(str) assert(!str)
#endif
#ifdef __cplusplus
}
#endif