blob: 6b28c1b7310c0176bddb490f9443eb15b101400e [file] [log] [blame]
Miguel Ojedaa3f8a302018-08-30 20:36:59 +02001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __LINUX_COMPILER_ATTRIBUTES_H
3#define __LINUX_COMPILER_ATTRIBUTES_H
4
5/*
6 * The attributes in this file are unconditionally defined and they directly
7 * map to compiler attribute(s) -- except those that are optional.
8 *
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).
12 *
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.
17 */
18
19/*
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.
23 *
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
28 * defined to 0.
29 */
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
Miguel Ojeda92676232018-09-19 01:06:24 +020037# define __GCC4_has_attribute___nonstring__ 0
Miguel Ojedaa3f8a302018-08-30 20:36:59 +020038# define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
39#endif
40
41/*
42 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute
43 */
44#define __alias(symbol) __attribute__((__alias__(#symbol)))
45
46/*
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
50 */
51#define __aligned(x) __attribute__((__aligned__(x)))
52#define __aligned_largest __attribute__((__aligned__))
53
54/*
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).
59 *
60 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute
61 * clang: mentioned
62 */
63#define __always_inline inline __attribute__((__always_inline__))
64
65/*
66 * The second argument is optional (default 0), so we use a variadic macro
67 * to make the shorthand.
68 *
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;').
74 *
75 * Optional: only supported since gcc >= 4.9
76 * Optional: not supported by icc
77 *
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
80 */
81#if __has_attribute(__assume_aligned__)
82# define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
83#else
84# define __assume_aligned(a, ...)
85#endif
86
87/*
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
90 */
91#define __cold __attribute__((__cold__))
92
93/*
94 * Note the long name.
95 *
96 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute
97 */
98#define __attribute_const__ __attribute__((__const__))
99
100/*
101 * Don't. Just don't. See commit 771c035372a0 ("deprecate the '__deprecated'
102 * attribute warnings entirely and for good") for more information.
103 *
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
109 */
110#define __deprecated
111
112/*
113 * Optional: only supported since gcc >= 5.1
114 * Optional: not supported by clang
115 * Optional: not supported by icc
116 *
117 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-designated_005finit-type-attribute
118 */
119#if __has_attribute(__designated_init__)
120# define __designated_init __attribute__((__designated_init__))
121#else
122# define __designated_init
123#endif
124
125/*
126 * Optional: not supported by clang
127 *
128 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-externally_005fvisible-function-attribute
129 */
130#if __has_attribute(__externally_visible__)
131# define __visible __attribute__((__externally_visible__))
132#else
133# define __visible
134#endif
135
136/*
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
139 */
140#define __printf(a, b) __attribute__((__format__(printf, a, b)))
141#define __scanf(a, b) __attribute__((__format__(scanf, a, b)))
142
143/*
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
146 */
147#define __gnu_inline __attribute__((__gnu_inline__))
148
149/*
150 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute
151 */
152#define __malloc __attribute__((__malloc__))
153
154/*
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
157 */
158#define __mode(x) __attribute__((__mode__(x)))
159
160/*
161 * Optional: not supported by clang
162 * Note: icc does not recognize gcc's no-tracer
163 *
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
166 */
167#if __has_attribute(__noclone__)
168# if __has_attribute(__optimize__)
169# define __noclone __attribute__((__noclone__, __optimize__("no-tracer")))
170# else
171# define __noclone __attribute__((__noclone__))
172# endif
173#else
174# define __noclone
175#endif
176
177/*
178 * Note the missing underscores.
179 *
180 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute
181 * clang: mentioned
182 */
183#define noinline __attribute__((__noinline__))
184
185/*
Miguel Ojeda92676232018-09-19 01:06:24 +0200186 * Optional: only supported since gcc >= 8
187 * Optional: not supported by clang
188 * Optional: not supported by icc
189 *
190 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute
191 */
192#if __has_attribute(__nonstring__)
193# define __nonstring __attribute__((__nonstring__))
194#else
195# define __nonstring
196#endif
197
198/*
Miguel Ojedaa3f8a302018-08-30 20:36:59 +0200199 * 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
202 */
203#define __noreturn __attribute__((__noreturn__))
204
205/*
206 * Optional: only supported since gcc >= 4.8
207 * Optional: not supported by icc
208 *
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
211 */
212#if __has_attribute(__no_sanitize_address__)
213# define __no_sanitize_address __attribute__((__no_sanitize_address__))
214#else
215# define __no_sanitize_address
216#endif
217
218/*
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
221 */
222#define __packed __attribute__((__packed__))
223
224/*
225 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute
226 */
227#define __pure __attribute__((__pure__))
228
229/*
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
233 */
234#define __section(S) __attribute__((__section__(#S)))
235
236/*
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
242 */
243#define __always_unused __attribute__((__unused__))
244#define __maybe_unused __attribute__((__unused__))
245
246/*
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
249 */
250#define __used __attribute__((__used__))
251
252/*
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
255 */
256#define __weak __attribute__((__weak__))
257
258#endif /* __LINUX_COMPILER_ATTRIBUTES_H */