xref: /linux/include/linux/compiler_attributes.h (revision 764e77d868a5b932c709e20ddb5993f9111a841c)
1 /* 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), unless one of the compilers does not support
8  * the attribute. In that case, __has_attribute is used to check for support
9  * and the reason is stated in its comment ("Optional: ...").
10  *
11  * Any other "attributes" (i.e. those that depend on a configuration option,
12  * on a compiler, on an architecture, on plugins, on other attributes...)
13  * should be defined elsewhere (e.g. compiler_types.h or compiler-*.h).
14  * The intention is to keep this file as simple as possible, as well as
15  * compiler- and version-agnostic (e.g. avoiding GCC_VERSION checks).
16  *
17  * This file is meant to be sorted (by actual attribute name,
18  * not by #define identifier). Use the __attribute__((__name__)) syntax
19  * (i.e. with underscores) to avoid future collisions with other macros.
20  * Provide links to the documentation of each supported compiler, if it exists.
21  */
22 
23 /*
24  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute
25  */
26 #define __alias(symbol)                 __attribute__((__alias__(#symbol)))
27 
28 /*
29  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-aligned-function-attribute
30  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute
31  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-aligned-variable-attribute
32  */
33 #define __aligned(x)                    __attribute__((__aligned__(x)))
34 #define __aligned_largest               __attribute__((__aligned__))
35 
36 /*
37  * Note: do not use this directly. Instead, use __alloc_size() since it is conditionally
38  * available and includes other attributes. For GCC < 9.1, __alloc_size__ gets undefined
39  * in compiler-gcc.h, due to misbehaviors.
40  *
41  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute
42  * clang: https://clang.llvm.org/docs/AttributeReference.html#alloc-size
43  */
44 #define __alloc_size__(x, ...)		__attribute__((__alloc_size__(x, ## __VA_ARGS__)))
45 
46 /*
47  * Note: users of __always_inline currently do not write "inline" themselves,
48  * which seems to be required by gcc to apply the attribute according
49  * to its docs (and also "warning: always_inline function might not be
50  * inlinable [-Wattributes]" is emitted).
51  *
52  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute
53  * clang: mentioned
54  */
55 #define __always_inline                 inline __attribute__((__always_inline__))
56 
57 /*
58  * The second argument is optional (default 0), so we use a variadic macro
59  * to make the shorthand.
60  *
61  * Beware: Do not apply this to functions which may return
62  * ERR_PTRs. Also, it is probably unwise to apply it to functions
63  * returning extra information in the low bits (but in that case the
64  * compiler should see some alignment anyway, when the return value is
65  * massaged by 'flags = ptr & 3; ptr &= ~3;').
66  *
67  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-assume_005faligned-function-attribute
68  * clang: https://clang.llvm.org/docs/AttributeReference.html#assume-aligned
69  */
70 #define __assume_aligned(a, ...)        __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
71 
72 /*
73  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-cleanup-variable-attribute
74  * clang: https://clang.llvm.org/docs/AttributeReference.html#cleanup
75  */
76 #define __cleanup(func)			__attribute__((__cleanup__(func)))
77 
78 /*
79  * Note the long name.
80  *
81  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute
82  */
83 #define __attribute_const__             __attribute__((__const__))
84 
85 /*
86  * Optional: only supported since gcc >= 9
87  * Optional: not supported by clang
88  *
89  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-copy-function-attribute
90  */
91 #if __has_attribute(__copy__)
92 # define __copy(symbol)                 __attribute__((__copy__(symbol)))
93 #else
94 # define __copy(symbol)
95 #endif
96 
97 /*
98  * Optional: not supported by gcc
99  * Optional: only supported since clang >= 14.0
100  *
101  * clang: https://clang.llvm.org/docs/AttributeReference.html#diagnose_as_builtin
102  */
103 #if __has_attribute(__diagnose_as_builtin__)
104 # define __diagnose_as(builtin...)	__attribute__((__diagnose_as_builtin__(builtin)))
105 #else
106 # define __diagnose_as(builtin...)
107 #endif
108 
109 /*
110  * Don't. Just don't. See commit 771c035372a0 ("deprecate the '__deprecated'
111  * attribute warnings entirely and for good") for more information.
112  *
113  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute
114  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-deprecated-type-attribute
115  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-deprecated-variable-attribute
116  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html#index-deprecated-enumerator-attribute
117  * clang: https://clang.llvm.org/docs/AttributeReference.html#deprecated
118  */
119 #define __deprecated
120 
121 /*
122  * Optional: not supported by clang
123  *
124  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-designated_005finit-type-attribute
125  */
126 #if __has_attribute(__designated_init__)
127 # define __designated_init              __attribute__((__designated_init__))
128 #else
129 # define __designated_init
130 #endif
131 
132 /*
133  * Optional: only supported since clang >= 14.0
134  *
135  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-error-function-attribute
136  */
137 #if __has_attribute(__error__)
138 # define __compiletime_error(msg)       __attribute__((__error__(msg)))
139 #else
140 # define __compiletime_error(msg)
141 #endif
142 
143 /*
144  * Optional: not supported by clang
145  *
146  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-externally_005fvisible-function-attribute
147  */
148 #if __has_attribute(__externally_visible__)
149 # define __visible                      __attribute__((__externally_visible__))
150 #else
151 # define __visible
152 #endif
153 
154 /*
155  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format-function-attribute
156  * clang: https://clang.llvm.org/docs/AttributeReference.html#format
157  */
158 #define __printf(a, b)                  __attribute__((__format__(printf, a, b)))
159 #define __scanf(a, b)                   __attribute__((__format__(scanf, a, b)))
160 
161 /*
162  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-gnu_005finline-function-attribute
163  * clang: https://clang.llvm.org/docs/AttributeReference.html#gnu-inline
164  */
165 #define __gnu_inline                    __attribute__((__gnu_inline__))
166 
167 /*
168  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute
169  * clang: https://clang.llvm.org/docs/AttributeReference.html#malloc
170  */
171 #define __malloc                        __attribute__((__malloc__))
172 
173 /*
174  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-mode-type-attribute
175  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-mode-variable-attribute
176  */
177 #define __mode(x)                       __attribute__((__mode__(x)))
178 
179 /*
180  * Optional: only supported since gcc >= 7
181  *
182  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html#index-no_005fcaller_005fsaved_005fregisters-function-attribute_002c-x86
183  * clang: https://clang.llvm.org/docs/AttributeReference.html#no-caller-saved-registers
184  */
185 #if __has_attribute(__no_caller_saved_registers__)
186 # define __no_caller_saved_registers	__attribute__((__no_caller_saved_registers__))
187 #else
188 # define __no_caller_saved_registers
189 #endif
190 
191 /*
192  * Optional: not supported by clang
193  *
194  *  gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noclone-function-attribute
195  */
196 #if __has_attribute(__noclone__)
197 # define __noclone                      __attribute__((__noclone__))
198 #else
199 # define __noclone
200 #endif
201 
202 /*
203  * Add the pseudo keyword 'fallthrough' so case statement blocks
204  * must end with any of these keywords:
205  *   break;
206  *   fallthrough;
207  *   continue;
208  *   goto <label>;
209  *   return [expression];
210  *
211  *  gcc: https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#Statement-Attributes
212  */
213 #if __has_attribute(__fallthrough__)
214 # define fallthrough                    __attribute__((__fallthrough__))
215 #else
216 # define fallthrough                    do {} while (0)  /* fallthrough */
217 #endif
218 
219 /*
220  * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes
221  * clang: https://clang.llvm.org/docs/AttributeReference.html#flatten
222  */
223 # define __flatten			__attribute__((flatten))
224 
225 /*
226  * Note the missing underscores.
227  *
228  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute
229  * clang: mentioned
230  */
231 #define   noinline                      __attribute__((__noinline__))
232 
233 /*
234  * Note: deliberately not named '__nonnull', to avoid clashing with glibc's
235  * __nonnull() when kernel and userspace headers are combined.
236  *
237  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-nonnull
238  * clang: https://clang.llvm.org/docs/AttributeReference.html#nonnull
239  */
240 #define __nonnull_args(x...)		__attribute__((__nonnull__(x)))
241 
242 /*
243  * Optional: only supported since gcc >= 8
244  * Optional: not supported by clang
245  *
246  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute
247  */
248 #if __has_attribute(__nonstring__)
249 # define __nonstring                    __attribute__((__nonstring__))
250 #else
251 # define __nonstring
252 #endif
253 
254 /*
255  * Optional: only supported since GCC >= 7.1, clang >= 13.0.
256  *
257  *      gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fprofile_005finstrument_005ffunction-function-attribute
258  *    clang: https://clang.llvm.org/docs/AttributeReference.html#no-profile-instrument-function
259  */
260 #if __has_attribute(__no_profile_instrument_function__)
261 # define __no_profile                  __attribute__((__no_profile_instrument_function__))
262 #else
263 # define __no_profile
264 #endif
265 
266 /*
267  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute
268  * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn
269  * clang: https://clang.llvm.org/docs/AttributeReference.html#id1
270  */
271 #define __noreturn                      __attribute__((__noreturn__))
272 
273 /*
274  * Optional: only supported since GCC >= 11.1, clang >= 7.0.
275  *
276  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fstack_005fprotector-function-attribute
277  *   clang: https://clang.llvm.org/docs/AttributeReference.html#no-stack-protector-safebuffers
278  */
279 #if __has_attribute(__no_stack_protector__)
280 # define __no_stack_protector		__attribute__((__no_stack_protector__))
281 #else
282 # define __no_stack_protector
283 #endif
284 
285 /*
286  * Optional: not supported by gcc.
287  *
288  * clang: https://clang.llvm.org/docs/AttributeReference.html#overloadable
289  */
290 #if __has_attribute(__overloadable__)
291 # define __overloadable			__attribute__((__overloadable__))
292 #else
293 # define __overloadable
294 #endif
295 
296 /*
297  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute
298  * clang: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute
299  */
300 #define __packed                        __attribute__((__packed__))
301 
302 /*
303  * Note: the "type" argument should match any __builtin_object_size(p, type) usage.
304  *
305  * Optional: not supported by gcc.
306  *
307  * clang: https://clang.llvm.org/docs/AttributeReference.html#pass-object-size-pass-dynamic-object-size
308  */
309 #if __has_attribute(__pass_dynamic_object_size__)
310 # define __pass_dynamic_object_size(type)	__attribute__((__pass_dynamic_object_size__(type)))
311 #else
312 # define __pass_dynamic_object_size(type)
313 #endif
314 #if __has_attribute(__pass_object_size__)
315 # define __pass_object_size(type)	__attribute__((__pass_object_size__(type)))
316 #else
317 # define __pass_object_size(type)
318 #endif
319 
320 /*
321  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute
322  */
323 #define __pure                          __attribute__((__pure__))
324 
325 /*
326  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-section-function-attribute
327  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-section-variable-attribute
328  * clang: https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate
329  */
330 #define __section(section)              __attribute__((__section__(section)))
331 
332 /*
333  * Optional: only supported since gcc >= 12
334  *
335  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-uninitialized-variable-attribute
336  * clang: https://clang.llvm.org/docs/AttributeReference.html#uninitialized
337  */
338 #if __has_attribute(__uninitialized__)
339 # define __uninitialized		__attribute__((__uninitialized__))
340 #else
341 # define __uninitialized
342 #endif
343 
344 /*
345  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute
346  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute
347  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-unused-variable-attribute
348  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-unused-label-attribute
349  * clang: https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused
350  */
351 #define __always_unused                 __attribute__((__unused__))
352 #define __maybe_unused                  __attribute__((__unused__))
353 
354 /*
355  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-used-function-attribute
356  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-used-variable-attribute
357  */
358 #define __used                          __attribute__((__used__))
359 
360 /*
361  * The __used attribute guarantees that the attributed variable will be
362  * always emitted by a compiler. It doesn't prevent the compiler from
363  * throwing 'unused' warnings when it can't detect how the variable is
364  * actually used. It's a compiler implementation details either emit
365  * the warning in that case or not.
366  *
367  * The combination of both 'used' and 'unused' attributes ensures that
368  * the variable would be emitted, and will not trigger 'unused' warnings.
369  * The attribute is applicable for functions, static and global variables.
370  */
371 #define __always_used			__used __maybe_unused
372 
373 /*
374  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warn_005funused_005fresult-function-attribute
375  * clang: https://clang.llvm.org/docs/AttributeReference.html#nodiscard-warn-unused-result
376  */
377 #define __must_check                    __attribute__((__warn_unused_result__))
378 
379 /*
380  * Optional: only supported since clang >= 14.0
381  *
382  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warning-function-attribute
383  */
384 #if __has_attribute(__warning__)
385 # define __compiletime_warning(msg)     __attribute__((__warning__(msg)))
386 #else
387 # define __compiletime_warning(msg)
388 #endif
389 
390 /*
391  * Optional: only supported since clang >= 14.0
392  *
393  * clang: https://clang.llvm.org/docs/AttributeReference.html#disable-sanitizer-instrumentation
394  *
395  * disable_sanitizer_instrumentation is not always similar to
396  * no_sanitize((<sanitizer-name>)): the latter may still let specific sanitizers
397  * insert code into functions to prevent false positives. Unlike that,
398  * disable_sanitizer_instrumentation prevents all kinds of instrumentation to
399  * functions with the attribute.
400  */
401 #if __has_attribute(disable_sanitizer_instrumentation)
402 # define __disable_sanitizer_instrumentation \
403 	 __attribute__((disable_sanitizer_instrumentation))
404 #else
405 # define __disable_sanitizer_instrumentation
406 #endif
407 
408 /*
409  * Optional: not supported by clang
410  *
411  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-noipa
412  */
413 #if __has_attribute(noipa)
414 # define __noipa __attribute__((noipa))
415 #else
416 # define __noipa
417 #endif
418 
419 /*
420  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
421  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
422  */
423 #define __weak                          __attribute__((__weak__))
424 
425 /*
426  * Used by functions that use '__builtin_return_address'. These function
427  * don't want to be splited or made inline, which can make
428  * the '__builtin_return_address' get unexpected address.
429  */
430 #define __fix_address noinline __noclone
431 
432 #endif /* __LINUX_COMPILER_ATTRIBUTES_H */
433