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
105
125
extern int (*dummy (void)) [sizeof (struct {...})];
107
127
* GCC warns about duplicate declarations of the dummy function if
108
-Wredundant_decls is used. GCC 4.3 and later have a builtin
128
-Wredundant-decls is used. GCC 4.3 and later have a builtin
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.
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.
119
# define verify(R) [another version to keep GCC happy]
136
* GCC warns if -Wnested-externs is enabled and verify() is used
137
within a function body; but inside a function, you can always
138
arrange to use verify_expr() instead.
122
140
* In C++, any struct definition inside sizeof is invalid.
123
141
Use a template type to work around the problem. */
141
159
# define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
143
/* Verify requirement R at compile-time, as an integer constant expression.
161
/* Verify requirement R at compile-time, as an integer constant expression
162
that returns 1. If R is false, fail at compile-time, preferably
163
with a diagnostic that includes the string-literal DIAGNOSTIC. */
165
# define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
166
(!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
146
168
# ifdef __cplusplus
169
# 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; }))
171
struct _gl_verify_type {
172
unsigned int _gl_verify_error_if_negative: w;
174
# define GNULIB_defined_struct__gl_verify_type 1
176
# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
177
_gl_verify_type<(R) ? 1 : -1>
178
# elif defined _GL_HAVE__STATIC_ASSERT
179
# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
181
_Static_assert (R, DIAGNOSTIC); \
185
# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
186
struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
189
/* Verify requirement R at compile-time, as a declaration without a
190
trailing ';'. If R is false, fail at compile-time, preferably
191
with a diagnostic that includes the string-literal DIAGNOSTIC.
193
Unfortunately, unlike C11, this implementation must appear as an
194
ordinary declaration, and cannot appear inside struct { ... }. */
196
# ifdef _GL_HAVE__STATIC_ASSERT
197
# define _GL_VERIFY _Static_assert
199
# define _GL_VERIFY(R, DIAGNOSTIC) \
200
extern int (*_GL_GENSYM (_gl_verify_function) (void)) \
201
[_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
204
/* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */
205
# ifdef _GL_STATIC_ASSERT_H
206
# if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert
207
# define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC)
209
# if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert
210
# define static_assert _Static_assert /* C11 requires this #define. */
214
/* @assert.h omit start@ */
216
/* Each of these macros verifies that its argument R is nonzero. To
217
be portable, R should be an integer constant expression. Unlike
218
assert (R), there is no run-time overhead.
220
There are two macros, since no single macro can be used in all
221
contexts in C. verify_true (R) is for scalar contexts, including
222
integer constant expression contexts. verify (R) is for declaration
223
contexts, e.g., the top level. */
225
/* Verify requirement R at compile-time, as an integer constant expression.
226
Return 1. This is equivalent to verify_expr (R, 1).
228
verify_true is obsolescent; please use verify_expr instead. */
230
# define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")")
232
/* Verify requirement R at compile-time. Return the value of the
235
# define verify_expr(R, E) \
236
(_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
157
238
/* Verify requirement R at compile-time, as a declaration without a
161
extern int (* _GL_GENSYM (verify_function) (void)) [verify_true (R)]
241
# define verify(R) _GL_VERIFY (R, "verify (" #R ")")
243
/* @assert.h omit end@ */