From db9e4bbab55d5c6e6ec38038f279ec14abecd4f5 Mon Sep 17 00:00:00 2001 From: Sertonix Date: Thu, 9 Apr 2026 11:41:59 +0200 Subject: Wrap __has_attribute and __has_builtin Simplifies the code and improves compatibility --- include/fortify-headers.h | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'include/fortify-headers.h') diff --git a/include/fortify-headers.h b/include/fortify-headers.h index a173466..4fbc808 100644 --- a/include/fortify-headers.h +++ b/include/fortify-headers.h @@ -17,9 +17,21 @@ #ifndef _FORTIFY_HEADERS_H #define _FORTIFY_HEADERS_H +#ifdef __has_attribute +# define __fortify_has_attribute(v) __has_attribute(v) +#else +# define __fortify_has_attribute(v) 0 +#endif + +#ifdef __has_builtin +# define __fortify_has_builtin(v) __has_builtin(v) +#else +# define __fortify_has_builtin(v) 0 +#endif + #ifdef __clang__ -#if _FORTIFY_SOURCE > 2 && defined __has_attribute && __has_attribute(pass_dynamic_object_size) +#if _FORTIFY_SOURCE > 2 && __fortify_has_attribute(pass_dynamic_object_size) #define _FORTIFY_POSN(n) const __attribute__((pass_dynamic_object_size(n))) #else /* clang uses overloads; see https://github.com/llvm/llvm-project/issues/53516 */ @@ -53,38 +65,28 @@ #define _FORTIFY_FN(fn) _FORTIFY_FNB(fn); _FORTIFY_INLINE -#if _FORTIFY_SOURCE > 2 && defined __has_builtin && __has_builtin (__builtin_dynamic_object_size) +#if _FORTIFY_SOURCE > 2 && __fortify_has_builtin(__builtin_dynamic_object_size) #define __bos(ptr, type) __builtin_dynamic_object_size (ptr, type) #else #define __bos(ptr, type) __builtin_object_size (ptr, type) #endif -#if defined __has_attribute - -#if __has_attribute (access) +#if __fortify_has_attribute(access) #define __fortify_access(...) __attribute__ ((access (__VA_ARGS__))) #else #define __fortify_access(...) #endif -#if __has_attribute (format) +#if __fortify_has_attribute(format) #define __fortify__format(...) __attribute__ ((format (__VA_ARGS__))) #else #define __fortify__format(...) #endif -#if __has_attribute (__diagnose_if) +#if __fortify_has_attribute(__diagnose_if) #define __fortify_warning_if(cond, msg) __attribute__ ((__diagnose_if (cond, msg, "warning"))) #else #define __fortify_warning_if(cond, msg) #endif -#else /* ! __has_attribute */ - -#define __fortify_access(...) -#define __fortify__format(...) -#define __fortify_warning_if(cond, msg) - -#endif - #endif -- cgit v1.3