Enchant: libgnu/warn-on-use.h Source File

Enchant Generic spell checking library

warn-on-use.h
1 /* A C macro for emitting warnings if a function is used.
2 Copyright (C) 2010-2024 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 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.
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 Lesser General Public License
15 along with this program. If not, see <https://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 _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
28 portability trap, with the intent that "literal string" include
29 instructions on the replacement function that should be used
30 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 _GL_WARN_ON_USE should not be used more than once for a given function
36 in a given compilation unit (because this may generate a warning even
37 if the function is never called).
38
39 However, one of the reasons that a function is a portability trap is
40 if it has the wrong signature. Declaring FUNCTION with a different
41 signature in C is a compilation error, so this macro must use the
42 same type as any existing declaration so that programs that avoid
43 the problematic FUNCTION do not fail to compile merely because they
44 included a header that poisoned the function. But this implies that
45 _GL_WARN_ON_USE is only safe to use if FUNCTION is known to already
46 have a declaration. Use of this macro implies that there must not
47 be any other macro hiding the declaration of FUNCTION; but
48 undefining FUNCTION first is part of the poisoning process anyway
49 (although for symbols that are provided only via a macro, the result
50 is a compilation error rather than a warning containing
51 "literal string"). Also note that in C++, it is only safe to use if
52 FUNCTION has no overloads.
53
54 For an example, it is possible to poison 'getline' by:
55 - adding a call to gl_WARN_ON_USE_PREPARE([[#include <stdio.h>]],
56 [getline]) in configure.ac, which potentially defines
57 HAVE_RAW_DECL_GETLINE
58 - adding this code to a header that wraps the system <stdio.h>:
59 #undef getline
60 #if HAVE_RAW_DECL_GETLINE
61 _GL_WARN_ON_USE (getline, "getline is required by POSIX 2008, but"
62 "not universally present; use the gnulib module getline");
63 #endif
64
65 It is not possible to directly poison global variables. But it is
66 possible to write a wrapper accessor function, and poison that
67 (less common usage, like &environ, will cause a compilation error
68 rather than issue the nice warning, but the end result of informing
69 the developer about their portability problem is still achieved):
70 #if HAVE_RAW_DECL_ENVIRON
71 static char ***
72 rpl_environ (void) { return &environ; }
73 _GL_WARN_ON_USE (rpl_environ, "environ is not always properly declared");
74 # undef environ
75 # define environ (*rpl_environ ())
76 #endif
77 or better (avoiding contradictory use of 'static' and 'extern'):
78 #if HAVE_RAW_DECL_ENVIRON
79 static char ***
80 _GL_WARN_ON_USE_ATTRIBUTE ("environ is not always properly declared")
81 rpl_environ (void) { return &environ; }
82 # undef environ
83 # define environ (*rpl_environ ())
84 #endif
85 */
86 #ifndef _GL_WARN_ON_USE
87
88 # if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
89 /* A compiler attribute is available in gcc versions 4.3.0 and later. */
90 # define _GL_WARN_ON_USE(function, message) \
91 _GL_WARN_EXTERN_C __typeof__ (function) function __attribute__ ((__warning__ (message)))
92 # define _GL_WARN_ON_USE_ATTRIBUTE(message) \
93 __attribute__ ((__warning__ (message)))
94 # elif __clang_major__ >= 4
95 /* Another compiler attribute is available in clang. */
96 # define _GL_WARN_ON_USE(function, message) \
97 _GL_WARN_EXTERN_C __typeof__ (function) function \
98 __attribute__ ((__diagnose_if__ (1, message, "warning")))
99 # define _GL_WARN_ON_USE_ATTRIBUTE(message) \
100 __attribute__ ((__diagnose_if__ (1, message, "warning")))
101 # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
102 /* Verify the existence of the function. */
103 # define _GL_WARN_ON_USE(function, message) \
104 _GL_WARN_EXTERN_C __typeof__ (function) function
105 # define _GL_WARN_ON_USE_ATTRIBUTE(message)
106 # else /* Unsupported. */
107 # define _GL_WARN_ON_USE(function, message) \
108 _GL_WARN_EXTERN_C int _gl_warn_on_use
109 # define _GL_WARN_ON_USE_ATTRIBUTE(message)
110 # endif
111 #endif
112
113 /* _GL_WARN_ON_USE_CXX (function, rettype_gcc, rettype_clang, parameters_and_attributes, "message")
114 is like _GL_WARN_ON_USE (function, "message"), except that in C++ mode the
115 function is declared with the given prototype, consisting of return type,
116 parameters, and attributes.
117 This variant is useful for overloaded functions in C++. _GL_WARN_ON_USE does
118 not work in this case. */
119 #ifndef _GL_WARN_ON_USE_CXX
120 # if !defined __cplusplus
121 # define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
122 _GL_WARN_ON_USE (function, msg)
123 # else
124 # if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
125 /* A compiler attribute is available in gcc versions 4.3.0 and later. */
126 # define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
127 extern rettype_gcc function parameters_and_attributes \
128 __attribute__ ((__warning__ (msg)))
129 # elif __clang_major__ >= 4
130 /* Another compiler attribute is available in clang. */
131 # define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
132 extern rettype_clang function parameters_and_attributes \
133 __attribute__ ((__diagnose_if__ (1, msg, "warning")))
134 # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
135 /* Verify the existence of the function. */
136 # define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
137 extern rettype_gcc function parameters_and_attributes
138 # else /* Unsupported. */
139 # define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
140 _GL_WARN_EXTERN_C int _gl_warn_on_use
141 # endif
142 # endif
143 #endif
144
145 /* _GL_WARN_EXTERN_C declaration;
146 performs the declaration with C linkage. */
147 #ifndef _GL_WARN_EXTERN_C
148 # if defined __cplusplus
149 # define _GL_WARN_EXTERN_C extern "C"
150 # else
151 # define _GL_WARN_EXTERN_C extern
152 # endif
153 #endif