compiler_attributes.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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) -- except those that are optional.
  7. *
  8. * Any other "attributes" (i.e. those that depend on a configuration option,
  9. * on a compiler, on an architecture, on plugins, on other attributes...)
  10. * should be defined elsewhere (e.g. compiler_types.h or compiler-*.h).
  11. *
  12. * This file is meant to be sorted (by actual attribute name,
  13. * not by #define identifier). Use the __attribute__((__name__)) syntax
  14. * (i.e. with underscores) to avoid future collisions with other macros.
  15. * If an attribute is optional, state the reason in the comment.
  16. */
  17. /*
  18. * To check for optional attributes, we use __has_attribute, which is supported
  19. * on gcc >= 5, clang >= 2.9 and icc >= 17. In the meantime, to support
  20. * 4.6 <= gcc < 5, we implement __has_attribute by hand.
  21. *
  22. * sparse does not support __has_attribute (yet) and defines __GNUC_MINOR__
  23. * depending on the compiler used to build it; however, these attributes have
  24. * no semantic effects for sparse, so it does not matter. Also note that,
  25. * in order to avoid sparse's warnings, even the unsupported ones must be
  26. * defined to 0.
  27. */
  28. #ifndef __has_attribute
  29. # define __has_attribute(x) __GCC4_has_attribute_##x
  30. # define __GCC4_has_attribute___assume_aligned__ (__GNUC_MINOR__ >= 9)
  31. # define __GCC4_has_attribute___designated_init__ 0
  32. # define __GCC4_has_attribute___externally_visible__ 1
  33. # define __GCC4_has_attribute___noclone__ 1
  34. # define __GCC4_has_attribute___optimize__ 1
  35. # define __GCC4_has_attribute___nonstring__ 0
  36. # define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
  37. #endif
  38. /*
  39. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute
  40. */
  41. #define __alias(symbol) __attribute__((__alias__(#symbol)))
  42. /*
  43. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-aligned-function-attribute
  44. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute
  45. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-aligned-variable-attribute
  46. */
  47. #define __aligned(x) __attribute__((__aligned__(x)))
  48. #define __aligned_largest __attribute__((__aligned__))
  49. /*
  50. * Note: users of __always_inline currently do not write "inline" themselves,
  51. * which seems to be required by gcc to apply the attribute according
  52. * to its docs (and also "warning: always_inline function might not be
  53. * inlinable [-Wattributes]" is emitted).
  54. *
  55. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute
  56. * clang: mentioned
  57. */
  58. #define __always_inline inline __attribute__((__always_inline__))
  59. /*
  60. * The second argument is optional (default 0), so we use a variadic macro
  61. * to make the shorthand.
  62. *
  63. * Beware: Do not apply this to functions which may return
  64. * ERR_PTRs. Also, it is probably unwise to apply it to functions
  65. * returning extra information in the low bits (but in that case the
  66. * compiler should see some alignment anyway, when the return value is
  67. * massaged by 'flags = ptr & 3; ptr &= ~3;').
  68. *
  69. * Optional: only supported since gcc >= 4.9
  70. * Optional: not supported by icc
  71. *
  72. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-assume_005faligned-function-attribute
  73. * clang: https://clang.llvm.org/docs/AttributeReference.html#assume-aligned
  74. */
  75. #if __has_attribute(__assume_aligned__)
  76. # define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
  77. #else
  78. # define __assume_aligned(a, ...)
  79. #endif
  80. /*
  81. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-cold-function-attribute
  82. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
  83. */
  84. #define __cold __attribute__((__cold__))
  85. /*
  86. * Note the long name.
  87. *
  88. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute
  89. */
  90. #define __attribute_const__ __attribute__((__const__))
  91. /*
  92. * Don't. Just don't. See commit 771c035372a0 ("deprecate the '__deprecated'
  93. * attribute warnings entirely and for good") for more information.
  94. *
  95. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute
  96. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-deprecated-type-attribute
  97. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-deprecated-variable-attribute
  98. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html#index-deprecated-enumerator-attribute
  99. * clang: https://clang.llvm.org/docs/AttributeReference.html#deprecated
  100. */
  101. #define __deprecated
  102. /*
  103. * Optional: only supported since gcc >= 5.1
  104. * Optional: not supported by clang
  105. * Optional: not supported by icc
  106. *
  107. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-designated_005finit-type-attribute
  108. */
  109. #if __has_attribute(__designated_init__)
  110. # define __designated_init __attribute__((__designated_init__))
  111. #else
  112. # define __designated_init
  113. #endif
  114. /*
  115. * Optional: not supported by clang
  116. *
  117. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-externally_005fvisible-function-attribute
  118. */
  119. #if __has_attribute(__externally_visible__)
  120. # define __visible __attribute__((__externally_visible__))
  121. #else
  122. # define __visible
  123. #endif
  124. /*
  125. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format-function-attribute
  126. * clang: https://clang.llvm.org/docs/AttributeReference.html#format
  127. */
  128. #define __printf(a, b) __attribute__((__format__(printf, a, b)))
  129. #define __scanf(a, b) __attribute__((__format__(scanf, a, b)))
  130. /*
  131. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-gnu_005finline-function-attribute
  132. * clang: https://clang.llvm.org/docs/AttributeReference.html#gnu-inline
  133. */
  134. #define __gnu_inline __attribute__((__gnu_inline__))
  135. /*
  136. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute
  137. */
  138. #define __malloc __attribute__((__malloc__))
  139. /*
  140. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-mode-type-attribute
  141. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-mode-variable-attribute
  142. */
  143. #define __mode(x) __attribute__((__mode__(x)))
  144. /*
  145. * Optional: not supported by clang
  146. * Note: icc does not recognize gcc's no-tracer
  147. *
  148. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noclone-function-attribute
  149. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-optimize-function-attribute
  150. */
  151. #if __has_attribute(__noclone__)
  152. # if __has_attribute(__optimize__)
  153. # define __noclone __attribute__((__noclone__, __optimize__("no-tracer")))
  154. # else
  155. # define __noclone __attribute__((__noclone__))
  156. # endif
  157. #else
  158. # define __noclone
  159. #endif
  160. /*
  161. * Note the missing underscores.
  162. *
  163. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute
  164. * clang: mentioned
  165. */
  166. #define noinline __attribute__((__noinline__))
  167. /*
  168. * Optional: only supported since gcc >= 8
  169. * Optional: not supported by clang
  170. * Optional: not supported by icc
  171. *
  172. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute
  173. */
  174. #if __has_attribute(__nonstring__)
  175. # define __nonstring __attribute__((__nonstring__))
  176. #else
  177. # define __nonstring
  178. #endif
  179. /*
  180. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute
  181. * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn
  182. * clang: https://clang.llvm.org/docs/AttributeReference.html#id1
  183. */
  184. #define __noreturn __attribute__((__noreturn__))
  185. /*
  186. * Optional: only supported since gcc >= 4.8
  187. * Optional: not supported by icc
  188. *
  189. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fsanitize_005faddress-function-attribute
  190. * clang: https://clang.llvm.org/docs/AttributeReference.html#no-sanitize-address-no-address-safety-analysis
  191. */
  192. #if __has_attribute(__no_sanitize_address__)
  193. # define __no_sanitize_address __attribute__((__no_sanitize_address__))
  194. #else
  195. # define __no_sanitize_address
  196. #endif
  197. /*
  198. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute
  199. * clang: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute
  200. */
  201. #define __packed __attribute__((__packed__))
  202. /*
  203. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute
  204. */
  205. #define __pure __attribute__((__pure__))
  206. /*
  207. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-section-function-attribute
  208. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-section-variable-attribute
  209. * clang: https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate
  210. */
  211. #define __section(S) __attribute__((__section__(#S)))
  212. /*
  213. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute
  214. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute
  215. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-unused-variable-attribute
  216. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-unused-label-attribute
  217. * clang: https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused
  218. */
  219. #define __always_unused __attribute__((__unused__))
  220. #define __maybe_unused __attribute__((__unused__))
  221. /*
  222. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-used-function-attribute
  223. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-used-variable-attribute
  224. */
  225. #define __used __attribute__((__used__))
  226. /*
  227. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
  228. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
  229. */
  230. #define __weak __attribute__((__weak__))
  231. #endif /* __LINUX_COMPILER_ATTRIBUTES_H */