commit db9e4bbab55d5c6e6ec38038f279ec14abecd4f5
parent 60c096eb0433f7573c768a2c8523abd3c11e0720
Author: Sertonix <sertonix@posteo.net>
Date: Thu, 9 Apr 2026 11:41:59 +0200
Wrap __has_attribute and __has_builtin
Simplifies the code and improves compatibility
Diffstat:
2 files changed, 19 insertions(+), 19 deletions(-)
diff --git 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
diff --git a/include/stdio.h b/include/stdio.h
@@ -108,8 +108,7 @@ _FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f,
return __r;
}
-#if defined(__has_builtin)
-#if __has_builtin(__builtin_va_arg_pack)
+#if __fortify_has_builtin(__builtin_va_arg_pack)
/* clang is missing __builtin_va_arg_pack, so we cannot use these impls
* outside of gcc; we then have a few options:
@@ -150,8 +149,7 @@ _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...)
return __r;
}
-#endif /* __has_builtin(__builtin_va_arg_pack) */
-#endif /* defined(__has_builtin) */
+#endif /* __fortify_has_builtin(__builtin_va_arg_pack) */
#ifdef __cplusplus
}