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