diff options
-rw-r--r-- | lib/verify.h | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/lib/verify.h b/lib/verify.h index b86acaea9..7880526fe 100644 --- a/lib/verify.h +++ b/lib/verify.h @@ -16,28 +16,42 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +/* Written by Paul Eggert and Jim Meyering. */ + #ifndef VERIFY_H # define VERIFY_H 1 -# ifndef verify_decl -# define GL_CONCAT0(x, y) x##y -# define GL_CONCAT(x, y) GL_CONCAT0 (x, y) - -/* Verify requirement, R, at compile-time, as a declaration. - The implementation uses a struct declaration whose name includes the - expansion of __LINE__, so there is a small chance that two uses of - verify_decl from different files will end up colliding (for example, - f.c includes f.h and verify_decl is used on the same line in each). */ -# define verify_decl(R) \ - struct GL_CONCAT (ct_assert_, __LINE__) { char a[(R) ? 1 : -1]; } -# endif - -/* Verify requirement, R, at compile-time, as an expression. - Unlike assert, there is no run-time overhead. Unlike verify_decl, - above, there is no risk of collision, since there is no declared name. - This macro may be used in some contexts where the other may not, and - vice versa. Return void. */ -# undef verify -# define verify(R) ((void) sizeof (struct { char a[(R) ? 1 : -1]; })) +# define GL_CONCAT0(x, y) x##y +# define GL_CONCAT(x, y) GL_CONCAT0 (x, y) + +/* A type that is valid if and only R is nonzero. + R should be an integer constant expression. + verify_type__ and verify_error_if_negative_size__ are symbols that + are private to this header file. */ + +# define verify_type__(R) \ + struct { int verify_error_if_negative_size__[(R) ? 1 : -1]; } + +/* Verify requirement R at compile-time, as a declaration. + R should be an integer constant expression. + Unlike assert, there is no run-time overhead. + + The implementation uses __LINE__ to lessen the probability of + generating a warning that verify_function_NNN is multiply declared. + However, even if two declarations in different files have the same + __LINE__, the multiple declarations are still valid C89 and C99 + code because they simply redeclare the same external function, so + no conforming compiler will reject them outright. */ + +# define verify(R) \ + extern verify_type__ (R) GL_CONCAT (verify_function_, __LINE__) (void) + +/* Verify requirement R at compile-time, as an expression. + R should be an integer constant expression. + Unlike assert, there is no run-time overhead. + This macro can be used in some contexts where verify cannot, and vice versa. + Return void. */ + +# define verify_expr(R) ((void) sizeof (verify_type__ (R))) #endif |