annotate lib/verify.h @ 437:84950ae2e2bf 3.0

Added tag 3.0 for changeset 4c272633fce2
author lost@l-w.ca
date Fri, 29 Oct 2010 19:38:14 -0600
parents b8bf63962a99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
434
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
1 /* Compile-time assert-like macros.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
2
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
3 Copyright (C) 2005-2006, 2009-2010 Free Software Foundation, Inc.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
4
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
8 (at your option) any later version.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
9
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
13 GNU General Public License for more details.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
14
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
17
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
18 /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
19
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
20 #ifndef VERIFY_H
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
21 # define VERIFY_H 1
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
22
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
23 /* Each of these macros verifies that its argument R is nonzero. To
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
24 be portable, R should be an integer constant expression. Unlike
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
25 assert (R), there is no run-time overhead.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
26
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
27 There are two macros, since no single macro can be used in all
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
28 contexts in C. verify_true (R) is for scalar contexts, including
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
29 integer constant expression contexts. verify (R) is for declaration
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
30 contexts, e.g., the top level.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
31
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
32 Symbols ending in "__" are private to this header.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
33
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
34 The code below uses several ideas.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
35
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
36 * The first step is ((R) ? 1 : -1). Given an expression R, of
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
37 integral or boolean or floating-point type, this yields an
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
38 expression of integral type, whose value is later verified to be
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
39 constant and nonnegative.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
40
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
41 * Next this expression W is wrapped in a type
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
42 struct verify_type__ { unsigned int verify_error_if_negative_size__: W; }.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
43 If W is negative, this yields a compile-time error. No compiler can
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
44 deal with a bit-field of negative size.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
45
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
46 One might think that an array size check would have the same
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
47 effect, that is, that the type struct { unsigned int dummy[W]; }
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
48 would work as well. However, inside a function, some compilers
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
49 (such as C++ compilers and GNU C) allow local parameters and
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
50 variables inside array size expressions. With these compilers,
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
51 an array size check would not properly diagnose this misuse of
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
52 the verify macro:
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
53
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
54 void function (int n) { verify (n < 0); }
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
55
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
56 * For the verify macro, the struct verify_type__ will need to
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
57 somehow be embedded into a declaration. To be portable, this
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
58 declaration must declare an object, a constant, a function, or a
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
59 typedef name. If the declared entity uses the type directly,
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
60 such as in
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
61
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
62 struct dummy {...};
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
63 typedef struct {...} dummy;
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
64 extern struct {...} *dummy;
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
65 extern void dummy (struct {...} *);
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
66 extern struct {...} *dummy (void);
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
67
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
68 two uses of the verify macro would yield colliding declarations
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
69 if the entity names are not disambiguated. A workaround is to
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
70 attach the current line number to the entity name:
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
71
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
72 #define GL_CONCAT0(x, y) x##y
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
73 #define GL_CONCAT(x, y) GL_CONCAT0 (x, y)
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
74 extern struct {...} * GL_CONCAT(dummy,__LINE__);
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
75
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
76 But this has the problem that two invocations of verify from
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
77 within the same macro would collide, since the __LINE__ value
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
78 would be the same for both invocations.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
79
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
80 A solution is to use the sizeof operator. It yields a number,
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
81 getting rid of the identity of the type. Declarations like
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
82
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
83 extern int dummy [sizeof (struct {...})];
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
84 extern void dummy (int [sizeof (struct {...})]);
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
85 extern int (*dummy (void)) [sizeof (struct {...})];
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
86
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
87 can be repeated.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
88
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
89 * Should the implementation use a named struct or an unnamed struct?
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
90 Which of the following alternatives can be used?
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
91
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
92 extern int dummy [sizeof (struct {...})];
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
93 extern int dummy [sizeof (struct verify_type__ {...})];
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
94 extern void dummy (int [sizeof (struct {...})]);
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
95 extern void dummy (int [sizeof (struct verify_type__ {...})]);
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
96 extern int (*dummy (void)) [sizeof (struct {...})];
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
97 extern int (*dummy (void)) [sizeof (struct verify_type__ {...})];
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
98
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
99 In the second and sixth case, the struct type is exported to the
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
100 outer scope; two such declarations therefore collide. GCC warns
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
101 about the first, third, and fourth cases. So the only remaining
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
102 possibility is the fifth case:
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
103
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
104 extern int (*dummy (void)) [sizeof (struct {...})];
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
105
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
106 * This implementation exploits the fact that GCC does not warn about
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
107 the last declaration mentioned above. If a future version of GCC
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
108 introduces a warning for this, the problem could be worked around
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
109 by using code specialized to GCC, e.g.,:
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
110
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
111 #if 4 <= __GNUC__
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
112 # define verify(R) \
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
113 extern int (* verify_function__ (void)) \
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
114 [__builtin_constant_p (R) && (R) ? 1 : -1]
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
115 #endif
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
116
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
117 * In C++, any struct definition inside sizeof is invalid.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
118 Use a template type to work around the problem. */
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
119
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
120
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
121 /* Verify requirement R at compile-time, as an integer constant expression.
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
122 Return 1. */
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
123
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
124 # ifdef __cplusplus
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
125 template <int w>
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
126 struct verify_type__ { unsigned int verify_error_if_negative_size__: w; };
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
127 # define verify_true(R) \
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
128 (!!sizeof (verify_type__<(R) ? 1 : -1>))
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
129 # else
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
130 # define verify_true(R) \
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
131 (!!sizeof \
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
132 (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
133 # endif
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
134
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
135 /* Verify requirement R at compile-time, as a declaration without a
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
136 trailing ';'. */
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
137
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
138 # define verify(R) extern int (* verify_function__ (void)) [verify_true (R)]
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
139
b8bf63962a99 Added various generated files for release
lost@l-w.ca
parents:
diff changeset
140 #endif