summaryrefslogtreecommitdiffstats
path: root/build-aux/warn-on-use.h
diff options
context:
space:
mode:
Diffstat (limited to 'build-aux/warn-on-use.h')
-rw-r--r--build-aux/warn-on-use.h102
1 files changed, 102 insertions, 0 deletions
diff --git a/build-aux/warn-on-use.h b/build-aux/warn-on-use.h
new file mode 100644
index 00000000..1cd5062d
--- /dev/null
+++ b/build-aux/warn-on-use.h
@@ -0,0 +1,102 @@
1/* A C macro for emitting warnings if a function is used.
2 Copyright (C) 2010 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 Lesser 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 inline 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) \
69extern __typeof__ (function) function __attribute__ ((__warning__ (message)))
70
71# else /* Unsupported. */
72# define _GL_WARN_ON_USE(function, message) \
73_GL_WARN_EXTERN_C int _gl_warn_on_use
74# endif
75#endif
76
77/* _GL_WARN_ON_USE_CXX (function, rettype, parameters_and_attributes, "string")
78 is like _GL_WARN_ON_USE (function, "string"), except that the function is
79 declared with the given prototype, consisting of return type, parameters,
80 and attributes.
81 This variant is useful for overloaded functions in C++. _GL_WARN_ON_USE does
82 not work in this case. */
83#ifndef _GL_WARN_ON_USE_CXX
84# if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
85# define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
86extern rettype function parameters_and_attributes \
87 __attribute__ ((__warning__ (msg)))
88# else /* Unsupported. */
89# define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
90_GL_WARN_EXTERN_C int _gl_warn_on_use
91# endif
92#endif
93
94/* _GL_WARN_EXTERN_C declaration;
95 performs the declaration with C linkage. */
96#ifndef _GL_WARN_EXTERN_C
97# if defined __cplusplus
98# define _GL_WARN_EXTERN_C extern "C"
99# else
100# define _GL_WARN_EXTERN_C extern
101# endif
102#endif