diff options
author | Daniel Stone <daniels@collabora.com> | 2022-12-29 20:06:07 +0000 |
---|---|---|
committer | Marius Vlad <marius.vlad@collabora.com> | 2023-01-10 10:59:04 +0200 |
commit | ef87ad2237183dd9e6f19f7dfe05fdabedd12a5a (patch) | |
tree | 5a72dd34bcac1ae0bf6549cc22235893e59964aa | |
parent | 35f04139c86442a3133fbfb60e176cc864175f77 (diff) | |
download | weston-ef87ad2237183dd9e6f19f7dfe05fdabedd12a5a.tar.gz |
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>
-rw-r--r-- | meson.build | 3 | ||||
-rw-r--r-- | shared/helpers.h | 20 |
2 files changed, 22 insertions, 1 deletions
diff --git a/meson.build b/meson.build index 2d9da454..7f8fed3d 100644 --- a/meson.build +++ b/meson.build @@ -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) diff --git a/shared/helpers.h b/shared/helpers.h index 32938eca..2e7ed2ea 100644 --- a/shared/helpers.h +++ b/shared/helpers.h @@ -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 |