blob: 94f5b9201ede3147f0ddbdd067e06ac63adaf7d8 [file] [log] [blame]
Christian Egli10efef72017-05-12 17:31:49 +02001/* A C macro for emitting warnings if a function is used.
Christian Egli543dd012022-01-12 14:42:24 +01002 Copyright (C) 2010-2022 Free Software Foundation, Inc.
Christian Egli10efef72017-05-12 17:31:49 +02003
4 This program is free software: you can redistribute it and/or modify it
Christian Egli543dd012022-01-12 14:42:24 +01005 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
Christian Egli10efef72017-05-12 17:31:49 +02007 (at your option) any later version.
8
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
Christian Egli543dd012022-01-12 14:42:24 +010012 Lesser General Public License for more details.
Christian Egli10efef72017-05-12 17:31:49 +020013
Christian Egli543dd012022-01-12 14:42:24 +010014 You should have received a copy of the GNU Lesser General Public License
Christian Egli9c7b1e82018-05-03 15:17:04 +020015 along with this program. If not, see <https://www.gnu.org/licenses/>. */
Christian Egli10efef72017-05-12 17:31:49 +020016
17/* _GL_WARN_ON_USE (function, "literal string") issues a declaration
18 for FUNCTION which will then trigger a compiler warning containing
19 the text of "literal string" anywhere that function is called, if
20 supported by the compiler. If the compiler does not support this
21 feature, the macro expands to an unused extern declaration.
22
André-Abush Clausea82ea7c2019-07-08 14:09:05 +020023 _GL_WARN_ON_USE_ATTRIBUTE ("literal string") expands to the
24 attribute used in _GL_WARN_ON_USE. If the compiler does not support
25 this feature, it expands to empty.
26
27 These macros are useful for marking a function as a potential
Christian Egli10efef72017-05-12 17:31:49 +020028 portability trap, with the intent that "literal string" include
29 instructions on the replacement function that should be used
André-Abush Clausea82ea7c2019-07-08 14:09:05 +020030 instead.
31 _GL_WARN_ON_USE is for functions with 'extern' linkage.
32 _GL_WARN_ON_USE_ATTRIBUTE is for functions with 'static' or 'inline'
33 linkage.
34
35 However, one of the reasons that a function is a portability trap is
36 if it has the wrong signature. Declaring FUNCTION with a different
37 signature in C is a compilation error, so this macro must use the
38 same type as any existing declaration so that programs that avoid
39 the problematic FUNCTION do not fail to compile merely because they
40 included a header that poisoned the function. But this implies that
41 _GL_WARN_ON_USE is only safe to use if FUNCTION is known to already
42 have a declaration. Use of this macro implies that there must not
43 be any other macro hiding the declaration of FUNCTION; but
44 undefining FUNCTION first is part of the poisoning process anyway
45 (although for symbols that are provided only via a macro, the result
46 is a compilation error rather than a warning containing
47 "literal string"). Also note that in C++, it is only safe to use if
48 FUNCTION has no overloads.
Christian Egli10efef72017-05-12 17:31:49 +020049
50 For an example, it is possible to poison 'getline' by:
51 - adding a call to gl_WARN_ON_USE_PREPARE([[#include <stdio.h>]],
52 [getline]) in configure.ac, which potentially defines
53 HAVE_RAW_DECL_GETLINE
54 - adding this code to a header that wraps the system <stdio.h>:
55 #undef getline
56 #if HAVE_RAW_DECL_GETLINE
57 _GL_WARN_ON_USE (getline, "getline is required by POSIX 2008, but"
58 "not universally present; use the gnulib module getline");
59 #endif
60
61 It is not possible to directly poison global variables. But it is
62 possible to write a wrapper accessor function, and poison that
63 (less common usage, like &environ, will cause a compilation error
64 rather than issue the nice warning, but the end result of informing
65 the developer about their portability problem is still achieved):
André-Abush Clausea82ea7c2019-07-08 14:09:05 +020066 #if HAVE_RAW_DECL_ENVIRON
67 static char ***
68 rpl_environ (void) { return &environ; }
69 _GL_WARN_ON_USE (rpl_environ, "environ is not always properly declared");
70 # undef environ
71 # define environ (*rpl_environ ())
72 #endif
73 or better (avoiding contradictory use of 'static' and 'extern'):
74 #if HAVE_RAW_DECL_ENVIRON
75 static char ***
76 _GL_WARN_ON_USE_ATTRIBUTE ("environ is not always properly declared")
77 rpl_environ (void) { return &environ; }
78 # undef environ
79 # define environ (*rpl_environ ())
80 #endif
Christian Egli10efef72017-05-12 17:31:49 +020081 */
82#ifndef _GL_WARN_ON_USE
83
84# if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
85/* A compiler attribute is available in gcc versions 4.3.0 and later. */
86# define _GL_WARN_ON_USE(function, message) \
Christian Egli543dd012022-01-12 14:42:24 +010087_GL_WARN_EXTERN_C __typeof__ (function) function __attribute__ ((__warning__ (message)))
André-Abush Clausea82ea7c2019-07-08 14:09:05 +020088# define _GL_WARN_ON_USE_ATTRIBUTE(message) \
89 __attribute__ ((__warning__ (message)))
Christian Egli543dd012022-01-12 14:42:24 +010090# elif __clang_major__ >= 4
91/* Another compiler attribute is available in clang. */
92# define _GL_WARN_ON_USE(function, message) \
93_GL_WARN_EXTERN_C __typeof__ (function) function \
94 __attribute__ ((__diagnose_if__ (1, message, "warning")))
95# define _GL_WARN_ON_USE_ATTRIBUTE(message) \
96 __attribute__ ((__diagnose_if__ (1, message, "warning")))
Christian Egli10efef72017-05-12 17:31:49 +020097# elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
98/* Verify the existence of the function. */
99# define _GL_WARN_ON_USE(function, message) \
Christian Egli543dd012022-01-12 14:42:24 +0100100_GL_WARN_EXTERN_C __typeof__ (function) function
André-Abush Clausea82ea7c2019-07-08 14:09:05 +0200101# define _GL_WARN_ON_USE_ATTRIBUTE(message)
Christian Egli10efef72017-05-12 17:31:49 +0200102# else /* Unsupported. */
103# define _GL_WARN_ON_USE(function, message) \
104_GL_WARN_EXTERN_C int _gl_warn_on_use
André-Abush Clausea82ea7c2019-07-08 14:09:05 +0200105# define _GL_WARN_ON_USE_ATTRIBUTE(message)
Christian Egli10efef72017-05-12 17:31:49 +0200106# endif
107#endif
108
Christian Egli543dd012022-01-12 14:42:24 +0100109/* _GL_WARN_ON_USE_CXX (function, rettype_gcc, rettype_clang, parameters_and_attributes, "message")
110 is like _GL_WARN_ON_USE (function, "message"), except that in C++ mode the
111 function is declared with the given prototype, consisting of return type,
112 parameters, and attributes.
Christian Egli10efef72017-05-12 17:31:49 +0200113 This variant is useful for overloaded functions in C++. _GL_WARN_ON_USE does
114 not work in this case. */
115#ifndef _GL_WARN_ON_USE_CXX
Christian Egli543dd012022-01-12 14:42:24 +0100116# if !defined __cplusplus
117# define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
118 _GL_WARN_ON_USE (function, msg)
119# else
120# if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
121/* A compiler attribute is available in gcc versions 4.3.0 and later. */
122# define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
123extern rettype_gcc function parameters_and_attributes \
124 __attribute__ ((__warning__ (msg)))
125# elif __clang_major__ >= 4
126/* Another compiler attribute is available in clang. */
127# define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
128extern rettype_clang function parameters_and_attributes \
129 __attribute__ ((__diagnose_if__ (1, msg, "warning")))
130# elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
Christian Egli10efef72017-05-12 17:31:49 +0200131/* Verify the existence of the function. */
Christian Egli543dd012022-01-12 14:42:24 +0100132# define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
133extern rettype_gcc function parameters_and_attributes
134# else /* Unsupported. */
135# define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
Christian Egli10efef72017-05-12 17:31:49 +0200136_GL_WARN_EXTERN_C int _gl_warn_on_use
Christian Egli543dd012022-01-12 14:42:24 +0100137# endif
Christian Egli10efef72017-05-12 17:31:49 +0200138# endif
139#endif
140
141/* _GL_WARN_EXTERN_C declaration;
142 performs the declaration with C linkage. */
143#ifndef _GL_WARN_EXTERN_C
144# if defined __cplusplus
145# define _GL_WARN_EXTERN_C extern "C"
146# else
147# define _GL_WARN_EXTERN_C extern
148# endif
149#endif