blob: 2e67ea2926d855792b38d8b4ebc4afeee8a6133b [file] [log] [blame]
Matt Turner976464c2014-09-21 18:14:01 -07001# ===========================================================================
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# packed
55# pure
Matt Turner377ab2f2016-05-13 13:17:02 -070056# returns_nonnull
Matt Turner976464c2014-09-21 18:14:01 -070057# unused
58# used
59# visibility
60# warning
61# warn_unused_result
62# weak
63# weakref
64#
65# Unsuppored function attributes will be tested with a prototype returning
66# an int and not accepting any arguments and the result of the check might
67# be wrong or meaningless so use with care.
68#
69# LICENSE
70#
71# Copyright (c) 2013 Gabriele Svelto <gabriele.svelto@gmail.com>
72#
73# Copying and distribution of this file, with or without modification, are
74# permitted in any medium without royalty provided the copyright notice
75# and this notice are preserved. This file is offered as-is, without any
76# warranty.
77
78#serial 2
79
Matt Turner377ab2f2016-05-13 13:17:02 -070080# mattst88:
81# Added support for returns_nonnull attribute
82
Matt Turner976464c2014-09-21 18:14:01 -070083AC_DEFUN([AX_GCC_FUNC_ATTRIBUTE], [
84 AS_VAR_PUSHDEF([ac_var], [ax_cv_have_func_attribute_$1])
85
86 AC_CACHE_CHECK([for __attribute__(($1))], [ac_var], [
87 AC_LINK_IFELSE([AC_LANG_PROGRAM([
88 m4_case([$1],
89 [alias], [
90 int foo( void ) { return 0; }
91 int bar( void ) __attribute__(($1("foo")));
92 ],
93 [aligned], [
94 int foo( void ) __attribute__(($1(32)));
95 ],
96 [alloc_size], [
97 void *foo(int a) __attribute__(($1(1)));
98 ],
99 [always_inline], [
100 inline __attribute__(($1)) int foo( void ) { return 0; }
101 ],
102 [artificial], [
103 inline __attribute__(($1)) int foo( void ) { return 0; }
104 ],
105 [cold], [
106 int foo( void ) __attribute__(($1));
107 ],
108 [const], [
109 int foo( void ) __attribute__(($1));
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 [flatten], [
133 int foo( void ) __attribute__(($1));
134 ],
135 [format], [
136 int foo(const char *p, ...) __attribute__(($1(printf, 1, 2)));
137 ],
138 [format_arg], [
139 char *foo(const char *p) __attribute__(($1(1)));
140 ],
141 [gnu_inline], [
142 inline __attribute__(($1)) int foo( void ) { return 0; }
143 ],
144 [hot], [
145 int foo( void ) __attribute__(($1));
146 ],
147 [ifunc], [
148 int my_foo( void ) { return 0; }
149 static int (*resolve_foo(void))(void) { return my_foo; }
150 int foo( void ) __attribute__(($1("resolve_foo")));
151 ],
152 [leaf], [
153 __attribute__(($1)) int foo( void ) { return 0; }
154 ],
155 [malloc], [
156 void *foo( void ) __attribute__(($1));
157 ],
158 [noclone], [
159 int foo( void ) __attribute__(($1));
160 ],
161 [noinline], [
162 __attribute__(($1)) int foo( void ) { return 0; }
163 ],
164 [nonnull], [
165 int foo(char *p) __attribute__(($1(1)));
166 ],
167 [noreturn], [
168 void foo( void ) __attribute__(($1));
169 ],
170 [nothrow], [
171 int foo( void ) __attribute__(($1));
172 ],
173 [optimize], [
174 __attribute__(($1(3))) int foo( void ) { return 0; }
175 ],
176 [packed], [
177 struct __attribute__(($1)) foo { int bar; };
178 ],
179 [pure], [
180 int foo( void ) __attribute__(($1));
181 ],
Matt Turner377ab2f2016-05-13 13:17:02 -0700182 [returns_nonnull], [
183 int *foo( void ) __attribute__(($1));
184 ],
Matt Turner976464c2014-09-21 18:14:01 -0700185 [unused], [
186 int foo( void ) __attribute__(($1));
187 ],
188 [used], [
189 int foo( void ) __attribute__(($1));
190 ],
191 [visibility], [
192 int foo_def( void ) __attribute__(($1("default")));
193 int foo_hid( void ) __attribute__(($1("hidden")));
194 int foo_int( void ) __attribute__(($1("internal")));
195 int foo_pro( void ) __attribute__(($1("protected")));
196 ],
197 [warning], [
198 int foo( void ) __attribute__(($1("")));
199 ],
200 [warn_unused_result], [
201 int foo( void ) __attribute__(($1));
202 ],
203 [weak], [
204 int foo( void ) __attribute__(($1));
205 ],
206 [weakref], [
207 static int foo( void ) { return 0; }
208 static int bar( void ) __attribute__(($1("foo")));
209 ],
210 [
211 m4_warn([syntax], [Unsupported attribute $1, the test may fail])
212 int foo( void ) __attribute__(($1));
213 ]
214 )], [])
215 ],
216 dnl GCC doesn't exit with an error if an unknown attribute is
217 dnl provided but only outputs a warning, so accept the attribute
218 dnl only if no warning were issued.
219 [AS_IF([test -s conftest.err],
220 [AS_VAR_SET([ac_var], [no])],
221 [AS_VAR_SET([ac_var], [yes])])],
222 [AS_VAR_SET([ac_var], [no])])
223 ])
224
225 AS_IF([test yes = AS_VAR_GET([ac_var])],
226 [AC_DEFINE_UNQUOTED(AS_TR_CPP(HAVE_FUNC_ATTRIBUTE_$1), 1,
227 [Define to 1 if the system has the `$1' function attribute])], [])
228
229 AS_VAR_POPDEF([ac_var])
230])