compiler_attributes.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
  36. #endif
  37. /*
  38. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute
  39. */
  40. #define __alias(symbol) __attribute__((__alias__(#symbol)))
  41. /*
  42. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-aligned-function-attribute
  43. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute
  44. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-aligned-variable-attribute
  45. */
  46. #define __aligned(x) __attribute__((__aligned__(x)))
  47. #define __aligned_largest __attribute__((__aligned__))
  48. /*
  49. * Note: users of __always_inline currently do not write "inline" themselves,
  50. * which seems to be required by gcc to apply the attribute according
  51. * to its docs (and also "warning: always_inline function might not be
  52. * inlinable [-Wattributes]" is emitted).
  53. *
  54. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute
  55. * clang: mentioned
  56. */
  57. #define __always_inline inline __attribute__((__always_inline__))
  58. /*
  59. * The second argument is optional (default 0), so we use a variadic macro
  60. * to make the shorthand.
  61. *
  62. * Beware: Do not apply this to functions which may return
  63. * ERR_PTRs. Also, it is probably unwise to apply it to functions
  64. * returning extra information in the low bits (but in that case the
  65. * compiler should see some alignment anyway, when the return value is
  66. * massaged by 'flags = ptr & 3; ptr &= ~3;').
  67. *
  68. * Optional: only supported since gcc >= 4.9
  69. * Optional: not supported by icc
  70. *
  71. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-assume_005faligned-function-attribute
  72. * clang: https://clang.llvm.org/docs/AttributeReference.html#assume-aligned
  73. */
  74. #if __has_attribute(__assume_aligned__)
  75. # define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
  76. #else
  77. # define __assume_aligned(a, ...)
  78. #endif
  79. /*
  80. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-cold-function-attribute
  81. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
  82. */
  83. #define __cold __attribute__((__cold__))
  84. /*
  85. * Note the long name.
  86. *
  87. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute
  88. */
  89. #define __attribute_const__ __attribute__((__const__))
  90. /*
  91. * Don't. Just don't. See commit 771c035372a0 ("deprecate the '__deprecated'
  92. * attribute warnings entirely and for good") for more information.
  93. *
  94. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute
  95. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-deprecated-type-attribute
  96. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-deprecated-variable-attribute
  97. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html#index-deprecated-enumerator-attribute
  98. * clang: https://clang.llvm.org/docs/AttributeReference.html#deprecated
  99. */
  100. #define __deprecated
  101. /*
  102. * Optional: only supported since gcc >= 5.1
  103. * Optional: not supported by clang
  104. * Optional: not supported by icc
  105. *
  106. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-designated_005finit-type-attribute
  107. */
  108. #if __has_attribute(__designated_init__)
  109. # define __designated_init __attribute__((__designated_init__))
  110. #else
  111. # define __designated_init
  112. #endif
  113. /*
  114. * Optional: not supported by clang
  115. *
  116. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-externally_005fvisible-function-attribute
  117. */
  118. #if __has_attribute(__externally_visible__)
  119. # define __visible __attribute__((__externally_visible__))
  120. #else
  121. # define __visible
  122. #endif
  123. /*
  124. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format-function-attribute
  125. * clang: https://clang.llvm.org/docs/AttributeReference.html#format
  126. */
  127. #define __printf(a, b) __attribute__((__format__(printf, a, b)))
  128. #define __scanf(a, b) __attribute__((__format__(scanf, a, b)))
  129. /*
  130. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-gnu_005finline-function-attribute
  131. * clang: https://clang.llvm.org/docs/AttributeReference.html#gnu-inline
  132. */
  133. #define __gnu_inline __attribute__((__gnu_inline__))
  134. /*
  135. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute
  136. */
  137. #define __malloc __attribute__((__malloc__))
  138. /*
  139. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-mode-type-attribute
  140. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-mode-variable-attribute
  141. */
  142. #define __mode(x) __attribute__((__mode__(x)))
  143. /*
  144. * Optional: not supported by clang
  145. * Note: icc does not recognize gcc's no-tracer
  146. *
  147. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noclone-function-attribute
  148. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-optimize-function-attribute
  149. */
  150. #if __has_attribute(__noclone__)
  151. # if __has_attribute(__optimize__)
  152. # define __noclone __attribute__((__noclone__, __optimize__("no-tracer")))
  153. # else
  154. # define __noclone __attribute__((__noclone__))
  155. # endif
  156. #else
  157. # define __noclone
  158. #endif
  159. /*
  160. * Note the missing underscores.
  161. *
  162. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute
  163. * clang: mentioned
  164. */
  165. #define noinline __attribute__((__noinline__))
  166. /*
  167. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute
  168. * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn
  169. * clang: https://clang.llvm.org/docs/AttributeReference.html#id1
  170. */
  171. #define __noreturn __attribute__((__noreturn__))
  172. /*
  173. * Optional: only supported since gcc >= 4.8
  174. * Optional: not supported by icc
  175. *
  176. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fsanitize_005faddress-function-attribute
  177. * clang: https://clang.llvm.org/docs/AttributeReference.html#no-sanitize-address-no-address-safety-analysis
  178. */
  179. #if __has_attribute(__no_sanitize_address__)
  180. # define __no_sanitize_address __attribute__((__no_sanitize_address__))
  181. #else
  182. # define __no_sanitize_address
  183. #endif
  184. /*
  185. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute
  186. * clang: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute
  187. */
  188. #define __packed __attribute__((__packed__))
  189. /*
  190. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute
  191. */
  192. #define __pure __attribute__((__pure__))
  193. /*
  194. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-section-function-attribute
  195. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-section-variable-attribute
  196. * clang: https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate
  197. */
  198. #define __section(S) __attribute__((__section__(#S)))
  199. /*
  200. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute
  201. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute
  202. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-unused-variable-attribute
  203. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-unused-label-attribute
  204. * clang: https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused
  205. */
  206. #define __always_unused __attribute__((__unused__))
  207. #define __maybe_unused __attribute__((__unused__))
  208. /*
  209. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-used-function-attribute
  210. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-used-variable-attribute
  211. */
  212. #define __used __attribute__((__used__))
  213. /*
  214. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
  215. * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
  216. */
  217. #define __weak __attribute__((__weak__))
  218. #endif /* __LINUX_COMPILER_ATTRIBUTES_H */