source: trunk/essentials/sys-devel/m4/lib/verify.h

Last change on this file was 3090, checked in by bird, 18 years ago

m4 1.4.8

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