1a3f8a30fSMiguel Ojeda /* SPDX-License-Identifier: GPL-2.0 */ 2a3f8a30fSMiguel Ojeda #ifndef __LINUX_COMPILER_ATTRIBUTES_H 3a3f8a30fSMiguel Ojeda #define __LINUX_COMPILER_ATTRIBUTES_H 4a3f8a30fSMiguel Ojeda 5a3f8a30fSMiguel Ojeda /* 6a3f8a30fSMiguel Ojeda * The attributes in this file are unconditionally defined and they directly 724efee41SMiguel Ojeda * map to compiler attribute(s), unless one of the compilers does not support 824efee41SMiguel Ojeda * the attribute. In that case, __has_attribute is used to check for support 924efee41SMiguel Ojeda * and the reason is stated in its comment ("Optional: ..."). 10a3f8a30fSMiguel Ojeda * 11a3f8a30fSMiguel Ojeda * Any other "attributes" (i.e. those that depend on a configuration option, 12a3f8a30fSMiguel Ojeda * on a compiler, on an architecture, on plugins, on other attributes...) 13a3f8a30fSMiguel Ojeda * should be defined elsewhere (e.g. compiler_types.h or compiler-*.h). 1424efee41SMiguel Ojeda * The intention is to keep this file as simple as possible, as well as 1524efee41SMiguel Ojeda * compiler- and version-agnostic (e.g. avoiding GCC_VERSION checks). 16a3f8a30fSMiguel Ojeda * 17a3f8a30fSMiguel Ojeda * This file is meant to be sorted (by actual attribute name, 18a3f8a30fSMiguel Ojeda * not by #define identifier). Use the __attribute__((__name__)) syntax 19a3f8a30fSMiguel Ojeda * (i.e. with underscores) to avoid future collisions with other macros. 2024efee41SMiguel Ojeda * Provide links to the documentation of each supported compiler, if it exists. 21a3f8a30fSMiguel Ojeda */ 22a3f8a30fSMiguel Ojeda 23a3f8a30fSMiguel Ojeda /* 24a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute 25a3f8a30fSMiguel Ojeda */ 26a3f8a30fSMiguel Ojeda #define __alias(symbol) __attribute__((__alias__(#symbol))) 27a3f8a30fSMiguel Ojeda 28a3f8a30fSMiguel Ojeda /* 29a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-aligned-function-attribute 30a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute 31a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-aligned-variable-attribute 32a3f8a30fSMiguel Ojeda */ 33a3f8a30fSMiguel Ojeda #define __aligned(x) __attribute__((__aligned__(x))) 34a3f8a30fSMiguel Ojeda #define __aligned_largest __attribute__((__aligned__)) 35a3f8a30fSMiguel Ojeda 36a3f8a30fSMiguel Ojeda /* 3786cffecdSKees Cook * Note: do not use this directly. Instead, use __alloc_size() since it is conditionally 389ed9cac1SKees Cook * available and includes other attributes. For GCC < 9.1, __alloc_size__ gets undefined 399ed9cac1SKees Cook * in compiler-gcc.h, due to misbehaviors. 4086cffecdSKees Cook * 4186cffecdSKees Cook * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute 4286cffecdSKees Cook * clang: https://clang.llvm.org/docs/AttributeReference.html#alloc-size 4386cffecdSKees Cook */ 4486cffecdSKees Cook #define __alloc_size__(x, ...) __attribute__((__alloc_size__(x, ## __VA_ARGS__))) 4586cffecdSKees Cook 4686cffecdSKees Cook /* 47a3f8a30fSMiguel Ojeda * Note: users of __always_inline currently do not write "inline" themselves, 48a3f8a30fSMiguel Ojeda * which seems to be required by gcc to apply the attribute according 49a3f8a30fSMiguel Ojeda * to its docs (and also "warning: always_inline function might not be 50a3f8a30fSMiguel Ojeda * inlinable [-Wattributes]" is emitted). 51a3f8a30fSMiguel Ojeda * 52a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute 53a3f8a30fSMiguel Ojeda * clang: mentioned 54a3f8a30fSMiguel Ojeda */ 55a3f8a30fSMiguel Ojeda #define __always_inline inline __attribute__((__always_inline__)) 56a3f8a30fSMiguel Ojeda 57a3f8a30fSMiguel Ojeda /* 58a3f8a30fSMiguel Ojeda * The second argument is optional (default 0), so we use a variadic macro 59a3f8a30fSMiguel Ojeda * to make the shorthand. 60a3f8a30fSMiguel Ojeda * 61a3f8a30fSMiguel Ojeda * Beware: Do not apply this to functions which may return 62a3f8a30fSMiguel Ojeda * ERR_PTRs. Also, it is probably unwise to apply it to functions 63a3f8a30fSMiguel Ojeda * returning extra information in the low bits (but in that case the 64a3f8a30fSMiguel Ojeda * compiler should see some alignment anyway, when the return value is 65a3f8a30fSMiguel Ojeda * massaged by 'flags = ptr & 3; ptr &= ~3;'). 66a3f8a30fSMiguel Ojeda * 67a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-assume_005faligned-function-attribute 68a3f8a30fSMiguel Ojeda * clang: https://clang.llvm.org/docs/AttributeReference.html#assume-aligned 69a3f8a30fSMiguel Ojeda */ 70a3f8a30fSMiguel Ojeda #define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__))) 71a3f8a30fSMiguel Ojeda 72a3f8a30fSMiguel Ojeda /* 7354da6a09SPeter Zijlstra * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-cleanup-variable-attribute 7454da6a09SPeter Zijlstra * clang: https://clang.llvm.org/docs/AttributeReference.html#cleanup 7554da6a09SPeter Zijlstra */ 7654da6a09SPeter Zijlstra #define __cleanup(func) __attribute__((__cleanup__(func))) 7754da6a09SPeter Zijlstra 7854da6a09SPeter Zijlstra /* 79a3f8a30fSMiguel Ojeda * Note the long name. 80a3f8a30fSMiguel Ojeda * 81a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute 82a3f8a30fSMiguel Ojeda */ 83a3f8a30fSMiguel Ojeda #define __attribute_const__ __attribute__((__const__)) 84a3f8a30fSMiguel Ojeda 85a3f8a30fSMiguel Ojeda /* 86c0d9782fSMiguel Ojeda * Optional: only supported since gcc >= 9 87c0d9782fSMiguel Ojeda * Optional: not supported by clang 88c0d9782fSMiguel Ojeda * 89c0d9782fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-copy-function-attribute 90c0d9782fSMiguel Ojeda */ 91c0d9782fSMiguel Ojeda #if __has_attribute(__copy__) 92c0d9782fSMiguel Ojeda # define __copy(symbol) __attribute__((__copy__(symbol))) 93c0d9782fSMiguel Ojeda #else 94c0d9782fSMiguel Ojeda # define __copy(symbol) 95c0d9782fSMiguel Ojeda #endif 96c0d9782fSMiguel Ojeda 97c0d9782fSMiguel Ojeda /* 981c7f4e5cSKees Cook * Optional: not supported by gcc 991c7f4e5cSKees Cook * Optional: only supported since clang >= 14.0 1001c7f4e5cSKees Cook * 1011c7f4e5cSKees Cook * clang: https://clang.llvm.org/docs/AttributeReference.html#diagnose_as_builtin 1021c7f4e5cSKees Cook */ 1031c7f4e5cSKees Cook #if __has_attribute(__diagnose_as_builtin__) 1041c7f4e5cSKees Cook # define __diagnose_as(builtin...) __attribute__((__diagnose_as_builtin__(builtin))) 1051c7f4e5cSKees Cook #else 1061c7f4e5cSKees Cook # define __diagnose_as(builtin...) 1071c7f4e5cSKees Cook #endif 1081c7f4e5cSKees Cook 1091c7f4e5cSKees Cook /* 110a3f8a30fSMiguel Ojeda * Don't. Just don't. See commit 771c035372a0 ("deprecate the '__deprecated' 111a3f8a30fSMiguel Ojeda * attribute warnings entirely and for good") for more information. 112a3f8a30fSMiguel Ojeda * 113a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute 114a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-deprecated-type-attribute 115a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-deprecated-variable-attribute 116a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html#index-deprecated-enumerator-attribute 117a3f8a30fSMiguel Ojeda * clang: https://clang.llvm.org/docs/AttributeReference.html#deprecated 118a3f8a30fSMiguel Ojeda */ 119a3f8a30fSMiguel Ojeda #define __deprecated 120a3f8a30fSMiguel Ojeda 121a3f8a30fSMiguel Ojeda /* 122a3f8a30fSMiguel Ojeda * Optional: not supported by clang 123a3f8a30fSMiguel Ojeda * 124a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-designated_005finit-type-attribute 125a3f8a30fSMiguel Ojeda */ 126a3f8a30fSMiguel Ojeda #if __has_attribute(__designated_init__) 127a3f8a30fSMiguel Ojeda # define __designated_init __attribute__((__designated_init__)) 128a3f8a30fSMiguel Ojeda #else 129a3f8a30fSMiguel Ojeda # define __designated_init 130a3f8a30fSMiguel Ojeda #endif 131a3f8a30fSMiguel Ojeda 132a3f8a30fSMiguel Ojeda /* 133b83a9084SNick Desaulniers * Optional: only supported since clang >= 14.0 134b83a9084SNick Desaulniers * 135b83a9084SNick Desaulniers * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-error-function-attribute 136b83a9084SNick Desaulniers */ 137b83a9084SNick Desaulniers #if __has_attribute(__error__) 138b83a9084SNick Desaulniers # define __compiletime_error(msg) __attribute__((__error__(msg))) 139b83a9084SNick Desaulniers #else 140b83a9084SNick Desaulniers # define __compiletime_error(msg) 141b83a9084SNick Desaulniers #endif 142b83a9084SNick Desaulniers 143b83a9084SNick Desaulniers /* 144a3f8a30fSMiguel Ojeda * Optional: not supported by clang 145a3f8a30fSMiguel Ojeda * 146a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-externally_005fvisible-function-attribute 147a3f8a30fSMiguel Ojeda */ 148a3f8a30fSMiguel Ojeda #if __has_attribute(__externally_visible__) 149a3f8a30fSMiguel Ojeda # define __visible __attribute__((__externally_visible__)) 150a3f8a30fSMiguel Ojeda #else 151a3f8a30fSMiguel Ojeda # define __visible 152a3f8a30fSMiguel Ojeda #endif 153a3f8a30fSMiguel Ojeda 154a3f8a30fSMiguel Ojeda /* 155a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format-function-attribute 156a3f8a30fSMiguel Ojeda * clang: https://clang.llvm.org/docs/AttributeReference.html#format 157a3f8a30fSMiguel Ojeda */ 158a3f8a30fSMiguel Ojeda #define __printf(a, b) __attribute__((__format__(printf, a, b))) 159a3f8a30fSMiguel Ojeda #define __scanf(a, b) __attribute__((__format__(scanf, a, b))) 160a3f8a30fSMiguel Ojeda 161a3f8a30fSMiguel Ojeda /* 162a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-gnu_005finline-function-attribute 163a3f8a30fSMiguel Ojeda * clang: https://clang.llvm.org/docs/AttributeReference.html#gnu-inline 164a3f8a30fSMiguel Ojeda */ 165a3f8a30fSMiguel Ojeda #define __gnu_inline __attribute__((__gnu_inline__)) 166a3f8a30fSMiguel Ojeda 167a3f8a30fSMiguel Ojeda /* 168a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute 16986cffecdSKees Cook * clang: https://clang.llvm.org/docs/AttributeReference.html#malloc 170a3f8a30fSMiguel Ojeda */ 171a3f8a30fSMiguel Ojeda #define __malloc __attribute__((__malloc__)) 172a3f8a30fSMiguel Ojeda 173a3f8a30fSMiguel Ojeda /* 174a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-mode-type-attribute 175a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-mode-variable-attribute 176a3f8a30fSMiguel Ojeda */ 177a3f8a30fSMiguel Ojeda #define __mode(x) __attribute__((__mode__(x))) 178a3f8a30fSMiguel Ojeda 179a3f8a30fSMiguel Ojeda /* 180feee1b8cSAlexander Popov * Optional: only supported since gcc >= 7 181feee1b8cSAlexander Popov * 182feee1b8cSAlexander Popov * gcc: https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html#index-no_005fcaller_005fsaved_005fregisters-function-attribute_002c-x86 183feee1b8cSAlexander Popov * clang: https://clang.llvm.org/docs/AttributeReference.html#no-caller-saved-registers 184feee1b8cSAlexander Popov */ 185feee1b8cSAlexander Popov #if __has_attribute(__no_caller_saved_registers__) 186feee1b8cSAlexander Popov # define __no_caller_saved_registers __attribute__((__no_caller_saved_registers__)) 187feee1b8cSAlexander Popov #else 188feee1b8cSAlexander Popov # define __no_caller_saved_registers 189feee1b8cSAlexander Popov #endif 190feee1b8cSAlexander Popov 191feee1b8cSAlexander Popov /* 192a3f8a30fSMiguel Ojeda * Optional: not supported by clang 193a3f8a30fSMiguel Ojeda * 194a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noclone-function-attribute 195a3f8a30fSMiguel Ojeda */ 196a3f8a30fSMiguel Ojeda #if __has_attribute(__noclone__) 197a3f8a30fSMiguel Ojeda # define __noclone __attribute__((__noclone__)) 198a3f8a30fSMiguel Ojeda #else 199a3f8a30fSMiguel Ojeda # define __noclone 200a3f8a30fSMiguel Ojeda #endif 201a3f8a30fSMiguel Ojeda 202a3f8a30fSMiguel Ojeda /* 203294f69e6SJoe Perches * Add the pseudo keyword 'fallthrough' so case statement blocks 204294f69e6SJoe Perches * must end with any of these keywords: 205294f69e6SJoe Perches * break; 206294f69e6SJoe Perches * fallthrough; 207ca0760e7SWei Ming Chen * continue; 208294f69e6SJoe Perches * goto <label>; 209294f69e6SJoe Perches * return [expression]; 210294f69e6SJoe Perches * 211294f69e6SJoe Perches * gcc: https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#Statement-Attributes 212294f69e6SJoe Perches */ 213294f69e6SJoe Perches #if __has_attribute(__fallthrough__) 214294f69e6SJoe Perches # define fallthrough __attribute__((__fallthrough__)) 215294f69e6SJoe Perches #else 216294f69e6SJoe Perches # define fallthrough do {} while (0) /* fallthrough */ 217294f69e6SJoe Perches #endif 218294f69e6SJoe Perches 219294f69e6SJoe Perches /* 220258e0815SDennis Zhou * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes 221258e0815SDennis Zhou * clang: https://clang.llvm.org/docs/AttributeReference.html#flatten 222258e0815SDennis Zhou */ 223258e0815SDennis Zhou # define __flatten __attribute__((flatten)) 224258e0815SDennis Zhou 225258e0815SDennis Zhou /* 226a3f8a30fSMiguel Ojeda * Note the missing underscores. 227a3f8a30fSMiguel Ojeda * 228a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute 229a3f8a30fSMiguel Ojeda * clang: mentioned 230a3f8a30fSMiguel Ojeda */ 231a3f8a30fSMiguel Ojeda #define noinline __attribute__((__noinline__)) 232a3f8a30fSMiguel Ojeda 233a3f8a30fSMiguel Ojeda /* 23492676236SMiguel Ojeda * Optional: only supported since gcc >= 8 23592676236SMiguel Ojeda * Optional: not supported by clang 23692676236SMiguel Ojeda * 23792676236SMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute 23892676236SMiguel Ojeda */ 23992676236SMiguel Ojeda #if __has_attribute(__nonstring__) 24092676236SMiguel Ojeda # define __nonstring __attribute__((__nonstring__)) 24192676236SMiguel Ojeda #else 24292676236SMiguel Ojeda # define __nonstring 24392676236SMiguel Ojeda #endif 24492676236SMiguel Ojeda 24592676236SMiguel Ojeda /* 246380d53c4SNick Desaulniers * Optional: only supported since GCC >= 7.1, clang >= 13.0. 247380d53c4SNick Desaulniers * 248380d53c4SNick Desaulniers * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fprofile_005finstrument_005ffunction-function-attribute 249380d53c4SNick Desaulniers * clang: https://clang.llvm.org/docs/AttributeReference.html#no-profile-instrument-function 250380d53c4SNick Desaulniers */ 251380d53c4SNick Desaulniers #if __has_attribute(__no_profile_instrument_function__) 252380d53c4SNick Desaulniers # define __no_profile __attribute__((__no_profile_instrument_function__)) 253380d53c4SNick Desaulniers #else 254380d53c4SNick Desaulniers # define __no_profile 255380d53c4SNick Desaulniers #endif 256380d53c4SNick Desaulniers 257380d53c4SNick Desaulniers /* 258a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute 259a3f8a30fSMiguel Ojeda * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn 260a3f8a30fSMiguel Ojeda * clang: https://clang.llvm.org/docs/AttributeReference.html#id1 261a3f8a30fSMiguel Ojeda */ 262a3f8a30fSMiguel Ojeda #define __noreturn __attribute__((__noreturn__)) 263a3f8a30fSMiguel Ojeda 264a3f8a30fSMiguel Ojeda /* 265514ca14eS[email protected] * Optional: only supported since GCC >= 11.1, clang >= 7.0. 266514ca14eS[email protected] * 267514ca14eS[email protected] * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fstack_005fprotector-function-attribute 268514ca14eS[email protected] * clang: https://clang.llvm.org/docs/AttributeReference.html#no-stack-protector-safebuffers 269514ca14eS[email protected] */ 270514ca14eS[email protected] #if __has_attribute(__no_stack_protector__) 271514ca14eS[email protected] # define __no_stack_protector __attribute__((__no_stack_protector__)) 272514ca14eS[email protected] #else 273514ca14eS[email protected] # define __no_stack_protector 274514ca14eS[email protected] #endif 275514ca14eS[email protected] 276514ca14eS[email protected] /* 277d694dbaeSKees Cook * Optional: not supported by gcc. 278d694dbaeSKees Cook * 279d694dbaeSKees Cook * clang: https://clang.llvm.org/docs/AttributeReference.html#overloadable 280d694dbaeSKees Cook */ 281d694dbaeSKees Cook #if __has_attribute(__overloadable__) 282d694dbaeSKees Cook # define __overloadable __attribute__((__overloadable__)) 283d694dbaeSKees Cook #else 284d694dbaeSKees Cook # define __overloadable 285d694dbaeSKees Cook #endif 286d694dbaeSKees Cook 287d694dbaeSKees Cook /* 288a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute 289a3f8a30fSMiguel Ojeda * clang: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute 290a3f8a30fSMiguel Ojeda */ 291a3f8a30fSMiguel Ojeda #define __packed __attribute__((__packed__)) 292a3f8a30fSMiguel Ojeda 293a3f8a30fSMiguel Ojeda /* 294f0202b8cSKees Cook * Note: the "type" argument should match any __builtin_object_size(p, type) usage. 295f0202b8cSKees Cook * 296f0202b8cSKees Cook * Optional: not supported by gcc. 297f0202b8cSKees Cook * 298f0202b8cSKees Cook * clang: https://clang.llvm.org/docs/AttributeReference.html#pass-object-size-pass-dynamic-object-size 299f0202b8cSKees Cook */ 300439a1bcaSKees Cook #if __has_attribute(__pass_dynamic_object_size__) 301439a1bcaSKees Cook # define __pass_dynamic_object_size(type) __attribute__((__pass_dynamic_object_size__(type))) 302439a1bcaSKees Cook #else 303439a1bcaSKees Cook # define __pass_dynamic_object_size(type) 304439a1bcaSKees Cook #endif 305f0202b8cSKees Cook #if __has_attribute(__pass_object_size__) 306f0202b8cSKees Cook # define __pass_object_size(type) __attribute__((__pass_object_size__(type))) 307f0202b8cSKees Cook #else 308f0202b8cSKees Cook # define __pass_object_size(type) 309f0202b8cSKees Cook #endif 310f0202b8cSKees Cook 311f0202b8cSKees Cook /* 312a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute 313a3f8a30fSMiguel Ojeda */ 314a3f8a30fSMiguel Ojeda #define __pure __attribute__((__pure__)) 315a3f8a30fSMiguel Ojeda 316a3f8a30fSMiguel Ojeda /* 317a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-section-function-attribute 318a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-section-variable-attribute 319a3f8a30fSMiguel Ojeda * clang: https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate 320a3f8a30fSMiguel Ojeda */ 32133def849SJoe Perches #define __section(section) __attribute__((__section__(section))) 322a3f8a30fSMiguel Ojeda 323a3f8a30fSMiguel Ojeda /* 324fd7eea27SHeiko Carstens * Optional: only supported since gcc >= 12 325fd7eea27SHeiko Carstens * 326fd7eea27SHeiko Carstens * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-uninitialized-variable-attribute 327fd7eea27SHeiko Carstens * clang: https://clang.llvm.org/docs/AttributeReference.html#uninitialized 328fd7eea27SHeiko Carstens */ 329fd7eea27SHeiko Carstens #if __has_attribute(__uninitialized__) 330fd7eea27SHeiko Carstens # define __uninitialized __attribute__((__uninitialized__)) 331fd7eea27SHeiko Carstens #else 332fd7eea27SHeiko Carstens # define __uninitialized 333fd7eea27SHeiko Carstens #endif 334fd7eea27SHeiko Carstens 335fd7eea27SHeiko Carstens /* 336a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute 337a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute 338a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-unused-variable-attribute 339a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-unused-label-attribute 340a3f8a30fSMiguel Ojeda * clang: https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused 341a3f8a30fSMiguel Ojeda */ 342a3f8a30fSMiguel Ojeda #define __always_unused __attribute__((__unused__)) 343a3f8a30fSMiguel Ojeda #define __maybe_unused __attribute__((__unused__)) 344a3f8a30fSMiguel Ojeda 345a3f8a30fSMiguel Ojeda /* 346a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-used-function-attribute 347a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-used-variable-attribute 348a3f8a30fSMiguel Ojeda */ 349a3f8a30fSMiguel Ojeda #define __used __attribute__((__used__)) 350a3f8a30fSMiguel Ojeda 351a3f8a30fSMiguel Ojeda /* 352*efe3a85eSYury Norov * The __used attribute guarantees that the attributed variable will be 353*efe3a85eSYury Norov * always emitted by a compiler. It doesn't prevent the compiler from 354*efe3a85eSYury Norov * throwing 'unused' warnings when it can't detect how the variable is 355*efe3a85eSYury Norov * actually used. It's a compiler implementation details either emit 356*efe3a85eSYury Norov * the warning in that case or not. 357*efe3a85eSYury Norov * 358*efe3a85eSYury Norov * The combination of both 'used' and 'unused' attributes ensures that 359*efe3a85eSYury Norov * the variable would be emitted, and will not trigger 'unused' warnings. 360*efe3a85eSYury Norov * The attribute is applicable for functions, static and global variables. 361*efe3a85eSYury Norov */ 362*efe3a85eSYury Norov #define __always_used __used __maybe_unused 363*efe3a85eSYury Norov 364*efe3a85eSYury Norov /* 36519679394SMasahiro Yamada * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warn_005funused_005fresult-function-attribute 36619679394SMasahiro Yamada * clang: https://clang.llvm.org/docs/AttributeReference.html#nodiscard-warn-unused-result 36719679394SMasahiro Yamada */ 36819679394SMasahiro Yamada #define __must_check __attribute__((__warn_unused_result__)) 36919679394SMasahiro Yamada 37019679394SMasahiro Yamada /* 371b83a9084SNick Desaulniers * Optional: only supported since clang >= 14.0 372b83a9084SNick Desaulniers * 373b83a9084SNick Desaulniers * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warning-function-attribute 374b83a9084SNick Desaulniers */ 375b83a9084SNick Desaulniers #if __has_attribute(__warning__) 376b83a9084SNick Desaulniers # define __compiletime_warning(msg) __attribute__((__warning__(msg))) 377b83a9084SNick Desaulniers #else 378b83a9084SNick Desaulniers # define __compiletime_warning(msg) 379b83a9084SNick Desaulniers #endif 380b83a9084SNick Desaulniers 381b83a9084SNick Desaulniers /* 382a015b708SAlexander Potapenko * Optional: only supported since clang >= 14.0 383a015b708SAlexander Potapenko * 384a015b708SAlexander Potapenko * clang: https://clang.llvm.org/docs/AttributeReference.html#disable-sanitizer-instrumentation 385a015b708SAlexander Potapenko * 386a015b708SAlexander Potapenko * disable_sanitizer_instrumentation is not always similar to 387a015b708SAlexander Potapenko * no_sanitize((<sanitizer-name>)): the latter may still let specific sanitizers 388a015b708SAlexander Potapenko * insert code into functions to prevent false positives. Unlike that, 389a015b708SAlexander Potapenko * disable_sanitizer_instrumentation prevents all kinds of instrumentation to 390a015b708SAlexander Potapenko * functions with the attribute. 391a015b708SAlexander Potapenko */ 392a015b708SAlexander Potapenko #if __has_attribute(disable_sanitizer_instrumentation) 393a015b708SAlexander Potapenko # define __disable_sanitizer_instrumentation \ 394a015b708SAlexander Potapenko __attribute__((disable_sanitizer_instrumentation)) 395a015b708SAlexander Potapenko #else 396a015b708SAlexander Potapenko # define __disable_sanitizer_instrumentation 397a015b708SAlexander Potapenko #endif 398a015b708SAlexander Potapenko 399a015b708SAlexander Potapenko /* 400a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute 401a3f8a30fSMiguel Ojeda * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute 402a3f8a30fSMiguel Ojeda */ 403a3f8a30fSMiguel Ojeda #define __weak __attribute__((__weak__)) 404a3f8a30fSMiguel Ojeda 405c205cc75SMenglong Dong /* 406c205cc75SMenglong Dong * Used by functions that use '__builtin_return_address'. These function 407c205cc75SMenglong Dong * don't want to be splited or made inline, which can make 408c205cc75SMenglong Dong * the '__builtin_return_address' get unexpected address. 409c205cc75SMenglong Dong */ 410c205cc75SMenglong Dong #define __fix_address noinline __noclone 411c205cc75SMenglong Dong 412a3f8a30fSMiguel Ojeda #endif /* __LINUX_COMPILER_ATTRIBUTES_H */ 413