annotate lib/verify.h @ 37191:2f1343191e67

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