summaryrefslogtreecommitdiffstats
path: root/gl/verify.h
diff options
context:
space:
mode:
Diffstat (limited to 'gl/verify.h')
-rw-r--r--gl/verify.h180
1 files changed, 136 insertions, 44 deletions
diff --git a/gl/verify.h b/gl/verify.h
index 4ad780c..d42d075 100644
--- a/gl/verify.h
+++ b/gl/verify.h
@@ -1,6 +1,6 @@
1/* Compile-time assert-like macros. 1/* Compile-time assert-like macros.
2 2
3 Copyright (C) 2005-2006, 2009-2010 Free Software Foundation, Inc. 3 Copyright (C) 2005-2006, 2009-2013 Free Software Foundation, Inc.
4 4
5 This program is free software: you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published by
@@ -17,21 +17,49 @@
17 17
18/* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */ 18/* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
19 19
20#ifndef VERIFY_H 20#ifndef _GL_VERIFY_H
21# define VERIFY_H 1 21#define _GL_VERIFY_H
22
23
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.
27
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.
30
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
44
45/* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other
46 system headers, defines a conflicting _Static_assert that is no
47 better than ours; override it. */
48#ifndef _GL_HAVE_STATIC_ASSERT
49# include <stddef.h>
50# undef _Static_assert
51#endif
22 52
23/* Each of these macros verifies that its argument R is nonzero. To 53/* Each of these macros verifies that its argument R is nonzero. To
24 be portable, R should be an integer constant expression. Unlike 54 be portable, R should be an integer constant expression. Unlike
25 assert (R), there is no run-time overhead. 55 assert (R), there is no run-time overhead.
26 56
27 There are two macros, since no single macro can be used in all 57 If _Static_assert works, verify (R) uses it directly. Similarly,
28 contexts in C. verify_true (R) is for scalar contexts, including 58 _GL_VERIFY_TRUE works by packaging a _Static_assert inside a struct
29 integer constant expression contexts. verify (R) is for declaration 59 that is an operand of sizeof.
30 contexts, e.g., the top level.
31
32 Symbols ending in "__" are private to this header.
33 60
34 The code below uses several ideas. 61 The code below uses several ideas for C++ compilers, and for C
62 compilers that do not support _Static_assert:
35 63
36 * The first step is ((R) ? 1 : -1). Given an expression R, of 64 * The first step is ((R) ? 1 : -1). Given an expression R, of
37 integral or boolean or floating-point type, this yields an 65 integral or boolean or floating-point type, this yields an
@@ -39,7 +67,9 @@
39 constant and nonnegative. 67 constant and nonnegative.
40 68
41 * Next this expression W is wrapped in a type 69 * Next this expression W is wrapped in a type
42 struct verify_type__ { unsigned int verify_error_if_negative_size__: W; }. 70 struct _gl_verify_type {
71 unsigned int _gl_verify_error_if_negative: W;
72 }.
43 If W is negative, this yields a compile-time error. No compiler can 73 If W is negative, this yields a compile-time error. No compiler can
44 deal with a bit-field of negative size. 74 deal with a bit-field of negative size.
45 75
@@ -53,7 +83,7 @@
53 83
54 void function (int n) { verify (n < 0); } 84 void function (int n) { verify (n < 0); }
55 85
56 * For the verify macro, the struct verify_type__ will need to 86 * For the verify macro, the struct _gl_verify_type will need to
57 somehow be embedded into a declaration. To be portable, this 87 somehow be embedded into a declaration. To be portable, this
58 declaration must declare an object, a constant, a function, or a 88 declaration must declare an object, a constant, a function, or a
59 typedef name. If the declared entity uses the type directly, 89 typedef name. If the declared entity uses the type directly,
@@ -91,11 +121,11 @@
91 Which of the following alternatives can be used? 121 Which of the following alternatives can be used?
92 122
93 extern int dummy [sizeof (struct {...})]; 123 extern int dummy [sizeof (struct {...})];
94 extern int dummy [sizeof (struct verify_type__ {...})]; 124 extern int dummy [sizeof (struct _gl_verify_type {...})];
95 extern void dummy (int [sizeof (struct {...})]); 125 extern void dummy (int [sizeof (struct {...})]);
96 extern void dummy (int [sizeof (struct verify_type__ {...})]); 126 extern void dummy (int [sizeof (struct _gl_verify_type {...})]);
97 extern int (*dummy (void)) [sizeof (struct {...})]; 127 extern int (*dummy (void)) [sizeof (struct {...})];
98 extern int (*dummy (void)) [sizeof (struct verify_type__ {...})]; 128 extern int (*dummy (void)) [sizeof (struct _gl_verify_type {...})];
99 129
100 In the second and sixth case, the struct type is exported to the 130 In the second and sixth case, the struct type is exported to the
101 outer scope; two such declarations therefore collide. GCC warns 131 outer scope; two such declarations therefore collide. GCC warns
@@ -105,59 +135,121 @@
105 extern int (*dummy (void)) [sizeof (struct {...})]; 135 extern int (*dummy (void)) [sizeof (struct {...})];
106 136
107 * GCC warns about duplicate declarations of the dummy function if 137 * GCC warns about duplicate declarations of the dummy function if
108 -Wredundant_decls is used. GCC 4.3 and later have a builtin 138 -Wredundant-decls is used. GCC 4.3 and later have a builtin
109 __COUNTER__ macro that can let us generate unique identifiers for 139 __COUNTER__ macro that can let us generate unique identifiers for
110 each dummy function, to suppress this warning. 140 each dummy function, to suppress this warning.
111 141
112 * This implementation exploits the fact that GCC does not warn about 142 * This implementation exploits the fact that older versions of GCC,
113 the last declaration mentioned above. If a future version of GCC 143 which do not support _Static_assert, also do not warn about the
114 introduces a warning for this, the problem could be worked around 144 last declaration mentioned above.
115 by using code specialized to GCC, just as __COUNTER__ is already
116 being used if available.
117 145
118 #if 4 <= __GNUC__ 146 * GCC warns if -Wnested-externs is enabled and verify() is used
119 # define verify(R) [another version to keep GCC happy] 147 within a function body; but inside a function, you can always
120 #endif 148 arrange to use verify_expr() instead.
121 149
122 * In C++, any struct definition inside sizeof is invalid. 150 * In C++, any struct definition inside sizeof is invalid.
123 Use a template type to work around the problem. */ 151 Use a template type to work around the problem. */
124 152
125/* Concatenate two preprocessor tokens. */ 153/* Concatenate two preprocessor tokens. */
126# define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y) 154#define _GL_CONCAT(x, y) _GL_CONCAT0 (x, y)
127# define _GL_CONCAT0(x, y) x##y 155#define _GL_CONCAT0(x, y) x##y
128 156
129/* _GL_COUNTER is an integer, preferably one that changes each time we 157/* _GL_COUNTER is an integer, preferably one that changes each time we
130 use it. Use __COUNTER__ if it works, falling back on __LINE__ 158 use it. Use __COUNTER__ if it works, falling back on __LINE__
131 otherwise. __LINE__ isn't perfect, but it's better than a 159 otherwise. __LINE__ isn't perfect, but it's better than a
132 constant. */ 160 constant. */
133# if defined __COUNTER__ && __COUNTER__ != __COUNTER__ 161#if defined __COUNTER__ && __COUNTER__ != __COUNTER__
134# define _GL_COUNTER __COUNTER__ 162# define _GL_COUNTER __COUNTER__
135# else 163#else
136# define _GL_COUNTER __LINE__ 164# define _GL_COUNTER __LINE__
137# endif 165#endif
138 166
139/* Generate a symbol with the given prefix, making it unique if 167/* Generate a symbol with the given prefix, making it unique if
140 possible. */ 168 possible. */
141# define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER) 169#define _GL_GENSYM(prefix) _GL_CONCAT (prefix, _GL_COUNTER)
142 170
143/* Verify requirement R at compile-time, as an integer constant expression. 171/* Verify requirement R at compile-time, as an integer constant expression
144 Return 1. */ 172 that returns 1. If R is false, fail at compile-time, preferably
173 with a diagnostic that includes the string-literal DIAGNOSTIC. */
145 174
146# ifdef __cplusplus 175#define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
176 (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))
177
178#ifdef __cplusplus
179# if !GNULIB_defined_struct__gl_verify_type
147template <int w> 180template <int w>
148 struct verify_type__ { unsigned int verify_error_if_negative_size__: w; }; 181 struct _gl_verify_type {
149# define verify_true(R) \ 182 unsigned int _gl_verify_error_if_negative: w;
150 (!!sizeof (verify_type__<(R) ? 1 : -1>)) 183 };
151# else 184# define GNULIB_defined_struct__gl_verify_type 1
152# define verify_true(R) \ 185# endif
153 (!!sizeof \ 186# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
154 (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; })) 187 _gl_verify_type<(R) ? 1 : -1>
188#elif defined _GL_HAVE__STATIC_ASSERT
189# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
190 struct { \
191 _Static_assert (R, DIAGNOSTIC); \
192 int _gl_dummy; \
193 }
194#else
195# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
196 struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }
197#endif
198
199/* Verify requirement R at compile-time, as a declaration without a
200 trailing ';'. If R is false, fail at compile-time, preferably
201 with a diagnostic that includes the string-literal DIAGNOSTIC.
202
203 Unfortunately, unlike C11, this implementation must appear as an
204 ordinary declaration, and cannot appear inside struct { ... }. */
205
206#ifdef _GL_HAVE__STATIC_ASSERT
207# define _GL_VERIFY _Static_assert
208#else
209# define _GL_VERIFY(R, DIAGNOSTIC) \
210 extern int (*_GL_GENSYM (_gl_verify_function) (void)) \
211 [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
212#endif
213
214/* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */
215#ifdef _GL_STATIC_ASSERT_H
216# if !defined _GL_HAVE__STATIC_ASSERT && !defined _Static_assert
217# define _Static_assert(R, DIAGNOSTIC) _GL_VERIFY (R, DIAGNOSTIC)
218# endif
219# if !defined _GL_HAVE_STATIC_ASSERT && !defined static_assert
220# define static_assert _Static_assert /* C11 requires this #define. */
155# endif 221# endif
222#endif
223
224/* @assert.h omit start@ */
225
226/* Each of these macros verifies that its argument R is nonzero. To
227 be portable, R should be an integer constant expression. Unlike
228 assert (R), there is no run-time overhead.
229
230 There are two macros, since no single macro can be used in all
231 contexts in C. verify_true (R) is for scalar contexts, including
232 integer constant expression contexts. verify (R) is for declaration
233 contexts, e.g., the top level. */
234
235/* Verify requirement R at compile-time, as an integer constant expression.
236 Return 1. This is equivalent to verify_expr (R, 1).
237
238 verify_true is obsolescent; please use verify_expr instead. */
239
240#define verify_true(R) _GL_VERIFY_TRUE (R, "verify_true (" #R ")")
241
242/* Verify requirement R at compile-time. Return the value of the
243 expression E. */
244
245#define verify_expr(R, E) \
246 (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))
156 247
157/* Verify requirement R at compile-time, as a declaration without a 248/* Verify requirement R at compile-time, as a declaration without a
158 trailing ';'. */ 249 trailing ';'. */
159 250
160# define verify(R) \ 251#define verify(R) _GL_VERIFY (R, "verify (" #R ")")
161 extern int (* _GL_GENSYM (verify_function) (void)) [verify_true (R)] 252
253/* @assert.h omit end@ */
162 254
163#endif 255#endif