compiler_attributes.h 15 KB

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