ax_gcc_func_attribute.m4 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # ===========================================================================
  2. # https://www.gnu.org/software/autoconf-archive/ax_gcc_func_attribute.html
  3. # ===========================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AX_GCC_FUNC_ATTRIBUTE(ATTRIBUTE)
  8. #
  9. # DESCRIPTION
  10. #
  11. # This macro checks if the compiler supports one of GCC's function
  12. # attributes; many other compilers also provide function attributes with
  13. # the same syntax. Compiler warnings are used to detect supported
  14. # attributes as unsupported ones are ignored by default so quieting
  15. # warnings when using this macro will yield false positives.
  16. #
  17. # The ATTRIBUTE parameter holds the name of the attribute to be checked.
  18. #
  19. # If ATTRIBUTE is supported define HAVE_FUNC_ATTRIBUTE_<ATTRIBUTE>.
  20. #
  21. # The macro caches its result in the ax_cv_have_func_attribute_<attribute>
  22. # variable.
  23. #
  24. # The macro currently supports the following function attributes:
  25. #
  26. # alias
  27. # aligned
  28. # alloc_size
  29. # always_inline
  30. # artificial
  31. # cold
  32. # const
  33. # constructor
  34. # constructor_priority for constructor attribute with priority
  35. # deprecated
  36. # destructor
  37. # dllexport
  38. # dllimport
  39. # error
  40. # externally_visible
  41. # fallthrough
  42. # flatten
  43. # format
  44. # format_arg
  45. # gnu_format
  46. # gnu_inline
  47. # hot
  48. # ifunc
  49. # leaf
  50. # malloc
  51. # noclone
  52. # noinline
  53. # nonnull
  54. # noreturn
  55. # nothrow
  56. # optimize
  57. # pure
  58. # sentinel
  59. # sentinel_position
  60. # unused
  61. # used
  62. # visibility
  63. # warning
  64. # warn_unused_result
  65. # weak
  66. # weakref
  67. #
  68. # Unsupported function attributes will be tested with a prototype
  69. # returning an int and not accepting any arguments and the result of the
  70. # check might be wrong or meaningless so use with care.
  71. #
  72. # LICENSE
  73. #
  74. # Copyright (c) 2013 Gabriele Svelto <gabriele.svelto@gmail.com>
  75. #
  76. # Copying and distribution of this file, with or without modification, are
  77. # permitted in any medium without royalty provided the copyright notice
  78. # and this notice are preserved. This file is offered as-is, without any
  79. # warranty.
  80. #serial 13
  81. AC_DEFUN([AX_GCC_FUNC_ATTRIBUTE], [
  82. AS_VAR_PUSHDEF([ac_var], [ax_cv_have_func_attribute_$1])
  83. AC_CACHE_CHECK([for __attribute__(($1))], [ac_var], [
  84. AC_LINK_IFELSE([AC_LANG_PROGRAM([
  85. m4_case([$1],
  86. [alias], [
  87. int foo( void ) { return 0; }
  88. int bar( void ) __attribute__(($1("foo")));
  89. ],
  90. [aligned], [
  91. int foo( void ) __attribute__(($1(32)));
  92. ],
  93. [alloc_size], [
  94. void *foo(int a) __attribute__(($1(1)));
  95. ],
  96. [always_inline], [
  97. inline __attribute__(($1)) int foo( void ) { return 0; }
  98. ],
  99. [artificial], [
  100. inline __attribute__(($1)) int foo( void ) { return 0; }
  101. ],
  102. [cold], [
  103. int foo( void ) __attribute__(($1));
  104. ],
  105. [const], [
  106. int foo( void ) __attribute__(($1));
  107. ],
  108. [constructor_priority], [
  109. int foo( void ) __attribute__((__constructor__(65535/2)));
  110. ],
  111. [constructor], [
  112. int foo( void ) __attribute__(($1));
  113. ],
  114. [deprecated], [
  115. int foo( void ) __attribute__(($1("")));
  116. ],
  117. [destructor], [
  118. int foo( void ) __attribute__(($1));
  119. ],
  120. [dllexport], [
  121. __attribute__(($1)) int foo( void ) { return 0; }
  122. ],
  123. [dllimport], [
  124. int foo( void ) __attribute__(($1));
  125. ],
  126. [error], [
  127. int foo( void ) __attribute__(($1("")));
  128. ],
  129. [externally_visible], [
  130. int foo( void ) __attribute__(($1));
  131. ],
  132. [fallthrough], [
  133. void foo( int x ) {switch (x) { case 1: __attribute__(($1)); case 2: break ; }};
  134. ],
  135. [flatten], [
  136. int foo( void ) __attribute__(($1));
  137. ],
  138. [format], [
  139. int foo(const char *p, ...) __attribute__(($1(printf, 1, 2)));
  140. ],
  141. [gnu_format], [
  142. int foo(const char *p, ...) __attribute__((format(gnu_printf, 1, 2)));
  143. ],
  144. [format_arg], [
  145. char *foo(const char *p) __attribute__(($1(1)));
  146. ],
  147. [gnu_inline], [
  148. inline __attribute__(($1)) int foo( void ) { return 0; }
  149. ],
  150. [hot], [
  151. int foo( void ) __attribute__(($1));
  152. ],
  153. [ifunc], [
  154. int my_foo( void ) { return 0; }
  155. static int (*resolve_foo(void))(void) { return my_foo; }
  156. int foo( void ) __attribute__(($1("resolve_foo")));
  157. ],
  158. [leaf], [
  159. __attribute__(($1)) int foo( void ) { return 0; }
  160. ],
  161. [malloc], [
  162. void *foo( void ) __attribute__(($1));
  163. ],
  164. [noclone], [
  165. int foo( void ) __attribute__(($1));
  166. ],
  167. [noinline], [
  168. __attribute__(($1)) int foo( void ) { return 0; }
  169. ],
  170. [nonnull], [
  171. int foo(char *p) __attribute__(($1(1)));
  172. ],
  173. [noreturn], [
  174. void foo( void ) __attribute__(($1));
  175. ],
  176. [nothrow], [
  177. int foo( void ) __attribute__(($1));
  178. ],
  179. [optimize], [
  180. __attribute__(($1(3))) int foo( void ) { return 0; }
  181. ],
  182. [pure], [
  183. int foo( void ) __attribute__(($1));
  184. ],
  185. [sentinel], [
  186. int foo(void *p, ...) __attribute__(($1));
  187. ],
  188. [sentinel_position], [
  189. int foo(void *p, ...) __attribute__(($1(1)));
  190. ],
  191. [returns_nonnull], [
  192. void *foo( void ) __attribute__(($1));
  193. ],
  194. [unused], [
  195. int foo( void ) __attribute__(($1));
  196. ],
  197. [used], [
  198. int foo( void ) __attribute__(($1));
  199. ],
  200. [visibility], [
  201. int foo_def( void ) __attribute__(($1("default")));
  202. int foo_hid( void ) __attribute__(($1("hidden")));
  203. int foo_int( void ) __attribute__(($1("internal")));
  204. int foo_pro( void ) __attribute__(($1("protected")));
  205. ],
  206. [warning], [
  207. int foo( void ) __attribute__(($1("")));
  208. ],
  209. [warn_unused_result], [
  210. int foo( void ) __attribute__(($1));
  211. ],
  212. [weak], [
  213. int foo( void ) __attribute__(($1));
  214. ],
  215. [weakref], [
  216. static int foo( void ) { return 0; }
  217. static int bar( void ) __attribute__(($1("foo")));
  218. ],
  219. [
  220. m4_warn([syntax], [Unsupported attribute $1, the test may fail])
  221. int foo( void ) __attribute__(($1));
  222. ]
  223. )], [])
  224. ],
  225. dnl GCC doesn't exit with an error if an unknown attribute is
  226. dnl provided but only outputs a warning, so accept the attribute
  227. dnl only if no warning were issued.
  228. [AS_IF([grep -- -Wattributes conftest.err],
  229. [AS_VAR_SET([ac_var], [no])],
  230. [AS_VAR_SET([ac_var], [yes])])],
  231. [AS_VAR_SET([ac_var], [no])])
  232. ])
  233. AS_IF([test yes = AS_VAR_GET([ac_var])],
  234. [AC_DEFINE_UNQUOTED(AS_TR_CPP(HAVE_FUNC_ATTRIBUTE_$1), 1,
  235. [Define to 1 if the system has the `$1' function attribute])], [])
  236. AS_VAR_POPDEF([ac_var])
  237. ])