1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_COMPILER_ATTRIBUTES_H
3 #define __LINUX_COMPILER_ATTRIBUTES_H
6 * The attributes in this file are unconditionally defined and they directly
7 * map to compiler attribute(s) -- except those that are optional.
9 * Any other "attributes" (i.e. those that depend on a configuration option,
10 * on a compiler, on an architecture, on plugins, on other attributes...)
11 * should be defined elsewhere (e.g. compiler_types.h or compiler-*.h).
13 * This file is meant to be sorted (by actual attribute name,
14 * not by #define identifier). Use the __attribute__((__name__)) syntax
15 * (i.e. with underscores) to avoid future collisions with other macros.
16 * If an attribute is optional, state the reason in the comment.
20 * To check for optional attributes, we use __has_attribute, which is supported
21 * on gcc >= 5, clang >= 2.9 and icc >= 17. In the meantime, to support
22 * 4.6 <= gcc < 5, we implement __has_attribute by hand.
24 * sparse does not support __has_attribute (yet) and defines __GNUC_MINOR__
25 * depending on the compiler used to build it; however, these attributes have
26 * no semantic effects for sparse, so it does not matter. Also note that,
27 * in order to avoid sparse's warnings, even the unsupported ones must be
30 #ifndef __has_attribute
31 # define __has_attribute(x) __GCC4_has_attribute_##x
32 # define __GCC4_has_attribute___assume_aligned__ (__GNUC_MINOR__ >= 9)
33 # define __GCC4_has_attribute___designated_init__ 0
34 # define __GCC4_has_attribute___externally_visible__ 1
35 # define __GCC4_has_attribute___noclone__ 1
36 # define __GCC4_has_attribute___optimize__ 1
37 # define __GCC4_has_attribute___nonstring__ 0
38 # define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
42 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute
44 #define __alias(symbol) __attribute__((__alias__(#symbol)))
47 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-aligned-function-attribute
48 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute
49 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-aligned-variable-attribute
51 #define __aligned(x) __attribute__((__aligned__(x)))
52 #define __aligned_largest __attribute__((__aligned__))
55 * Note: users of __always_inline currently do not write "inline" themselves,
56 * which seems to be required by gcc to apply the attribute according
57 * to its docs (and also "warning: always_inline function might not be
58 * inlinable [-Wattributes]" is emitted).
60 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute
63 #define __always_inline inline __attribute__((__always_inline__))
66 * The second argument is optional (default 0), so we use a variadic macro
67 * to make the shorthand.
69 * Beware: Do not apply this to functions which may return
70 * ERR_PTRs. Also, it is probably unwise to apply it to functions
71 * returning extra information in the low bits (but in that case the
72 * compiler should see some alignment anyway, when the return value is
73 * massaged by 'flags = ptr & 3; ptr &= ~3;').
75 * Optional: only supported since gcc >= 4.9
76 * Optional: not supported by icc
78 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-assume_005faligned-function-attribute
79 * clang: https://clang.llvm.org/docs/AttributeReference.html#assume-aligned
81 #if __has_attribute(__assume_aligned__)
82 # define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
84 # define __assume_aligned(a, ...)
88 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-cold-function-attribute
89 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
91 #define __cold __attribute__((__cold__))
96 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute
98 #define __attribute_const__ __attribute__((__const__))
101 * Don't. Just don't. See commit 771c035372a0 ("deprecate the '__deprecated'
102 * attribute warnings entirely and for good") for more information.
104 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute
105 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-deprecated-type-attribute
106 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-deprecated-variable-attribute
107 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html#index-deprecated-enumerator-attribute
108 * clang: https://clang.llvm.org/docs/AttributeReference.html#deprecated
113 * Optional: only supported since gcc >= 5.1
114 * Optional: not supported by clang
115 * Optional: not supported by icc
117 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-designated_005finit-type-attribute
119 #if __has_attribute(__designated_init__)
120 # define __designated_init __attribute__((__designated_init__))
122 # define __designated_init
126 * Optional: not supported by clang
128 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-externally_005fvisible-function-attribute
130 #if __has_attribute(__externally_visible__)
131 # define __visible __attribute__((__externally_visible__))
137 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format-function-attribute
138 * clang: https://clang.llvm.org/docs/AttributeReference.html#format
140 #define __printf(a, b) __attribute__((__format__(printf, a, b)))
141 #define __scanf(a, b) __attribute__((__format__(scanf, a, b)))
144 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-gnu_005finline-function-attribute
145 * clang: https://clang.llvm.org/docs/AttributeReference.html#gnu-inline
147 #define __gnu_inline __attribute__((__gnu_inline__))
150 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute
152 #define __malloc __attribute__((__malloc__))
155 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-mode-type-attribute
156 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-mode-variable-attribute
158 #define __mode(x) __attribute__((__mode__(x)))
161 * Optional: not supported by clang
162 * Note: icc does not recognize gcc's no-tracer
164 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noclone-function-attribute
165 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-optimize-function-attribute
167 #if __has_attribute(__noclone__)
168 # if __has_attribute(__optimize__)
169 # define __noclone __attribute__((__noclone__, __optimize__("no-tracer")))
171 # define __noclone __attribute__((__noclone__))
178 * Note the missing underscores.
180 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute
183 #define noinline __attribute__((__noinline__))
186 * Optional: only supported since gcc >= 8
187 * Optional: not supported by clang
188 * Optional: not supported by icc
190 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute
192 #if __has_attribute(__nonstring__)
193 # define __nonstring __attribute__((__nonstring__))
199 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute
200 * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn
201 * clang: https://clang.llvm.org/docs/AttributeReference.html#id1
203 #define __noreturn __attribute__((__noreturn__))
206 * Optional: only supported since gcc >= 4.8
207 * Optional: not supported by icc
209 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fsanitize_005faddress-function-attribute
210 * clang: https://clang.llvm.org/docs/AttributeReference.html#no-sanitize-address-no-address-safety-analysis
212 #if __has_attribute(__no_sanitize_address__)
213 # define __no_sanitize_address __attribute__((__no_sanitize_address__))
215 # define __no_sanitize_address
219 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute
220 * clang: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute
222 #define __packed __attribute__((__packed__))
225 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute
227 #define __pure __attribute__((__pure__))
230 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-section-function-attribute
231 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-section-variable-attribute
232 * clang: https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate
234 #define __section(S) __attribute__((__section__(#S)))
237 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute
238 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute
239 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-unused-variable-attribute
240 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-unused-label-attribute
241 * clang: https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused
243 #define __always_unused __attribute__((__unused__))
244 #define __maybe_unused __attribute__((__unused__))
247 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-used-function-attribute
248 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-used-variable-attribute
250 #define __used __attribute__((__used__))
253 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
254 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
256 #define __weak __attribute__((__weak__))
258 #endif /* __LINUX_COMPILER_ATTRIBUTES_H */