36bc87848dad14171c46c0cb6aced5890f6bd14d
[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-2013 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 /* Each of these macros verifies that its argument R is nonzero.  To
48    be portable, R should be an integer constant expression.  Unlike
49    assert (R), there is no run-time overhead.
50
51    If _Static_assert works, verify (R) uses it directly.  Similarly,
52    _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
53    that is an operand of sizeof.
54
55    The code below uses several ideas for C++ compilers, and for C
56    compilers that do not support _Static_assert:
57
58    * The first step is ((R) ? 1 : -1).  Given an expression R, of
59      integral or boolean or floating-point type, this yields an
60      expression of integral type, whose value is later verified to be
61      constant and nonnegative.
62
63    * Next this expression W is wrapped in a type
64      struct _gl_verify_type {
65        unsigned int _gl_verify_error_if_negative: W;
66      }.
67      If W is negative, this yields a compile-time error.  No compiler can
68      deal with a bit-field of negative size.
69
70      One might think that an array size check would have the same
71      effect, that is, that the type struct { unsigned int dummy[W]; }
72      would work as well.  However, inside a function, some compilers
73      (such as C++ compilers and GNU C) allow local parameters and
74      variables inside array size expressions.  With these compilers,
75      an array size check would not properly diagnose this misuse of
76      the verify macro:
77
78        void function (int n) { verify (n < 0); }
79
80    * For the verify macro, the struct _gl_verify_type will need to
81      somehow be embedded into a declaration.  To be portable, this
82      declaration must declare an object, a constant, a function, or a
83      typedef name.  If the declared entity uses the type directly,
84      such as in
85
86        struct dummy {...};
87        typedef struct {...} dummy;
88        extern struct {...} *dummy;
89        extern void dummy (struct {...} *);
90        extern struct {...} *dummy (void);
91
92      two uses of the verify macro would yield colliding declarations
93      if the entity names are not disambiguated.  A workaround is to
94      attach the current line number to the entity name:
95
96        #define _GL_CONCAT0(x, y) x##y
97        #define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
98        extern struct {...} * _GL_CONCAT (dummy, __LINE__);
99
100      But this has the problem that two invocations of verify from
101      within the same macro would collide, since the __LINE__ value
102      would be the same for both invocations.  (The GCC __COUNTER__
103      macro solves this problem, but is not portable.)
104
105      A solution is to use the sizeof operator.  It yields a number,
106      getting rid of the identity of the type.  Declarations like
107
108        extern int dummy [sizeof (struct {...})];
109        extern void dummy (int [sizeof (struct {...})]);
110        extern int (*dummy (void)) [sizeof (struct {...})];
111
112      can be repeated.
113
114    * Should the implementation use a named struct or an unnamed struct?
115      Which of the following alternatives can be used?
116
117        extern int dummy [sizeof (struct {...})];
118        extern int dummy [sizeof (struct _gl_verify_type {...})];
119        extern void dummy (int [sizeof (struct {...})]);
120        extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
121        extern int (*dummy (void)) [sizeof (struct {...})];
122        extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
123
124      In the second and sixth case, the struct type is exported to the
125      outer scope; two such declarations therefore collide.  GCC warns
126      about the first, third, and fourth cases.  So the only remaining
127      possibility is the fifth case:
128
129        extern int (*dummy (void)) [sizeof (struct {...})];
130
131    * GCC warns about duplicate declarations of the dummy function if
132      -Wredundant-decls is used.  GCC 4.3 and later have a builtin
133      __COUNTER__ macro that can let us generate unique identifiers for
134      each dummy function, to suppress this warning.
135
136    * This implementation exploits the fact that older versions of GCC,
137      which do not support _Static_assert, also do not warn about the
138      last declaration mentioned above.
139
140    * GCC warns if -Wnested-externs is enabled and verify() is used
141      within a function body; but inside a function, you can always
142      arrange to use verify_expr() instead.
143
144    * In C++, any struct definition inside sizeof is invalid.
145      Use a template type to work around the problem.  */
146
147 /* Concatenate two preprocessor tokens.  */
148 # define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
149 # define _GL_CONCAT0(x, y) x##y
150
151 /* _GL_COUNTER is an integer, preferably one that changes each time we
152    use it.  Use __COUNTER__ if it works, falling back on __LINE__
153    otherwise.  __LINE__ isn't perfect, but it's better than a
154    constant.  */
155 # if defined __COUNTER__ && __COUNTER__ != __COUNTER__
156 #  define _GL_COUNTER __COUNTER__
157 # else
158 #  define _GL_COUNTER __LINE__
159 # endif
160
161 /* Generate a symbol with the given prefix, making it unique if
162    possible.  */
163 # define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
164
165 /* Verify requirement R at compile-time, as an integer constant expression
166    that returns 1.  If R is false, fail at compile-time, preferably
167    with a diagnostic that includes the string-literal DIAGNOSTIC.  */
168
169 # define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
170     (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
171
172 # ifdef __cplusplus
173 #  if !GNULIB_defined_struct__gl_verify_type
174 template <int w>
175   struct _gl_verify_type {
176     unsigned int _gl_verify_error_if_negative: w;
177   };
178 #   define GNULIB_defined_struct__gl_verify_type 1
179 #  endif
180 #  define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
181     _gl_verify_type<(R) ? 1 : -1>
182 # elif defined _GL_HAVE__STATIC_ASSERT
183 #  define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
184      struct {                                   \
185        _Static_assert (R, DIAGNOSTIC);          \
186        int _gl_dummy;                          \
187      }
188 # else
189 #  define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
190      struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
191 # endif
192
193 /* Verify requirement R at compile-time, as a declaration without a
194    trailing ';'.  If R is false, fail at compile-time, preferably
195    with a diagnostic that includes the string-literal DIAGNOSTIC.
196
197    Unfortunately, unlike C11, this implementation must appear as an
198    ordinary declaration, and cannot appear inside struct { ... }.  */
199
200 # ifdef _GL_HAVE__STATIC_ASSERT
201 #  define _GL_VERIFY _Static_assert
202 # else
203 #  define _GL_VERIFY(R, DIAGNOSTIC)                                    \
204      extern int (*_GL_GENSYM (_gl_verify_function) (void))             \
205        [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
206 # endif
207
208 /* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h.  */
209 # ifdef _GL_STATIC_ASSERT_H
210 #  if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert
211 #   define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC)
212 #  endif
213 #  if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert
214 #   define static_assert _Static_assert /* C11 requires this #define.  */
215 #  endif
216 # endif
217
218 /* @assert.h omit start@  */
219
220 /* Each of these macros verifies that its argument R is nonzero.  To
221    be portable, R should be an integer constant expression.  Unlike
222    assert (R), there is no run-time overhead.
223
224    There are two macros, since no single macro can be used in all
225    contexts in C.  verify_true (R) is for scalar contexts, including
226    integer constant expression contexts.  verify (R) is for declaration
227    contexts, e.g., the top level.  */
228
229 /* Verify requirement R at compile-time, as an integer constant expression.
230    Return 1.  This is equivalent to verify_expr (R, 1).
231
232    verify_true is obsolescent; please use verify_expr instead.  */
233
234 # define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")")
235
236 /* Verify requirement R at compile-time.  Return the value of the
237    expression E.  */
238
239 # define verify_expr(R, E) \
240     (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
241
242 /* Verify requirement R at compile-time, as a declaration without a
243    trailing ';'.  */
244
245 # define verify(R) _GL_VERIFY (R, "verify (" #R ")")
246
247 /* @assert.h omit end@  */
248
249 #endif