From b41ebe3d18f8ac1270329747c64fe1949843f2b6 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Sat, 28 May 2022 11:25:33 +0200 Subject: [PATCH 1/2] macro: Move attribute defintions to macro-fundamental This also sorts them. --- src/basic/macro.h | 18 ------------ src/fundamental/macro-fundamental.h | 45 ++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/basic/macro.h b/src/basic/macro.h index e6f89608f4..9727279155 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -11,24 +11,6 @@ #include "macro-fundamental.h" -#define _printf_(a, b) __attribute__((__format__(printf, a, b))) -#ifdef __clang__ -# define _alloc_(...) -#else -# define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__))) -#endif -#define _sentinel_ __attribute__((__sentinel__)) -#define _destructor_ __attribute__((__destructor__)) -#define _deprecated_ __attribute__((__deprecated__)) -#define _malloc_ __attribute__((__malloc__)) -#define _weak_ __attribute__((__weak__)) -#define _public_ __attribute__((__visibility__("default"))) -#define _hidden_ __attribute__((__visibility__("hidden"))) -#define _weakref_(x) __attribute__((__weakref__(#x))) -#define _alignas_(x) __attribute__((__aligned__(__alignof__(x)))) -#define _alignptr_ __attribute__((__aligned__(sizeof(void*)))) -#define _warn_unused_result_ __attribute__((__warn_unused_result__)) - #if !defined(HAS_FEATURE_MEMORY_SANITIZER) # if defined(__has_feature) # if __has_feature(memory_sanitizer) diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index 77efb51ba0..eb43cf3647 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -10,22 +10,41 @@ #include "types-fundamental.h" #define _align_(x) __attribute__((__aligned__(x))) -#define _const_ __attribute__((__const__)) -#define _pure_ __attribute__((__pure__)) -#define _section_(x) __attribute__((__section__(x))) -#define _packed_ __attribute__((__packed__)) -#define _retain_ __attribute__((__retain__)) -#define _used_ __attribute__((__used__)) -#define _unused_ __attribute__((__unused__)) +#define _alignas_(x) __attribute__((__aligned__(__alignof__(x)))) +#define _alignptr_ __attribute__((__aligned__(sizeof(void *)))) #define _cleanup_(x) __attribute__((__cleanup__(x))) +#define _const_ __attribute__((__const__)) +#define _deprecated_ __attribute__((__deprecated__)) +#define _destructor_ __attribute__((__destructor__)) +#define _hidden_ __attribute__((__visibility__("hidden"))) #define _likely_(x) (__builtin_expect(!!(x), 1)) -#define _unlikely_(x) (__builtin_expect(!!(x), 0)) -#if __GNUC__ >= 7 -#define _fallthrough_ __attribute__((__fallthrough__)) -#else -#define _fallthrough_ -#endif +#define _malloc_ __attribute__((__malloc__)) #define _noreturn_ _Noreturn +#define _packed_ __attribute__((__packed__)) +#define _printf_(a, b) __attribute__((__format__(printf, a, b))) +#define _public_ __attribute__((__visibility__("default"))) +#define _pure_ __attribute__((__pure__)) +#define _retain_ __attribute__((__retain__)) +#define _section_(x) __attribute__((__section__(x))) +#define _sentinel_ __attribute__((__sentinel__)) +#define _unlikely_(x) (__builtin_expect(!!(x), 0)) +#define _unused_ __attribute__((__unused__)) +#define _used_ __attribute__((__used__)) +#define _warn_unused_result_ __attribute__((__warn_unused_result__)) +#define _weak_ __attribute__((__weak__)) +#define _weakref_(x) __attribute__((__weakref__(#x))) + +#ifdef __clang__ +# define _alloc_(...) +#else +# define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__))) +#endif + +#if __GNUC__ >= 7 +# define _fallthrough_ __attribute__((__fallthrough__)) +#else +# define _fallthrough_ +#endif #define XSTRINGIFY(x) #x #define STRINGIFY(x) XSTRINGIFY(x) From c0f5d58c9ab77fd9a12ba83ee0171d836b66d0b1 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Sat, 28 May 2022 11:41:10 +0200 Subject: [PATCH 2/2] meson: Document why -Wimplicit-fallthrough is not used with clang This changes the macro to ensure proper fallthrough attributes are used with clang in case this option is added in the future. --- meson.build | 3 +++ src/fundamental/macro-fundamental.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index f416b4f234..38ebe509f6 100644 --- a/meson.build +++ b/meson.build @@ -351,6 +351,9 @@ possible_common_cc_flags = [ '-Werror=shift-overflow=2', '-Werror=undef', '-Wfloat-equal', + # gperf prevents us from enabling this because it does not emit fallthrough + # attribute with clang. + #'-Wimplicit-fallthrough', '-Wimplicit-fallthrough=5', '-Winit-self', '-Wlogical-op', diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index eb43cf3647..59d63e8e5d 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -40,7 +40,7 @@ # define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__))) #endif -#if __GNUC__ >= 7 +#if __GNUC__ >= 7 || __clang__ # define _fallthrough_ __attribute__((__fallthrough__)) #else # define _fallthrough_