Blame view

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