xref: /OK3568_Linux_fs/kernel/include/linux/compiler_attributes.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __LINUX_COMPILER_ATTRIBUTES_H
3*4882a593Smuzhiyun #define __LINUX_COMPILER_ATTRIBUTES_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun  * The attributes in this file are unconditionally defined and they directly
7*4882a593Smuzhiyun  * map to compiler attribute(s), unless one of the compilers does not support
8*4882a593Smuzhiyun  * the attribute. In that case, __has_attribute is used to check for support
9*4882a593Smuzhiyun  * and the reason is stated in its comment ("Optional: ...").
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Any other "attributes" (i.e. those that depend on a configuration option,
12*4882a593Smuzhiyun  * on a compiler, on an architecture, on plugins, on other attributes...)
13*4882a593Smuzhiyun  * should be defined elsewhere (e.g. compiler_types.h or compiler-*.h).
14*4882a593Smuzhiyun  * The intention is to keep this file as simple as possible, as well as
15*4882a593Smuzhiyun  * compiler- and version-agnostic (e.g. avoiding GCC_VERSION checks).
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * This file is meant to be sorted (by actual attribute name,
18*4882a593Smuzhiyun  * not by #define identifier). Use the __attribute__((__name__)) syntax
19*4882a593Smuzhiyun  * (i.e. with underscores) to avoid future collisions with other macros.
20*4882a593Smuzhiyun  * Provide links to the documentation of each supported compiler, if it exists.
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun  * __has_attribute is supported on gcc >= 5, clang >= 2.9 and icc >= 17.
25*4882a593Smuzhiyun  * In the meantime, to support gcc < 5, we implement __has_attribute
26*4882a593Smuzhiyun  * by hand.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun #ifndef __has_attribute
29*4882a593Smuzhiyun # define __has_attribute(x) __GCC4_has_attribute_##x
30*4882a593Smuzhiyun # define __GCC4_has_attribute___assume_aligned__      (__GNUC_MINOR__ >= 9)
31*4882a593Smuzhiyun # define __GCC4_has_attribute___copy__                0
32*4882a593Smuzhiyun # define __GCC4_has_attribute___designated_init__     0
33*4882a593Smuzhiyun # define __GCC4_has_attribute___error__               1
34*4882a593Smuzhiyun # define __GCC4_has_attribute___externally_visible__  1
35*4882a593Smuzhiyun # define __GCC4_has_attribute___no_caller_saved_registers__ 0
36*4882a593Smuzhiyun # define __GCC4_has_attribute___noclone__             1
37*4882a593Smuzhiyun # define __GCC4_has_attribute___nonstring__           0
38*4882a593Smuzhiyun # define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
39*4882a593Smuzhiyun # define __GCC4_has_attribute___no_sanitize_undefined__ (__GNUC_MINOR__ >= 9)
40*4882a593Smuzhiyun # define __GCC4_has_attribute___fallthrough__         0
41*4882a593Smuzhiyun # define __GCC4_has_attribute___warning__             1
42*4882a593Smuzhiyun #endif
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun #define __alias(symbol)                 __attribute__((__alias__(#symbol)))
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-aligned-function-attribute
51*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute
52*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-aligned-variable-attribute
53*4882a593Smuzhiyun  */
54*4882a593Smuzhiyun #define __aligned(x)                    __attribute__((__aligned__(x)))
55*4882a593Smuzhiyun #define __aligned_largest               __attribute__((__aligned__))
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun  * Note: users of __always_inline currently do not write "inline" themselves,
59*4882a593Smuzhiyun  * which seems to be required by gcc to apply the attribute according
60*4882a593Smuzhiyun  * to its docs (and also "warning: always_inline function might not be
61*4882a593Smuzhiyun  * inlinable [-Wattributes]" is emitted).
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute
64*4882a593Smuzhiyun  * clang: mentioned
65*4882a593Smuzhiyun  */
66*4882a593Smuzhiyun #define __always_inline                 inline __attribute__((__always_inline__))
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun /*
69*4882a593Smuzhiyun  * The second argument is optional (default 0), so we use a variadic macro
70*4882a593Smuzhiyun  * to make the shorthand.
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * Beware: Do not apply this to functions which may return
73*4882a593Smuzhiyun  * ERR_PTRs. Also, it is probably unwise to apply it to functions
74*4882a593Smuzhiyun  * returning extra information in the low bits (but in that case the
75*4882a593Smuzhiyun  * compiler should see some alignment anyway, when the return value is
76*4882a593Smuzhiyun  * massaged by 'flags = ptr & 3; ptr &= ~3;').
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * Optional: only supported since gcc >= 4.9
79*4882a593Smuzhiyun  * Optional: not supported by icc
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-assume_005faligned-function-attribute
82*4882a593Smuzhiyun  * clang: https://clang.llvm.org/docs/AttributeReference.html#assume-aligned
83*4882a593Smuzhiyun  */
84*4882a593Smuzhiyun #if __has_attribute(__assume_aligned__)
85*4882a593Smuzhiyun # define __assume_aligned(a, ...)       __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
86*4882a593Smuzhiyun #else
87*4882a593Smuzhiyun # define __assume_aligned(a, ...)
88*4882a593Smuzhiyun #endif
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /*
91*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-cold-function-attribute
92*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
93*4882a593Smuzhiyun  */
94*4882a593Smuzhiyun #define __cold                          __attribute__((__cold__))
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun  * Note the long name.
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute
100*4882a593Smuzhiyun  */
101*4882a593Smuzhiyun #define __attribute_const__             __attribute__((__const__))
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun  * Optional: only supported since gcc >= 9
105*4882a593Smuzhiyun  * Optional: not supported by clang
106*4882a593Smuzhiyun  * Optional: not supported by icc
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-copy-function-attribute
109*4882a593Smuzhiyun  */
110*4882a593Smuzhiyun #if __has_attribute(__copy__)
111*4882a593Smuzhiyun # define __copy(symbol)                 __attribute__((__copy__(symbol)))
112*4882a593Smuzhiyun #else
113*4882a593Smuzhiyun # define __copy(symbol)
114*4882a593Smuzhiyun #endif
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun  * Don't. Just don't. See commit 771c035372a0 ("deprecate the '__deprecated'
118*4882a593Smuzhiyun  * attribute warnings entirely and for good") for more information.
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute
121*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-deprecated-type-attribute
122*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-deprecated-variable-attribute
123*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html#index-deprecated-enumerator-attribute
124*4882a593Smuzhiyun  * clang: https://clang.llvm.org/docs/AttributeReference.html#deprecated
125*4882a593Smuzhiyun  */
126*4882a593Smuzhiyun #define __deprecated
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun  * Optional: only supported since gcc >= 5.1
130*4882a593Smuzhiyun  * Optional: not supported by clang
131*4882a593Smuzhiyun  * Optional: not supported by icc
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-designated_005finit-type-attribute
134*4882a593Smuzhiyun  */
135*4882a593Smuzhiyun #if __has_attribute(__designated_init__)
136*4882a593Smuzhiyun # define __designated_init              __attribute__((__designated_init__))
137*4882a593Smuzhiyun #else
138*4882a593Smuzhiyun # define __designated_init
139*4882a593Smuzhiyun #endif
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun  * Optional: only supported since clang >= 14.0
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-error-function-attribute
145*4882a593Smuzhiyun  */
146*4882a593Smuzhiyun #if __has_attribute(__error__)
147*4882a593Smuzhiyun # define __compiletime_error(msg)       __attribute__((__error__(msg)))
148*4882a593Smuzhiyun #else
149*4882a593Smuzhiyun # define __compiletime_error(msg)
150*4882a593Smuzhiyun #endif
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun  * Optional: not supported by clang
154*4882a593Smuzhiyun  *
155*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-externally_005fvisible-function-attribute
156*4882a593Smuzhiyun  */
157*4882a593Smuzhiyun #if __has_attribute(__externally_visible__)
158*4882a593Smuzhiyun # define __visible                      __attribute__((__externally_visible__))
159*4882a593Smuzhiyun #else
160*4882a593Smuzhiyun # define __visible
161*4882a593Smuzhiyun #endif
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format-function-attribute
165*4882a593Smuzhiyun  * clang: https://clang.llvm.org/docs/AttributeReference.html#format
166*4882a593Smuzhiyun  */
167*4882a593Smuzhiyun #define __printf(a, b)                  __attribute__((__format__(printf, a, b)))
168*4882a593Smuzhiyun #define __scanf(a, b)                   __attribute__((__format__(scanf, a, b)))
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-gnu_005finline-function-attribute
172*4882a593Smuzhiyun  * clang: https://clang.llvm.org/docs/AttributeReference.html#gnu-inline
173*4882a593Smuzhiyun  */
174*4882a593Smuzhiyun #define __gnu_inline                    __attribute__((__gnu_inline__))
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute
178*4882a593Smuzhiyun  */
179*4882a593Smuzhiyun #define __malloc                        __attribute__((__malloc__))
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun /*
182*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-mode-type-attribute
183*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-mode-variable-attribute
184*4882a593Smuzhiyun  */
185*4882a593Smuzhiyun #define __mode(x)                       __attribute__((__mode__(x)))
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /*
188*4882a593Smuzhiyun  * Optional: only supported since gcc >= 7
189*4882a593Smuzhiyun  *
190*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html#index-no_005fcaller_005fsaved_005fregisters-function-attribute_002c-x86
191*4882a593Smuzhiyun  * clang: https://clang.llvm.org/docs/AttributeReference.html#no-caller-saved-registers
192*4882a593Smuzhiyun  */
193*4882a593Smuzhiyun #if __has_attribute(__no_caller_saved_registers__)
194*4882a593Smuzhiyun # define __no_caller_saved_registers	__attribute__((__no_caller_saved_registers__))
195*4882a593Smuzhiyun #else
196*4882a593Smuzhiyun # define __no_caller_saved_registers
197*4882a593Smuzhiyun #endif
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun /*
200*4882a593Smuzhiyun  * Optional: not supported by clang
201*4882a593Smuzhiyun  *
202*4882a593Smuzhiyun  *  gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noclone-function-attribute
203*4882a593Smuzhiyun  */
204*4882a593Smuzhiyun #if __has_attribute(__noclone__)
205*4882a593Smuzhiyun # define __noclone                      __attribute__((__noclone__))
206*4882a593Smuzhiyun #else
207*4882a593Smuzhiyun # define __noclone
208*4882a593Smuzhiyun #endif
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun /*
211*4882a593Smuzhiyun  * Add the pseudo keyword 'fallthrough' so case statement blocks
212*4882a593Smuzhiyun  * must end with any of these keywords:
213*4882a593Smuzhiyun  *   break;
214*4882a593Smuzhiyun  *   fallthrough;
215*4882a593Smuzhiyun  *   goto <label>;
216*4882a593Smuzhiyun  *   return [expression];
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  *  gcc: https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#Statement-Attributes
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun #if __has_attribute(__fallthrough__)
221*4882a593Smuzhiyun # define fallthrough                    __attribute__((__fallthrough__))
222*4882a593Smuzhiyun #else
223*4882a593Smuzhiyun # define fallthrough                    do {} while (0)  /* fallthrough */
224*4882a593Smuzhiyun #endif
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun  * Note the missing underscores.
228*4882a593Smuzhiyun  *
229*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute
230*4882a593Smuzhiyun  * clang: mentioned
231*4882a593Smuzhiyun  */
232*4882a593Smuzhiyun #define   noinline                      __attribute__((__noinline__))
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /*
235*4882a593Smuzhiyun  * Optional: only supported since gcc >= 8
236*4882a593Smuzhiyun  * Optional: not supported by clang
237*4882a593Smuzhiyun  * Optional: not supported by icc
238*4882a593Smuzhiyun  *
239*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute
240*4882a593Smuzhiyun  */
241*4882a593Smuzhiyun #if __has_attribute(__nonstring__)
242*4882a593Smuzhiyun # define __nonstring                    __attribute__((__nonstring__))
243*4882a593Smuzhiyun #else
244*4882a593Smuzhiyun # define __nonstring
245*4882a593Smuzhiyun #endif
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun /*
248*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute
249*4882a593Smuzhiyun  * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn
250*4882a593Smuzhiyun  * clang: https://clang.llvm.org/docs/AttributeReference.html#id1
251*4882a593Smuzhiyun  */
252*4882a593Smuzhiyun #define __noreturn                      __attribute__((__noreturn__))
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun /*
255*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute
256*4882a593Smuzhiyun  * clang: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute
257*4882a593Smuzhiyun  */
258*4882a593Smuzhiyun #define __packed                        __attribute__((__packed__))
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun /*
261*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute
262*4882a593Smuzhiyun  */
263*4882a593Smuzhiyun #define __pure                          __attribute__((__pure__))
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun /*
266*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-section-function-attribute
267*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-section-variable-attribute
268*4882a593Smuzhiyun  * clang: https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate
269*4882a593Smuzhiyun  */
270*4882a593Smuzhiyun #define __section(section)              __attribute__((__section__(section)))
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun /*
273*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute
274*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute
275*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-unused-variable-attribute
276*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-unused-label-attribute
277*4882a593Smuzhiyun  * clang: https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused
278*4882a593Smuzhiyun  */
279*4882a593Smuzhiyun #define __always_unused                 __attribute__((__unused__))
280*4882a593Smuzhiyun #define __maybe_unused                  __attribute__((__unused__))
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun /*
283*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-used-function-attribute
284*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-used-variable-attribute
285*4882a593Smuzhiyun  */
286*4882a593Smuzhiyun #define __used                          __attribute__((__used__))
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun /*
289*4882a593Smuzhiyun  * Optional: only supported since clang >= 14.0
290*4882a593Smuzhiyun  *
291*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warning-function-attribute
292*4882a593Smuzhiyun  */
293*4882a593Smuzhiyun #if __has_attribute(__warning__)
294*4882a593Smuzhiyun # define __compiletime_warning(msg)     __attribute__((__warning__(msg)))
295*4882a593Smuzhiyun #else
296*4882a593Smuzhiyun # define __compiletime_warning(msg)
297*4882a593Smuzhiyun #endif
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun /*
300*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
301*4882a593Smuzhiyun  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
302*4882a593Smuzhiyun  */
303*4882a593Smuzhiyun #define __weak                          __attribute__((__weak__))
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun #endif /* __LINUX_COMPILER_ATTRIBUTES_H */
306