diff options
| author | Sertonix | 2026-04-09 11:41:59 +0200 |
|---|---|---|
| committer | Julien Voisin | 2026-04-13 00:04:14 +0200 |
| commit | db9e4bbab55d5c6e6ec38038f279ec14abecd4f5 (patch) | |
| tree | d89f312d591b48f8ab0f4a77269143c035497e81 /include | |
| parent | 60c096eb0433f7573c768a2c8523abd3c11e0720 (diff) | |
Wrap __has_attribute and __has_builtin
Simplifies the code and improves compatibility
Diffstat (limited to 'include')
| -rw-r--r-- | include/fortify-headers.h | 32 | ||||
| -rw-r--r-- | include/stdio.h | 6 |
2 files changed, 19 insertions, 19 deletions
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 @@ | |||
| 17 | #ifndef _FORTIFY_HEADERS_H | 17 | #ifndef _FORTIFY_HEADERS_H |
| 18 | #define _FORTIFY_HEADERS_H | 18 | #define _FORTIFY_HEADERS_H |
| 19 | 19 | ||
| 20 | #ifdef __has_attribute | ||
| 21 | # define __fortify_has_attribute(v) __has_attribute(v) | ||
| 22 | #else | ||
| 23 | # define __fortify_has_attribute(v) 0 | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #ifdef __has_builtin | ||
| 27 | # define __fortify_has_builtin(v) __has_builtin(v) | ||
| 28 | #else | ||
| 29 | # define __fortify_has_builtin(v) 0 | ||
| 30 | #endif | ||
| 31 | |||
| 20 | #ifdef __clang__ | 32 | #ifdef __clang__ |
| 21 | 33 | ||
| 22 | #if _FORTIFY_SOURCE > 2 && defined __has_attribute && __has_attribute(pass_dynamic_object_size) | 34 | #if _FORTIFY_SOURCE > 2 && __fortify_has_attribute(pass_dynamic_object_size) |
| 23 | #define _FORTIFY_POSN(n) const __attribute__((pass_dynamic_object_size(n))) | 35 | #define _FORTIFY_POSN(n) const __attribute__((pass_dynamic_object_size(n))) |
| 24 | #else | 36 | #else |
| 25 | /* clang uses overloads; see https://github.com/llvm/llvm-project/issues/53516 */ | 37 | /* clang uses overloads; see https://github.com/llvm/llvm-project/issues/53516 */ |
| @@ -53,38 +65,28 @@ | |||
| 53 | #define _FORTIFY_FN(fn) _FORTIFY_FNB(fn); _FORTIFY_INLINE | 65 | #define _FORTIFY_FN(fn) _FORTIFY_FNB(fn); _FORTIFY_INLINE |
| 54 | 66 | ||
| 55 | 67 | ||
| 56 | #if _FORTIFY_SOURCE > 2 && defined __has_builtin && __has_builtin (__builtin_dynamic_object_size) | 68 | #if _FORTIFY_SOURCE > 2 && __fortify_has_builtin(__builtin_dynamic_object_size) |
| 57 | #define __bos(ptr, type) __builtin_dynamic_object_size (ptr, type) | 69 | #define __bos(ptr, type) __builtin_dynamic_object_size (ptr, type) |
| 58 | #else | 70 | #else |
| 59 | #define __bos(ptr, type) __builtin_object_size (ptr, type) | 71 | #define __bos(ptr, type) __builtin_object_size (ptr, type) |
| 60 | #endif | 72 | #endif |
| 61 | 73 | ||
| 62 | #if defined __has_attribute | 74 | #if __fortify_has_attribute(access) |
| 63 | |||
| 64 | #if __has_attribute (access) | ||
| 65 | #define __fortify_access(...) __attribute__ ((access (__VA_ARGS__))) | 75 | #define __fortify_access(...) __attribute__ ((access (__VA_ARGS__))) |
| 66 | #else | 76 | #else |
| 67 | #define __fortify_access(...) | 77 | #define __fortify_access(...) |
| 68 | #endif | 78 | #endif |
| 69 | 79 | ||
| 70 | #if __has_attribute (format) | 80 | #if __fortify_has_attribute(format) |
| 71 | #define __fortify__format(...) __attribute__ ((format (__VA_ARGS__))) | 81 | #define __fortify__format(...) __attribute__ ((format (__VA_ARGS__))) |
| 72 | #else | 82 | #else |
| 73 | #define __fortify__format(...) | 83 | #define __fortify__format(...) |
| 74 | #endif | 84 | #endif |
| 75 | 85 | ||
| 76 | #if __has_attribute (__diagnose_if) | 86 | #if __fortify_has_attribute(__diagnose_if) |
| 77 | #define __fortify_warning_if(cond, msg) __attribute__ ((__diagnose_if (cond, msg, "warning"))) | 87 | #define __fortify_warning_if(cond, msg) __attribute__ ((__diagnose_if (cond, msg, "warning"))) |
| 78 | #else | 88 | #else |
| 79 | #define __fortify_warning_if(cond, msg) | 89 | #define __fortify_warning_if(cond, msg) |
| 80 | #endif | 90 | #endif |
| 81 | 91 | ||
| 82 | #else /* ! __has_attribute */ | ||
| 83 | |||
| 84 | #define __fortify_access(...) | ||
| 85 | #define __fortify__format(...) | ||
| 86 | #define __fortify_warning_if(cond, msg) | ||
| 87 | |||
| 88 | #endif | ||
| 89 | |||
| 90 | #endif | 92 | #endif |
diff --git a/include/stdio.h b/include/stdio.h index 2a19cd0..2d1ee33 100644 --- a/include/stdio.h +++ b/include/stdio.h | |||
| @@ -108,8 +108,7 @@ _FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f, | |||
| 108 | return __r; | 108 | return __r; |
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | #if defined(__has_builtin) | 111 | #if __fortify_has_builtin(__builtin_va_arg_pack) |
| 112 | #if __has_builtin(__builtin_va_arg_pack) | ||
| 113 | 112 | ||
| 114 | /* clang is missing __builtin_va_arg_pack, so we cannot use these impls | 113 | /* clang is missing __builtin_va_arg_pack, so we cannot use these impls |
| 115 | * outside of gcc; we then have a few options: | 114 | * outside of gcc; we then have a few options: |
| @@ -150,8 +149,7 @@ _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...) | |||
| 150 | return __r; | 149 | return __r; |
| 151 | } | 150 | } |
| 152 | 151 | ||
| 153 | #endif /* __has_builtin(__builtin_va_arg_pack) */ | 152 | #endif /* __fortify_has_builtin(__builtin_va_arg_pack) */ |
| 154 | #endif /* defined(__has_builtin) */ | ||
| 155 | 153 | ||
| 156 | #ifdef __cplusplus | 154 | #ifdef __cplusplus |
| 157 | } | 155 | } |
