ax_gcc_func_attribute.m4 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # ===========================================================================
  2. # http://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. # deprecated
  35. # destructor
  36. # dllexport
  37. # dllimport
  38. # error
  39. # externally_visible
  40. # flatten
  41. # format
  42. # format_arg
  43. # gnu_inline
  44. # hot
  45. # ifunc
  46. # leaf
  47. # malloc
  48. # noclone
  49. # noinline
  50. # nonnull
  51. # noreturn
  52. # nothrow
  53. # optimize
  54. # pure
  55. # unused
  56. # used
  57. # visibility
  58. # warning
  59. # warn_unused_result
  60. # weak
  61. # weakref
  62. #
  63. # Unsuppored function attributes will be tested with a prototype returning
  64. # an int and not accepting any arguments and the result of the check might
  65. # be wrong or meaningless so use with care.
  66. #
  67. # LICENSE
  68. #
  69. # Copyright (c) 2013 Gabriele Svelto <gabriele.svelto@gmail.com>
  70. #
  71. # Copying and distribution of this file, with or without modification, are
  72. # permitted in any medium without royalty provided the copyright notice
  73. # and this notice are preserved. This file is offered as-is, without any
  74. # warranty.
  75. #serial 2
  76. AC_DEFUN([AX_GCC_FUNC_ATTRIBUTE], [
  77. AS_VAR_PUSHDEF([ac_var], [ax_cv_have_func_attribute_$1])
  78. AC_CACHE_CHECK([for __attribute__(($1))], [ac_var], [
  79. AC_LINK_IFELSE([AC_LANG_PROGRAM([
  80. m4_case([$1],
  81. [alias], [
  82. int foo( void ) { return 0; }
  83. int bar( void ) __attribute__(($1("foo")));
  84. ],
  85. [aligned], [
  86. int foo( void ) __attribute__(($1(32)));
  87. ],
  88. [alloc_size], [
  89. void *foo(int a) __attribute__(($1(1)));
  90. ],
  91. [always_inline], [
  92. inline __attribute__(($1)) int foo( void ) { return 0; }
  93. ],
  94. [artificial], [
  95. inline __attribute__(($1)) int foo( void ) { return 0; }
  96. ],
  97. [cold], [
  98. int foo( void ) __attribute__(($1));
  99. ],
  100. [const], [
  101. int foo( void ) __attribute__(($1));
  102. ],
  103. [constructor], [
  104. int foo( void ) __attribute__(($1));
  105. ],
  106. [deprecated], [
  107. int foo( void ) __attribute__(($1("")));
  108. ],
  109. [destructor], [
  110. int foo( void ) __attribute__(($1));
  111. ],
  112. [dllexport], [
  113. __attribute__(($1)) int foo( void ) { return 0; }
  114. ],
  115. [dllimport], [
  116. int foo( void ) __attribute__(($1));
  117. ],
  118. [error], [
  119. int foo( void ) __attribute__(($1("")));
  120. ],
  121. [externally_visible], [
  122. int foo( void ) __attribute__(($1));
  123. ],
  124. [flatten], [
  125. int foo( void ) __attribute__(($1));
  126. ],
  127. [format], [
  128. int foo(const char *p, ...) __attribute__(($1(printf, 1, 2)));
  129. ],
  130. [format_arg], [
  131. char *foo(const char *p) __attribute__(($1(1)));
  132. ],
  133. [gnu_inline], [
  134. inline __attribute__(($1)) int foo( void ) { return 0; }
  135. ],
  136. [hot], [
  137. int foo( void ) __attribute__(($1));
  138. ],
  139. [ifunc], [
  140. int my_foo( void ) { return 0; }
  141. static int (*resolve_foo(void))(void) { return my_foo; }
  142. int foo( void ) __attribute__(($1("resolve_foo")));
  143. ],
  144. [leaf], [
  145. __attribute__(($1)) int foo( void ) { return 0; }
  146. ],
  147. [malloc], [
  148. void *foo( void ) __attribute__(($1));
  149. ],
  150. [noclone], [
  151. int foo( void ) __attribute__(($1));
  152. ],
  153. [noinline], [
  154. __attribute__(($1)) int foo( void ) { return 0; }
  155. ],
  156. [nonnull], [
  157. int foo(char *p) __attribute__(($1(1)));
  158. ],
  159. [noreturn], [
  160. void foo( void ) __attribute__(($1));
  161. ],
  162. [nothrow], [
  163. int foo( void ) __attribute__(($1));
  164. ],
  165. [optimize], [
  166. __attribute__(($1(3))) int foo( void ) { return 0; }
  167. ],
  168. [pure], [
  169. int foo( void ) __attribute__(($1));
  170. ],
  171. [unused], [
  172. int foo( void ) __attribute__(($1));
  173. ],
  174. [used], [
  175. int foo( void ) __attribute__(($1));
  176. ],
  177. [visibility], [
  178. int foo_def( void ) __attribute__(($1("default")));
  179. int foo_hid( void ) __attribute__(($1("hidden")));
  180. int foo_int( void ) __attribute__(($1("internal")));
  181. int foo_pro( void ) __attribute__(($1("protected")));
  182. ],
  183. [warning], [
  184. int foo( void ) __attribute__(($1("")));
  185. ],
  186. [warn_unused_result], [
  187. int foo( void ) __attribute__(($1));
  188. ],
  189. [weak], [
  190. int foo( void ) __attribute__(($1));
  191. ],
  192. [weakref], [
  193. static int foo( void ) { return 0; }
  194. static int bar( void ) __attribute__(($1("foo")));
  195. ],
  196. [
  197. m4_warn([syntax], [Unsupported attribute $1, the test may fail])
  198. int foo( void ) __attribute__(($1));
  199. ]
  200. )], [])
  201. ],
  202. dnl GCC doesn't exit with an error if an unknown attribute is
  203. dnl provided but only outputs a warning, so accept the attribute
  204. dnl only if no warning were issued.
  205. [AS_IF([test -s conftest.err],
  206. [AS_VAR_SET([ac_var], [no])],
  207. [AS_VAR_SET([ac_var], [yes])])],
  208. [AS_VAR_SET([ac_var], [no])])
  209. ])
  210. AS_IF([test yes = AS_VAR_GET([ac_var])],
  211. [AC_DEFINE_UNQUOTED(AS_TR_CPP(HAVE_FUNC_ATTRIBUTE_$1), 1,
  212. [Define to 1 if the system has the `$1' function attribute])], [])
  213. AS_VAR_POPDEF([ac_var])
  214. ])