compiler_attributes.h 15 KB

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