1 /* C++ compatible function declaration macros.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20 /* Begin/end the GNULIB_NAMESPACE namespace. */
21 #if defined __cplusplus && defined GNULIB_NAMESPACE
22 # define _GL_BEGIN_NAMESPACE namespace GNULIB_NAMESPACE {
23 # define _GL_END_NAMESPACE }
25 # define _GL_BEGIN_NAMESPACE
26 # define _GL_END_NAMESPACE
29 /* The three most frequent use cases of these macros are:
31 * For providing a substitute for a function that is missing on some
32 platforms, but is declared and works fine on the platforms on which
37 _GL_FUNCDECL_SYS (foo, ...);
39 _GL_CXXALIAS_SYS (foo, ...);
40 _GL_CXXALIASWARN (foo);
41 #elif defined GNULIB_POSIXCHECK
45 * For providing a replacement for a function that exists on all platforms,
46 but is broken/insufficient and needs to be replaced on some platforms:
50 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
54 _GL_FUNCDECL_RPL (foo, ...);
55 _GL_CXXALIAS_RPL (foo, ...);
57 _GL_CXXALIAS_SYS (foo, ...);
59 _GL_CXXALIASWARN (foo);
60 #elif defined GNULIB_POSIXCHECK
64 * For providing a replacement for a function that exists on some platforms
65 but is broken/insufficient and needs to be replaced on some of them and
66 is additionally either missing or undeclared on some other platforms:
70 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
74 _GL_FUNCDECL_RPL (foo, ...);
75 _GL_CXXALIAS_RPL (foo, ...);
77 # if !@HAVE_FOO@ or if !@HAVE_DECL_FOO@
78 _GL_FUNCDECL_SYS (foo, ...);
80 _GL_CXXALIAS_SYS (foo, ...);
82 _GL_CXXALIASWARN (foo);
83 #elif defined GNULIB_POSIXCHECK
88 /* _GL_EXTERN_C declaration;
89 performs the declaration with C linkage. */
90 #if defined __cplusplus
91 # define _GL_EXTERN_C extern "C"
93 # define _GL_EXTERN_C extern
96 /* _GL_EXTERN_C_FUNC declaration;
97 performs the declaration of a function with C linkage. */
98 #if defined __cplusplus
99 # define _GL_EXTERN_C_FUNC extern "C"
101 /* In C mode, omit the 'extern' keyword, because attributes in bracket syntax
102 are not allowed between 'extern' and the return type (see gnulib-common.m4).
104 # define _GL_EXTERN_C_FUNC
107 /* _GL_FUNCDECL_RPL (func, rettype, parameters[, attributes]);
108 declares a replacement function, named rpl_func, with the given prototype,
109 consisting of return type, parameters, and attributes.
111 _GL_FUNCDECL_RPL (open, int, (const char *filename, int flags, ...),
112 _GL_ARG_NONNULL ((1)));
114 Note: Attributes, such as _GL_ATTRIBUTE_DEPRECATED, are supported in front
115 of a _GL_FUNCDECL_RPL invocation only in C mode, not in C++ mode. (That's
117 [[...]] extern "C" <declaration>;
118 is invalid syntax in C++.)
120 Note: The attribute _GL_ATTRIBUTE_NOTHROW, if needed, must be placed outside
121 of the _GL_FUNCDECL_RPL invocation, at the end of the declaration.
123 #define _GL_FUNCDECL_RPL(func,rettype,parameters,...) \
124 _GL_FUNCDECL_RPL_1 (rpl_##func, rettype, parameters, __VA_ARGS__)
125 #define _GL_FUNCDECL_RPL_1(rpl_func,rettype,parameters,...) \
126 _GL_EXTERN_C_FUNC __VA_ARGS__ rettype rpl_func parameters
128 /* _GL_FUNCDECL_SYS (func, rettype, parameters[, attributes]);
129 declares the system function, named func, with the given prototype,
130 consisting of return type, parameters, and attributes.
132 _GL_FUNCDECL_SYS (open, int, (const char *filename, int flags, ...),
133 _GL_ARG_NONNULL ((1)));
135 Note: The attribute _GL_ATTRIBUTE_NOTHROW, if needed, must be placed outside
136 of the _GL_FUNCDECL_SYS invocation, at the end of the declaration.
138 #define _GL_FUNCDECL_SYS(func,rettype,parameters,...) \
139 _GL_EXTERN_C_FUNC __VA_ARGS__ rettype func parameters
141 /* _GL_CXXALIAS_RPL (func, rettype, parameters);
142 declares a C++ alias called GNULIB_NAMESPACE::func
143 that redirects to rpl_func, if GNULIB_NAMESPACE is defined.
145 _GL_CXXALIAS_RPL (open, int, (const char *filename, int flags, ...));
147 Wrapping rpl_func in an object with an inline conversion operator
148 avoids a reference to rpl_func unless GNULIB_NAMESPACE::func is
149 actually used in the program. */
150 #define _GL_CXXALIAS_RPL(func,rettype,parameters) \
151 _GL_CXXALIAS_RPL_1 (func, rpl_##func, rettype, parameters)
152 #if defined __cplusplus && defined GNULIB_NAMESPACE
153 # define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
154 namespace GNULIB_NAMESPACE \
156 static const struct _gl_ ## func ## _wrapper \
158 typedef rettype (*type) parameters; \
160 inline operator type () const \
166 _GL_EXTERN_C int _gl_cxxalias_dummy
168 # define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
169 _GL_EXTERN_C int _gl_cxxalias_dummy
172 /* _GL_CXXALIAS_MDA (func, rettype, parameters);
173 is to be used when func is a Microsoft deprecated alias, on native Windows.
174 It declares a C++ alias called GNULIB_NAMESPACE::func
175 that redirects to _func, if GNULIB_NAMESPACE is defined.
177 _GL_CXXALIAS_MDA (open, int, (const char *filename, int flags, ...));
179 #define _GL_CXXALIAS_MDA(func,rettype,parameters) \
180 _GL_CXXALIAS_RPL_1 (func, _##func, rettype, parameters)
182 /* _GL_CXXALIAS_RPL_CAST_1 (func, rpl_func, rettype, parameters);
183 is like _GL_CXXALIAS_RPL_1 (func, rpl_func, rettype, parameters);
184 except that the C function rpl_func may have a slightly different
185 declaration. A cast is used to silence the "invalid conversion" error
186 that would otherwise occur. */
187 #if defined __cplusplus && defined GNULIB_NAMESPACE
188 # define _GL_CXXALIAS_RPL_CAST_1(func,rpl_func,rettype,parameters) \
189 namespace GNULIB_NAMESPACE \
191 static const struct _gl_ ## func ## _wrapper \
193 typedef rettype (*type) parameters; \
195 inline operator type () const \
197 return reinterpret_cast<type>(::rpl_func); \
201 _GL_EXTERN_C int _gl_cxxalias_dummy
203 # define _GL_CXXALIAS_RPL_CAST_1(func,rpl_func,rettype,parameters) \
204 _GL_EXTERN_C int _gl_cxxalias_dummy
207 /* _GL_CXXALIAS_MDA_CAST (func, rettype, parameters);
208 is like _GL_CXXALIAS_MDA (func, rettype, parameters);
209 except that the C function func may have a slightly different declaration.
210 A cast is used to silence the "invalid conversion" error that would
212 #define _GL_CXXALIAS_MDA_CAST(func,rettype,parameters) \
213 _GL_CXXALIAS_RPL_CAST_1 (func, _##func, rettype, parameters)
215 /* _GL_CXXALIAS_SYS (func, rettype, parameters);
216 declares a C++ alias called GNULIB_NAMESPACE::func
217 that redirects to the system provided function func, if GNULIB_NAMESPACE
220 _GL_CXXALIAS_SYS (open, int, (const char *filename, int flags, ...));
222 Wrapping func in an object with an inline conversion operator
223 avoids a reference to func unless GNULIB_NAMESPACE::func is
224 actually used in the program. */
225 #if defined __cplusplus && defined GNULIB_NAMESPACE
226 # define _GL_CXXALIAS_SYS(func,rettype,parameters) \
227 namespace GNULIB_NAMESPACE \
229 static const struct _gl_ ## func ## _wrapper \
231 typedef rettype (*type) parameters; \
233 inline operator type () const \
239 _GL_EXTERN_C int _gl_cxxalias_dummy
241 # define _GL_CXXALIAS_SYS(func,rettype,parameters) \
242 _GL_EXTERN_C int _gl_cxxalias_dummy
245 /* _GL_CXXALIAS_SYS_CAST (func, rettype, parameters);
246 is like _GL_CXXALIAS_SYS (func, rettype, parameters);
247 except that the C function func may have a slightly different declaration.
248 A cast is used to silence the "invalid conversion" error that would
250 #if defined __cplusplus && defined GNULIB_NAMESPACE
251 # define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
252 namespace GNULIB_NAMESPACE \
254 static const struct _gl_ ## func ## _wrapper \
256 typedef rettype (*type) parameters; \
258 inline operator type () const \
260 return reinterpret_cast<type>(::func); \
264 _GL_EXTERN_C int _gl_cxxalias_dummy
266 # define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
267 _GL_EXTERN_C int _gl_cxxalias_dummy
270 /* _GL_CXXALIAS_SYS_CAST2 (func, rettype, parameters, rettype2, parameters2);
271 is like _GL_CXXALIAS_SYS (func, rettype, parameters);
272 except that the C function is picked among a set of overloaded functions,
273 namely the one with rettype2 and parameters2. Two consecutive casts
274 are used to silence the "cannot find a match" and "invalid conversion"
275 errors that would otherwise occur. */
276 #if defined __cplusplus && defined GNULIB_NAMESPACE
277 /* The outer cast must be a reinterpret_cast.
278 The inner cast: When the function is defined as a set of overloaded
279 functions, it works as a static_cast<>, choosing the designated variant.
280 When the function is defined as a single variant, it works as a
281 reinterpret_cast<>. The parenthesized cast syntax works both ways. */
282 # define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
283 namespace GNULIB_NAMESPACE \
285 static const struct _gl_ ## func ## _wrapper \
287 typedef rettype (*type) parameters; \
289 inline operator type () const \
291 return reinterpret_cast<type>((rettype2 (*) parameters2)(::func)); \
295 _GL_EXTERN_C int _gl_cxxalias_dummy
297 # define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
298 _GL_EXTERN_C int _gl_cxxalias_dummy
301 /* _GL_CXXALIASWARN (func);
302 causes a warning to be emitted when ::func is used but not when
303 GNULIB_NAMESPACE::func is used. func must be defined without overloaded
305 #if defined __cplusplus && defined GNULIB_NAMESPACE
306 # define _GL_CXXALIASWARN(func) \
307 _GL_CXXALIASWARN_1 (func, GNULIB_NAMESPACE)
308 # define _GL_CXXALIASWARN_1(func,namespace) \
309 _GL_CXXALIASWARN_2 (func, namespace)
310 /* To work around GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43881>,
311 we enable the warning only when not optimizing. */
312 # if !(defined __GNUC__ && !defined __clang__ && __OPTIMIZE__)
313 # define _GL_CXXALIASWARN_2(func,namespace) \
314 _GL_WARN_ON_USE (func, \
315 "The symbol ::" #func " refers to the system function. " \
316 "Use " #namespace "::" #func " instead.")
317 # elif (__GNUC__ >= 3 || defined __clang__) && GNULIB_STRICT_CHECKING
318 # define _GL_CXXALIASWARN_2(func,namespace) \
319 extern __typeof__ (func) func
321 # define _GL_CXXALIASWARN_2(func,namespace) \
322 _GL_EXTERN_C int _gl_cxxalias_dummy
325 # define _GL_CXXALIASWARN(func) \
326 _GL_EXTERN_C int _gl_cxxalias_dummy
329 /* _GL_CXXALIASWARN1 (func, rettype, parameters_and_attributes);
330 causes a warning to be emitted when the given overloaded variant of ::func
331 is used but not when GNULIB_NAMESPACE::func is used. */
332 #if defined __cplusplus && defined GNULIB_NAMESPACE
333 # define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
334 _GL_CXXALIASWARN1_1 (func, rettype, parameters_and_attributes, \
336 # define _GL_CXXALIASWARN1_1(func,rettype,parameters_and_attributes,namespace) \
337 _GL_CXXALIASWARN1_2 (func, rettype, parameters_and_attributes, namespace)
338 /* To work around GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43881>,
339 we enable the warning only when not optimizing. */
340 # if !(defined __GNUC__ && !defined __clang__ && __OPTIMIZE__)
341 # define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
342 _GL_WARN_ON_USE_CXX (func, rettype, rettype, parameters_and_attributes, \
343 "The symbol ::" #func " refers to the system function. " \
344 "Use " #namespace "::" #func " instead.")
346 # define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
347 _GL_EXTERN_C int _gl_cxxalias_dummy
350 # define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
351 _GL_EXTERN_C int _gl_cxxalias_dummy
354 #endif /* _GL_CXXDEFS_H */