1 | /* A C macro for emitting warnings if a function is used.
|
---|
2 | Copyright (C) 2010-2015 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This program is free software: you can redistribute it and/or modify it
|
---|
5 | under the terms of the GNU General Public License as published
|
---|
6 | by the Free Software Foundation; either version 3 of the License, or
|
---|
7 | (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
|
---|
12 | General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
16 |
|
---|
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 |
|
---|
23 | This macro is useful for marking a function as a potential
|
---|
24 | portability trap, with the intent that "literal string" include
|
---|
25 | instructions on the replacement function that should be used
|
---|
26 | instead. However, one of the reasons that a function is a
|
---|
27 | portability trap is if it has the wrong signature. Declaring
|
---|
28 | FUNCTION with a different signature in C is a compilation error, so
|
---|
29 | this macro must use the same type as any existing declaration so
|
---|
30 | that programs that avoid the problematic FUNCTION do not fail to
|
---|
31 | compile merely because they included a header that poisoned the
|
---|
32 | function. But this implies that _GL_WARN_ON_USE is only safe to
|
---|
33 | use if FUNCTION is known to already have a declaration. Use of
|
---|
34 | this macro implies that there must not be any other macro hiding
|
---|
35 | the declaration of FUNCTION; but undefining FUNCTION first is part
|
---|
36 | of the poisoning process anyway (although for symbols that are
|
---|
37 | provided only via a macro, the result is a compilation error rather
|
---|
38 | than a warning containing "literal string"). Also note that in
|
---|
39 | C++, it is only safe to use if FUNCTION has no overloads.
|
---|
40 |
|
---|
41 | For an example, it is possible to poison 'getline' by:
|
---|
42 | - adding a call to gl_WARN_ON_USE_PREPARE([[#include <stdio.h>]],
|
---|
43 | [getline]) in configure.ac, which potentially defines
|
---|
44 | HAVE_RAW_DECL_GETLINE
|
---|
45 | - adding this code to a header that wraps the system <stdio.h>:
|
---|
46 | #undef getline
|
---|
47 | #if HAVE_RAW_DECL_GETLINE
|
---|
48 | _GL_WARN_ON_USE (getline, "getline is required by POSIX 2008, but"
|
---|
49 | "not universally present; use the gnulib module getline");
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | It is not possible to directly poison global variables. But it is
|
---|
53 | possible to write a wrapper accessor function, and poison that
|
---|
54 | (less common usage, like &environ, will cause a compilation error
|
---|
55 | rather than issue the nice warning, but the end result of informing
|
---|
56 | the developer about their portability problem is still achieved):
|
---|
57 | #if HAVE_RAW_DECL_ENVIRON
|
---|
58 | static char ***rpl_environ (void) { return &environ; }
|
---|
59 | _GL_WARN_ON_USE (rpl_environ, "environ is not always properly declared");
|
---|
60 | # undef environ
|
---|
61 | # define environ (*rpl_environ ())
|
---|
62 | #endif
|
---|
63 | */
|
---|
64 | #ifndef _GL_WARN_ON_USE
|
---|
65 |
|
---|
66 | # if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
|
---|
67 | /* A compiler attribute is available in gcc versions 4.3.0 and later. */
|
---|
68 | # define _GL_WARN_ON_USE(function, message) \
|
---|
69 | extern __typeof__ (function) function __attribute__ ((__warning__ (message)))
|
---|
70 | # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
|
---|
71 | /* Verify the existence of the function. */
|
---|
72 | # define _GL_WARN_ON_USE(function, message) \
|
---|
73 | extern __typeof__ (function) function
|
---|
74 | # else /* Unsupported. */
|
---|
75 | # define _GL_WARN_ON_USE(function, message) \
|
---|
76 | _GL_WARN_EXTERN_C int _gl_warn_on_use
|
---|
77 | # endif
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | /* _GL_WARN_ON_USE_CXX (function, rettype, parameters_and_attributes, "string")
|
---|
81 | is like _GL_WARN_ON_USE (function, "string"), except that the function is
|
---|
82 | declared with the given prototype, consisting of return type, parameters,
|
---|
83 | and attributes.
|
---|
84 | This variant is useful for overloaded functions in C++. _GL_WARN_ON_USE does
|
---|
85 | not work in this case. */
|
---|
86 | #ifndef _GL_WARN_ON_USE_CXX
|
---|
87 | # if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
|
---|
88 | # define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
|
---|
89 | extern rettype function parameters_and_attributes \
|
---|
90 | __attribute__ ((__warning__ (msg)))
|
---|
91 | # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
|
---|
92 | /* Verify the existence of the function. */
|
---|
93 | # define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
|
---|
94 | extern rettype function parameters_and_attributes
|
---|
95 | # else /* Unsupported. */
|
---|
96 | # define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
|
---|
97 | _GL_WARN_EXTERN_C int _gl_warn_on_use
|
---|
98 | # endif
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | /* _GL_WARN_EXTERN_C declaration;
|
---|
102 | performs the declaration with C linkage. */
|
---|
103 | #ifndef _GL_WARN_EXTERN_C
|
---|
104 | # if defined __cplusplus
|
---|
105 | # define _GL_WARN_EXTERN_C extern "C"
|
---|
106 | # else
|
---|
107 | # define _GL_WARN_EXTERN_C extern
|
---|
108 | # endif
|
---|
109 | #endif
|
---|