* admin.el (manual-pdf, manual-ps): Work in the directory with the texi file,
[emacs.git] / lib / verify.h
blob03492efcd3f4a5c2beaafb5ba8ea372f4ece957f
1 /* Compile-time assert-like macros.
3 Copyright (C) 2005-2006, 2009-2013 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
20 #ifndef _GL_VERIFY_H
21 # define _GL_VERIFY_H
24 /* Define _GL_HAVE__STATIC_ASSERT to 1 if _Static_assert works as per C11.
25 This is supported by GCC 4.6.0 and later, in C mode, and its use
26 here generates easier-to-read diagnostics when verify (R) fails.
28 Define _GL_HAVE_STATIC_ASSERT to 1 if static_assert works as per C++11.
29 This will likely be supported by future GCC versions, in C++ mode.
31 Use this only with GCC. If we were willing to slow 'configure'
32 down we could also use it with other compilers, but since this
33 affects only the quality of diagnostics, why bother? */
34 # if (4 < __GNUC__ + (6 <= __GNUC_MINOR__) \
35 && (201112L <= __STDC_VERSION__ || !defined __STRICT_ANSI__) \
36 && !defined __cplusplus)
37 # define _GL_HAVE__STATIC_ASSERT 1
38 # endif
39 /* The condition (99 < __GNUC__) is temporary, until we know about the
40 first G++ release that supports static_assert. */
41 # if (99 < __GNUC__) && defined __cplusplus
42 # define _GL_HAVE_STATIC_ASSERT 1
43 # endif
45 /* Each of these macros verifies that its argument R is nonzero. To
46 be portable, R should be an integer constant expression. Unlike
47 assert (R), there is no run-time overhead.
49 If _Static_assert works, verify (R) uses it directly. Similarly,
50 _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
51 that is an operand of sizeof.
53 The code below uses several ideas for C++ compilers, and for C
54 compilers that do not support _Static_assert:
56 * The first step is ((R) ? 1 : -1). Given an expression R, of
57 integral or boolean or floating-point type, this yields an
58 expression of integral type, whose value is later verified to be
59 constant and nonnegative.
61 * Next this expression W is wrapped in a type
62 struct _gl_verify_type {
63 unsigned int _gl_verify_error_if_negative: W;
65 If W is negative, this yields a compile-time error. No compiler can
66 deal with a bit-field of negative size.
68 One might think that an array size check would have the same
69 effect, that is, that the type struct { unsigned int dummy[W]; }
70 would work as well. However, inside a function, some compilers
71 (such as C++ compilers and GNU C) allow local parameters and
72 variables inside array size expressions. With these compilers,
73 an array size check would not properly diagnose this misuse of
74 the verify macro:
76 void function (int n) { verify (n < 0); }
78 * For the verify macro, the struct _gl_verify_type will need to
79 somehow be embedded into a declaration. To be portable, this
80 declaration must declare an object, a constant, a function, or a
81 typedef name. If the declared entity uses the type directly,
82 such as in
84 struct dummy {...};
85 typedef struct {...} dummy;
86 extern struct {...} *dummy;
87 extern void dummy (struct {...} *);
88 extern struct {...} *dummy (void);
90 two uses of the verify macro would yield colliding declarations
91 if the entity names are not disambiguated. A workaround is to
92 attach the current line number to the entity name:
94 #define _GL_CONCAT0(x, y) x##y
95 #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
96 extern struct {...} * _GL_CONCAT (dummy, __LINE__);
98 But this has the problem that two invocations of verify from
99 within the same macro would collide, since the __LINE__ value
100 would be the same for both invocations. (The GCC __COUNTER__
101 macro solves this problem, but is not portable.)
103 A solution is to use the sizeof operator. It yields a number,
104 getting rid of the identity of the type. Declarations like
106 extern int dummy [sizeof (struct {...})];
107 extern void dummy (int [sizeof (struct {...})]);
108 extern int (*dummy (void)) [sizeof (struct {...})];
110 can be repeated.
112 * Should the implementation use a named struct or an unnamed struct?
113 Which of the following alternatives can be used?
115 extern int dummy [sizeof (struct {...})];
116 extern int dummy [sizeof (struct _gl_verify_type {...})];
117 extern void dummy (int [sizeof (struct {...})]);
118 extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
119 extern int (*dummy (void)) [sizeof (struct {...})];
120 extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
122 In the second and sixth case, the struct type is exported to the
123 outer scope; two such declarations therefore collide. GCC warns
124 about the first, third, and fourth cases. So the only remaining
125 possibility is the fifth case:
127 extern int (*dummy (void)) [sizeof (struct {...})];
129 * GCC warns about duplicate declarations of the dummy function if
130 -Wredundant-decls is used. GCC 4.3 and later have a builtin
131 __COUNTER__ macro that can let us generate unique identifiers for
132 each dummy function, to suppress this warning.
134 * This implementation exploits the fact that older versions of GCC,
135 which do not support _Static_assert, also do not warn about the
136 last declaration mentioned above.
138 * GCC warns if -Wnested-externs is enabled and verify() is used
139 within a function body; but inside a function, you can always
140 arrange to use verify_expr() instead.
142 * In C++, any struct definition inside sizeof is invalid.
143 Use a template type to work around the problem. */
145 /* Concatenate two preprocessor tokens. */
146 # define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
147 # define _GL_CONCAT0(x, y) x##y
149 /* _GL_COUNTER is an integer, preferably one that changes each time we
150 use it. Use __COUNTER__ if it works, falling back on __LINE__
151 otherwise. __LINE__ isn't perfect, but it's better than a
152 constant. */
153 # if defined __COUNTER__ && __COUNTER__ != __COUNTER__
154 # define _GL_COUNTER __COUNTER__
155 # else
156 # define _GL_COUNTER __LINE__
157 # endif
159 /* Generate a symbol with the given prefix, making it unique if
160 possible. */
161 # define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
163 /* Verify requirement R at compile-time, as an integer constant expression
164 that returns 1. If R is false, fail at compile-time, preferably
165 with a diagnostic that includes the string-literal DIAGNOSTIC. */
167 # define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
168 (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
170 # ifdef __cplusplus
171 # if !GNULIB_defined_struct__gl_verify_type
172 template <int w>
173 struct _gl_verify_type {
174 unsigned int _gl_verify_error_if_negative: w;
176 # define GNULIB_defined_struct__gl_verify_type 1
177 # endif
178 # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
179 _gl_verify_type<(R) ? 1 : -1>
180 # elif defined _GL_HAVE__STATIC_ASSERT
181 # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
182 struct { \
183 _Static_assert (R, DIAGNOSTIC); \
184 int _gl_dummy; \
186 # else
187 # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
188 struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
189 # endif
191 /* Verify requirement R at compile-time, as a declaration without a
192 trailing ';'. If R is false, fail at compile-time, preferably
193 with a diagnostic that includes the string-literal DIAGNOSTIC.
195 Unfortunately, unlike C11, this implementation must appear as an
196 ordinary declaration, and cannot appear inside struct { ... }. */
198 # ifdef _GL_HAVE__STATIC_ASSERT
199 # define _GL_VERIFY _Static_assert
200 # else
201 # define _GL_VERIFY(R, DIAGNOSTIC) \
202 extern int (*_GL_GENSYM (_gl_verify_function) (void)) \
203 [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
204 # endif
206 /* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */
207 # ifdef _GL_STATIC_ASSERT_H
208 # if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert
209 # define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC)
210 # endif
211 # if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert
212 # define static_assert _Static_assert /* C11 requires this #define. */
213 # endif
214 # endif
216 /* @assert.h omit start@ */
218 /* Each of these macros verifies that its argument R is nonzero. To
219 be portable, R should be an integer constant expression. Unlike
220 assert (R), there is no run-time overhead.
222 There are two macros, since no single macro can be used in all
223 contexts in C. verify_true (R) is for scalar contexts, including
224 integer constant expression contexts. verify (R) is for declaration
225 contexts, e.g., the top level. */
227 /* Verify requirement R at compile-time, as an integer constant expression.
228 Return 1. This is equivalent to verify_expr (R, 1).
230 verify_true is obsolescent; please use verify_expr instead. */
232 # define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")")
234 /* Verify requirement R at compile-time. Return the value of the
235 expression E. */
237 # define verify_expr(R, E) \
238 (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
240 /* Verify requirement R at compile-time, as a declaration without a
241 trailing ';'. */
243 # define verify(R) _GL_VERIFY (R, "verify (" #R ")")
245 /* @assert.h omit end@ */
247 #endif