c-strtof, c-strtod, c-strtold: Make multithread-safe.
[gnulib.git] / doc / verify.texi
blob40fc459972511655449ce2ff859c1efd97b5711c
1 @c GNU verify module documentation
3 @c Copyright (C) 2006, 2009--2024 Free Software Foundation, Inc.
5 @c Permission is granted to copy, distribute and/or modify this document
6 @c under the terms of the GNU Free Documentation License, Version 1.3 or
7 @c any later version published by the Free Software Foundation; with no
8 @c Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.  A
9 @c copy of the license is at <https://www.gnu.org/licenses/fdl-1.3.en.html>.
11 @node Compile-time Assertions
12 @section Compile-time Assertions
14 @cindex assertion
15 @findex verify
16 @findex verify_expr
18 This module provides a header file @file{verify.h} that defines
19 macros related to compile-time verification.
21 Two of these macros are @code{verify (@var{V})} and @code{verify_expr
22 (@var{V}, @var{EXPR})}.  Both accept an integer constant expression
23 argument @var{V} and verify that it is nonzero.  If not, a compile-time error
24 results.
26 These two macros implement compile-time tests, as opposed to
27 the standard @code{assert} macro which supports only runtime tests.
28 Since the tests occur at compile-time, they are more reliable, and
29 they require no runtime overhead.
31 @code{verify (@var{V});} is a declaration; it can occur outside of
32 functions.  In contrast, @code{verify_expr (@var{V}, @var{EXPR})} is
33 an expression that returns the value of @var{EXPR}; it can be used in
34 macros that expand to expressions.  If @var{EXPR} is an integer
35 constant expression, then @code{verify_expr (@var{V}, @var{EXPR})} is
36 also an integer constant expression.  Although @var{EXPR} and
37 @code{verify_expr (@var{V}, @var{EXPR})}@ are guaranteed to have the
38 same side effects and value and type (after integer promotion), they
39 need not have the same type if @var{EXPR}'s type is an integer that is
40 narrower than @code{int} or @code{unsigned int}.
42 @var{V} should be an integer constant expression in the sense
43 of the C standard.  Its leaf operands should be integer, enumeration,
44 or character constants; or @code{sizeof} expressions that return
45 constants; or floating constants that are the immediate operands of
46 casts.  Outside a @code{sizeof} subexpression, @var{V} should
47 not contain any assignments, function calls, comma operators, casts to
48 non-integer types, or subexpressions whose values are outside the
49 representable ranges for their types.  If @var{V} is not an
50 integer constant expression, then a compiler might reject a usage like
51 @samp{verify (@var{V});} even when @var{V} is
52 nonzero.
54 Although the standard @code{assert} macro is a runtime test, C23 and C++17
55 specify a builtin @code{static_assert (@var{V})}, which differs
56 from @code{verify} in two major ways.  First, it can also be used
57 within a @code{struct} or @code{union} specifier, in place of an
58 ordinary member declaration.  Second, it allows the programmer to
59 specify, as an optional second argument, a compile-time diagnostic as
60 a string literal.  If your program is not intended to be portable to
61 compilers that lack C23 or C++17 @code{static_assert}, the only
62 advantage of @code{verify} is that its name is a bit shorter.
64 The @file{verify.h} header defines one more macro, @code{assume
65 (@var{E})}, which expands to an expression of type @code{void}
66 that causes the compiler to assume that @var{E} yields a nonzero
67 value.  @var{E} should be a scalar expression, and should not
68 have side effects; it may or may not be evaluated.  The behavior is
69 undefined if @var{E} would yield zero.  The main use of @code{assume}
70 is optimization, as the compiler may be able to generate better code
71 if it assumes @var{E}.  For best results, @var{E} should be simple
72 enough that a compiler can determine that it has no side effects: if
73 @var{E} calls an external function or accesses volatile storage the
74 compiler may not be able to optimize @var{E} away and @code{assume
75 (@var{E})} may therefore slow down the program.
77 Here are some example uses of these macros.
79 @example
80 #include <verify.h>
82 #include <limits.h>
83 #include <time.h>
85 /* Verify that time_t is an integer type.  */
86 verify ((time_t) 1.5 == 1);
88 /* Verify that time_t is no smaller than int.  */
89 verify (sizeof (int) <= sizeof (time_t));
91 /* Verify that time_t is signed.  */
92 verify ((time_t) -1 < 0);
94 /* Verify that time_t uses two's complement representation.  */
95 verify (~ (time_t) -1 == 0);
97 /* Return the maximum value of the integer type T,
98    verifying that T is an unsigned integer type.
99    The cast to (T) is outside the call to verify_expr
100    so that the result is of type T
101    even when T is narrower than unsigned int.  */
102 #define MAX_UNSIGNED_VAL(t) \
103    ((T) verify_expr (0 < (T) -1, -1))
105 /* Return T divided by CHAR_MAX + 1, where behavior is
106    undefined if T < 0.  In the common case where CHAR_MAX
107    is 127 the compiler can therefore implement the division
108    by shifting T right 7 bits, an optimization that would
109    not be valid if T were negative.  */
110 time_t
111 time_index (time_t t)
113   assume (0 <= t);
114   return t / (CHAR_MAX + 1);
118 @end example