Import upstream version 1.28
[debian/tar] / gnu / verify.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Compile-time assert-like macros.
4
5    Copyright (C) 2005-2006, 2009-2014 Free Software Foundation, Inc.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* Written by Paul Eggert, Bruno Haible, and Jim Meyering.  */
21
22 #ifndef _GL_VERIFY_H
23 #define _GL_VERIFY_H
24
25
26 /* Define _GL_HAVE__STATIC_ASSERT to 1 if _Static_assert works as per C11.
27    This is supported by GCC 4.6.0 and later, in C mode, and its use
28    here generates easier-to-read diagnostics when verify (R) fails.
29
30    Define _GL_HAVE_STATIC_ASSERT to 1 if static_assert works as per C++11.
31    This will likely be supported by future GCC versions, in C++ mode.
32
33    Use this only with GCC.  If we were willing to slow 'configure'
34    down we could also use it with other compilers, but since this
35    affects only the quality of diagnostics, why bother?  */
36 #if (4 < __GNUC__ + (6 <= __GNUC_MINOR__) \
37      && (201112L <= __STDC_VERSION__  || !defined __STRICT_ANSI__) \
38      && !defined __cplusplus)
39 # define _GL_HAVE__STATIC_ASSERT 1
40 #endif
41 /* The condition (99 < __GNUC__) is temporary, until we know about the
42    first G++ release that supports static_assert.  */
43 #if (99 < __GNUC__) && defined __cplusplus
44 # define _GL_HAVE_STATIC_ASSERT 1
45 #endif
46
47 /* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other
48    system headers, defines a conflicting _Static_assert that is no
49    better than ours; override it.  */
50 #ifndef _GL_HAVE_STATIC_ASSERT
51 # include <stddef.h>
52 # undef _Static_assert
53 #endif
54
55 /* Each of these macros verifies that its argument R is nonzero.  To
56    be portable, R should be an integer constant expression.  Unlike
57    assert (R), there is no run-time overhead.
58
59    If _Static_assert works, verify (R) uses it directly.  Similarly,
60    _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
61    that is an operand of sizeof.
62
63    The code below uses several ideas for C++ compilers, and for C
64    compilers that do not support _Static_assert:
65
66    * The first step is ((R) ? 1 : -1).  Given an expression R, of
67      integral or boolean or floating-point type, this yields an
68      expression of integral type, whose value is later verified to be
69      constant and nonnegative.
70
71    * Next this expression W is wrapped in a type
72      struct _gl_verify_type {
73        unsigned int _gl_verify_error_if_negative: W;
74      }.
75      If W is negative, this yields a compile-time error.  No compiler can
76      deal with a bit-field of negative size.
77
78      One might think that an array size check would have the same
79      effect, that is, that the type struct { unsigned int dummy[W]; }
80      would work as well.  However, inside a function, some compilers
81      (such as C++ compilers and GNU C) allow local parameters and
82      variables inside array size expressions.  With these compilers,
83      an array size check would not properly diagnose this misuse of
84      the verify macro:
85
86        void function (int n) { verify (n < 0); }
87
88    * For the verify macro, the struct _gl_verify_type will need to
89      somehow be embedded into a declaration.  To be portable, this
90      declaration must declare an object, a constant, a function, or a
91      typedef name.  If the declared entity uses the type directly,
92      such as in
93
94        struct dummy {...};
95        typedef struct {...} dummy;
96        extern struct {...} *dummy;
97        extern void dummy (struct {...} *);
98        extern struct {...} *dummy (void);
99
100      two uses of the verify macro would yield colliding declarations
101      if the entity names are not disambiguated.  A workaround is to
102      attach the current line number to the entity name:
103
104        #define _GL_CONCAT0(x, y) x##y
105        #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
106        extern struct {...} * _GL_CONCAT (dummy, __LINE__);
107
108      But this has the problem that two invocations of verify from
109      within the same macro would collide, since the __LINE__ value
110      would be the same for both invocations.  (The GCC __COUNTER__
111      macro solves this problem, but is not portable.)
112
113      A solution is to use the sizeof operator.  It yields a number,
114      getting rid of the identity of the type.  Declarations like
115
116        extern int dummy [sizeof (struct {...})];
117        extern void dummy (int [sizeof (struct {...})]);
118        extern int (*dummy (void)) [sizeof (struct {...})];
119
120      can be repeated.
121
122    * Should the implementation use a named struct or an unnamed struct?
123      Which of the following alternatives can be used?
124
125        extern int dummy [sizeof (struct {...})];
126        extern int dummy [sizeof (struct _gl_verify_type {...})];
127        extern void dummy (int [sizeof (struct {...})]);
128        extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
129        extern int (*dummy (void)) [sizeof (struct {...})];
130        extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
131
132      In the second and sixth case, the struct type is exported to the
133      outer scope; two such declarations therefore collide.  GCC warns
134      about the first, third, and fourth cases.  So the only remaining
135      possibility is the fifth case:
136
137        extern int (*dummy (void)) [sizeof (struct {...})];
138
139    * GCC warns about duplicate declarations of the dummy function if
140      -Wredundant-decls is used.  GCC 4.3 and later have a builtin
141      __COUNTER__ macro that can let us generate unique identifiers for
142      each dummy function, to suppress this warning.
143
144    * This implementation exploits the fact that older versions of GCC,
145      which do not support _Static_assert, also do not warn about the
146      last declaration mentioned above.
147
148    * GCC warns if -Wnested-externs is enabled and verify() is used
149      within a function body; but inside a function, you can always
150      arrange to use verify_expr() instead.
151
152    * In C++, any struct definition inside sizeof is invalid.
153      Use a template type to work around the problem.  */
154
155 /* Concatenate two preprocessor tokens.  */
156 #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
157 #define _GL_CONCAT0(x, y) x##y
158
159 /* _GL_COUNTER is an integer, preferably one that changes each time we
160    use it.  Use __COUNTER__ if it works, falling back on __LINE__
161    otherwise.  __LINE__ isn't perfect, but it's better than a
162    constant.  */
163 #if defined __COUNTER__ && __COUNTER__ != __COUNTER__
164 # define _GL_COUNTER __COUNTER__
165 #else
166 # define _GL_COUNTER __LINE__
167 #endif
168
169 /* Generate a symbol with the given prefix, making it unique if
170    possible.  */
171 #define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
172
173 /* Verify requirement R at compile-time, as an integer constant expression
174    that returns 1.  If R is false, fail at compile-time, preferably
175    with a diagnostic that includes the string-literal DIAGNOSTIC.  */
176
177 #define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
178    (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
179
180 #ifdef __cplusplus
181 # if !GNULIB_defined_struct__gl_verify_type
182 template <int w>
183   struct _gl_verify_type {
184     unsigned int _gl_verify_error_if_negative: w;
185   };
186 #  define GNULIB_defined_struct__gl_verify_type 1
187 # endif
188 # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
189     _gl_verify_type<(R) ? 1 : -1>
190 #elif defined _GL_HAVE__STATIC_ASSERT
191 # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
192     struct {                                   \
193       _Static_assert (R, DIAGNOSTIC);          \
194       int _gl_dummy;                          \
195     }
196 #else
197 # define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
198     struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
199 #endif
200
201 /* Verify requirement R at compile-time, as a declaration without a
202    trailing ';'.  If R is false, fail at compile-time, preferably
203    with a diagnostic that includes the string-literal DIAGNOSTIC.
204
205    Unfortunately, unlike C11, this implementation must appear as an
206    ordinary declaration, and cannot appear inside struct { ... }.  */
207
208 #ifdef _GL_HAVE__STATIC_ASSERT
209 # define _GL_VERIFY _Static_assert
210 #else
211 # define _GL_VERIFY(R, DIAGNOSTIC)                                     \
212     extern int (*_GL_GENSYM (_gl_verify_function) (void))              \
213       [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
214 #endif
215
216 /* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h.  */
217 #ifdef _GL_STATIC_ASSERT_H
218 # if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert
219 #  define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC)
220 # endif
221 # if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert
222 #  define static_assert _Static_assert /* C11 requires this #define.  */
223 # endif
224 #endif
225
226 /* @assert.h omit start@  */
227
228 /* Each of these macros verifies that its argument R is nonzero.  To
229    be portable, R should be an integer constant expression.  Unlike
230    assert (R), there is no run-time overhead.
231
232    There are two macros, since no single macro can be used in all
233    contexts in C.  verify_true (R) is for scalar contexts, including
234    integer constant expression contexts.  verify (R) is for declaration
235    contexts, e.g., the top level.  */
236
237 /* Verify requirement R at compile-time, as an integer constant expression.
238    Return 1.  This is equivalent to verify_expr (R, 1).
239
240    verify_true is obsolescent; please use verify_expr instead.  */
241
242 #define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")")
243
244 /* Verify requirement R at compile-time.  Return the value of the
245    expression E.  */
246
247 #define verify_expr(R, E) \
248    (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
249
250 /* Verify requirement R at compile-time, as a declaration without a
251    trailing ';'.  */
252
253 #define verify(R) _GL_VERIFY (R, "verify (" #R ")")
254
255 #ifndef __has_builtin
256 # define __has_builtin(x) 0
257 #endif
258
259 /* Assume that R always holds.  This lets the compiler optimize
260    accordingly.  R should not have side-effects; it may or may not be
261    evaluated.  Behavior is undefined if R is false.  */
262
263 #if (__has_builtin (__builtin_unreachable) \
264      || 4 < __GNUC__ + (5 <= __GNUC_MINOR__))
265 # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
266 #elif 1200 <= _MSC_VER
267 # define assume(R) __assume (R)
268 #elif (defined lint \
269        && (__has_builtin (__builtin_trap) \
270            || 3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <= __GNUC_PATCHLEVEL__))))
271   /* Doing it this way helps various packages when configured with
272      --enable-gcc-warnings, which compiles with -Dlint.  It's nicer
273      when 'assume' silences warnings even with older GCCs.  */
274 # define assume(R) ((R) ? (void) 0 : __builtin_trap ())
275 #else
276 # define assume(R) ((void) (0 && (R)))
277 #endif
278
279 /* @assert.h omit end@  */
280
281 #endif