From 5f57cb41f93a99f0528ac850b6910e9b8e6dbfaa Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Wed, 19 Dec 2018 20:36:05 +0000 Subject: [PATCH] A bit more compiler dependent macros Signed-off-by: Yuxuan Shui --- src/compiler.h | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/compiler.h b/src/compiler.h index 75d3fcb..dca59e9 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -5,3 +5,77 @@ #define auto __auto_type #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) + +#ifndef __has_attribute +# if __GNUC__ >= 4 +# define __has_attribute(x) 1 +# else +# define __has_attribute(x) 0 +# endif +#endif + +#if __has_attribute(const) +# define attr_const __attribute__((const)) +#else +# define attr_const +#endif + +#if __has_attribute(format) +# define attr_printf(a, b) __attribute__((format(printf, a, b))) +#else +# define attr_printf(a, b) +#endif + +#if __has_attribute(pure) +# define attr_pure __attribute__((pure)) +#else +# define attr_pure +#endif + +#if __has_attribute(unused) +# define attr_unused __attribute__((unused)) +#else +# define attr_unused +#endif + +#if __has_attribute(warn_unused_result) +# define attr_warn_unused_result __attribute__((warn_unused_result)) +#else +# define attr_warn_unused_result +#endif + +#if __has_attribute(nonnull) +# define attr_nonnull(...) __attribute__((nonnull(__VA_ARGS__))) +# define attr_nonnull_all __attribute__((nonnull)) +#else +# define attr_nonnull(...) +# define attr_nonnull_all +#endif + +#if __has_attribute(returns_nonnull) +# define attr_ret_nonnull __attribute__((returns_nonnull)) +#else +# define attr_ret_nonnull +#endif + +#if __has_attribute(malloc) +# define attr_malloc __attribute__((malloc)) +#else +# define attr_malloc +#endif + +#if __STDC_VERSION__ >= 201112L +# define attr_noret _Noreturn +#else +# if __has_attribute(noreturn) +# define attr_noret __attribute__((noreturn)) +# else +# define attr_noret +# endif +#endif + +#if defined(__GNUC__) || defined(__clang__) +# define unreachable __builtin_unreachable() +#else +# define unreachable do {} while(0) +#endif