18
18
/* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
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__ || (__GNUC__ == 4 && 6 <= __GNUC_MINOR__)) && !defined __cplusplus
35
# define _GL_HAVE__STATIC_ASSERT 1
37
/* The condition (99 < __GNUC__) is temporary, until we know about the
38
first G++ release that supports static_assert. */
39
# if (99 < __GNUC__) && defined __cplusplus
40
# define _GL_HAVE_STATIC_ASSERT 1
23
43
/* Each of these macros verifies that its argument R is nonzero. To
24
44
be portable, R should be an integer constant expression. Unlike
25
45
assert (R), there is no run-time overhead.
27
There are two macros, since no single macro can be used in all
28
contexts in C. verify_true (R) is for scalar contexts, including
29
integer constant expression contexts. verify (R) is for declaration
30
contexts, e.g., the top level.
32
Symbols ending in "__" are private to this header.
34
The code below uses several ideas.
47
If _Static_assert works, verify (R) uses it directly. Similarly,
48
_GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
49
that is an operand of sizeof.
51
The code below uses several ideas for C++ compilers, and for C
52
compilers that do not support _Static_assert:
36
54
* The first step is ((R) ? 1 : -1). Given an expression R, of
37
55
integral or boolean or floating-point type, this yields an
91
111
Which of the following alternatives can be used?
93
113
extern int dummy [sizeof (struct {...})];
94
extern int dummy [sizeof (struct verify_type__ {...})];
114
extern int dummy [sizeof (struct _gl_verify_type {...})];
95
115
extern void dummy (int [sizeof (struct {...})]);
96
extern void dummy (int [sizeof (struct verify_type__ {...})]);
116
extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
97
117
extern int (*dummy (void)) [sizeof (struct {...})];
98
extern int (*dummy (void)) [sizeof (struct verify_type__ {...})];
118
extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
100
120
In the second and sixth case, the struct type is exported to the
101
121
outer scope; two such declarations therefore collide. GCC warns
109
129
__COUNTER__ macro that can let us generate unique identifiers for
110
130
each dummy function, to suppress this warning.
112
* This implementation exploits the fact that GCC does not warn about
113
the last declaration mentioned above. If a future version of GCC
114
introduces a warning for this, the problem could be worked around
115
by using code specialized to GCC, just as __COUNTER__ is already
116
being used if available.
119
# define verify(R) [another version to keep GCC happy]
132
* This implementation exploits the fact that older versions of GCC,
133
which do not support _Static_assert, also do not warn about the
134
last declaration mentioned above.
122
136
* In C++, any struct definition inside sizeof is invalid.
123
137
Use a template type to work around the problem. */
141
155
# define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
143
/* Verify requirement R at compile-time, as an integer constant expression.
157
/* Verify requirement R at compile-time, as an integer constant expression
158
that returns 1. If R is false, fail at compile-time, preferably
159
with a diagnostic that includes the string-literal DIAGNOSTIC. */
161
# define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
162
(!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
146
164
# ifdef __cplusplus
165
# if !GNULIB_defined_struct__gl_verify_type
148
struct verify_type__ { unsigned int verify_error_if_negative_size__: w; };
149
# define verify_true(R) \
150
(!!sizeof (verify_type__<(R) ? 1 : -1>))
152
# define verify_true(R) \
154
(struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
167
struct _gl_verify_type {
168
unsigned int _gl_verify_error_if_negative: w;
170
# define GNULIB_defined_struct__gl_verify_type 1
172
# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
173
_gl_verify_type<(R) ? 1 : -1>
174
# elif defined _GL_HAVE__STATIC_ASSERT
175
# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
177
_Static_assert (R, DIAGNOSTIC); \
181
# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
182
struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
185
/* Verify requirement R at compile-time, as a declaration without a
186
trailing ';'. If R is false, fail at compile-time, preferably
187
with a diagnostic that includes the string-literal DIAGNOSTIC.
189
Unfortunately, unlike C11, this implementation must appear as an
190
ordinary declaration, and cannot appear inside struct { ... }. */
192
# ifdef _GL_HAVE__STATIC_ASSERT
193
# define _GL_VERIFY _Static_assert
195
# define _GL_VERIFY(R, DIAGNOSTIC) \
196
extern int (*_GL_GENSYM (_gl_verify_function) (void)) \
197
[_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
200
/* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */
201
# ifdef _GL_STATIC_ASSERT_H
202
# if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert
203
# define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC)
205
# if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert
206
# define static_assert _Static_assert /* C11 requires this #define. */
210
/* @assert.h omit start@ */
212
/* Each of these macros verifies that its argument R is nonzero. To
213
be portable, R should be an integer constant expression. Unlike
214
assert (R), there is no run-time overhead.
216
There are two macros, since no single macro can be used in all
217
contexts in C. verify_true (R) is for scalar contexts, including
218
integer constant expression contexts. verify (R) is for declaration
219
contexts, e.g., the top level. */
221
/* Verify requirement R at compile-time, as an integer constant expression.
222
Return 1. This is equivalent to verify_expr (R, 1).
224
verify_true is obsolescent; please use verify_expr instead. */
226
# define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")")
228
/* Verify requirement R at compile-time. Return the value of the
231
# define verify_expr(R, E) \
232
(_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
157
234
/* Verify requirement R at compile-time, as a declaration without a
161
extern int (* _GL_GENSYM (verify_function) (void)) [verify_true (R)]
237
# define verify(R) _GL_VERIFY (R, "verify (" #R ")")
239
/* @assert.h omit end@ */