2 * I.h - support for invariants (assertions) using C code.
4 * Copyright (c) 1997 Phil Maker
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Id: I.h,v 1.1.1.1 1997/11/23 11:45:50 pjm Exp
42 * nana-config.h - the system wide configuration file; we put the ifndef
43 * around it to avoid the file 5 million times during a compile.
46 #ifndef _nana_config_h_
47 #include <nana-config.h>
51 * I_LEVEL sets the level of invariant analogously to NDEBUG in assert.h
53 * I_LEVEL == 2: invariants are always evaluated.
54 * I_LEVEL == 1: evaluate invariants iff they have a true GUARD.
55 * I_LEVEL == 0: invariants are never evaluated.
58 #ifndef I_LEVEL /* define DEFAULT for I_LEVEL */
64 * I_DEFAULT_GUARD - the default guard expression; an invariant is checked
65 * iff the guard is true. By default its always true.
68 #ifndef I_DEFAULT_GUARD
69 #define I_DEFAULT_GUARD (1)
73 * I_DEFAULT_PARAMS - the default value to be passed as the second argument
74 * to the handler macro when an invariant fails.
78 #ifndef I_DEFAULT_PARAMS
79 #define I_DEFAULT_PARAMS /* nothing */
83 * I_DEFAULT_HANDLER(expr,file,line,param) - called when an error is detected.
86 #ifndef I_DEFAULT_HANDLER /* define default handler */
87 void _I_default_handler(char *expr
, char *file
, int line
);
89 #define I_DEFAULT_HANDLER(expr,file,line,param) \
90 _I_default_handler(expr,__FILE__,__LINE__)
92 #endif /* I_DEFAULT_HANDLER */
95 * _IGHPS(e,g,h,p,s) - implements the general case for invariant handling.
97 * e - expression to check
98 * g - guard, check only if this is true (subject to I_DEFAULT_LEVEL)
99 * h - handler, called when a failure is detected
100 * p - parameter to pass off to the handler
101 * s - string representation of the expression (e.g. "I(x>=i)")
103 * _ISD(e) - generates a data declaration for use in postconditions
104 * _ISG(e,g) - generates a guarded assignment to a data declaration
105 * for use in postconditions
107 * N.B. The two types are necessary since we cannot guard a C declaration
108 * with an if statement.
111 #if I_LEVEL == 2 /* always check the assertion */
112 #define _IGHPS(e,g,h,p,s) \
115 h (s, __FILE__, __LINE__, p); \
121 #elif I_LEVEL == 1 /* check it iff g is true */
122 #define _IGHPS(e,g,h,p,s) \
126 h (s, __FILE__, __LINE__, p); \
137 #elif I_LEVEL == 0 /* no assertions so just remove them */
138 #define _IGHPS(e,g,h,p,s) /* nothing */
139 #define _ID(e) /* nothing */
140 #define _ISG(e,g) /* nothing */
144 * And all the user macros; these are used to put in the default arguments.
145 * The name is used to determine the arguments; e.g. IGH takes an expression
146 * to check; a guard and a handler as parameters. The letters in the names
147 * are in ascending order (i.e. IGH(...) not IHG(...)).
149 * I[G][H][P] - it must be true (e) with an optional guard, handler and
150 * parameter for the handler.
151 * N[G][H][P] - as for I... except that (e) must never ever be true.
155 _IGHPS(e,I_DEFAULT_GUARD,I_DEFAULT_HANDLER,I_DEFAULT_PARAMS,"I("#e")")
157 _IGHPS(e,g,I_DEFAULT_HANDLER,I_DEFAULT_PARAMS,"I("#e")")
159 _IGHPS(e,I_DEFAULT_GUARD,h,I_DEFAULT_PARAMS,"I("#e")")
161 _IGHPS(e,I_DEFAULT_GUARD,I_DEFAULT_HANDLER,p,"I("#e")")
163 _IGHPS(e,g,h,I_DEFAULT_PARAMS,"I("#e")")
165 _IGHPS(e,g,I_DEFAULT_HANDLER,p,"I("#e")")
167 _IGHPS(e,I_DEFAULT_GUARD,h,p,"I("#e")")
168 #define IGHP(e,g,h,p) \
169 _IGHPS(e,g,h,p,"I("#e")")
172 _IGHPS((!(e)),I_DEFAULT_GUARD,I_DEFAULT_HANDLER,I_DEFAULT_PARAMS,"N("#e")")
174 _IGHPS((!(e)),g,I_DEFAULT_HANDLER,I_DEFAULT_PARAMS,"N("#e")")
176 _IGHPS((!(e)),I_DEFAULT_GUARD,h,I_DEFAULT_PARAMS,"N("#e")")
178 _IGHPS((!(e)),I_DEFAULT_GUARD,I_DEFAULT_HANDLER,p,"N("#e")")
180 _IGHPS((!(e)),g,h,I_DEFAULT_PARAMS,"N("#e")")
182 _IGHPS((!(e)),g,I_DEFAULT_HANDLER,p,"N("#e")")
184 _IGHPS((!(e)),I_DEFAULT_GUARD,h,p,"N("#e")")
185 #define NGHP(e,g,h,p) \
186 _IGHPS((!(e)),g,h,p,"N("#e")")
189 * ID(e) - declares a variable to be used to store values for a postcondition.
190 * This can include an initialiser.
191 * Note this declaration is not disabled by I_DEFAULT_GUARD
192 * IS(e) - an assignment to a variable. This statement is enabled by
194 * ISG(e,g) - the guarded version of IS
198 #define IS(e) _ISG(e,I_DEFAULT_GUARD)
199 #define ISG(e,g) _ISG(e,g)
201 #else /* defined(WITHOUT_NANA) */
203 #define I(e) /* empty */
204 #define IG(e,g) /* empty */
205 #define IH(e,h) /* empty */
206 #define IP(e,p) /* empty */
207 #define IGH(e,g,h) /* empty */
208 #define IGP(e,g,p) /* empty */
209 #define IHP(e,h,p) /* empty */
210 #define IGHP(e,g,h,p) /* empty */
212 #define N(e) /* empty */
213 #define NG(e,g) /* empty */
214 #define NH(e,h) /* empty */
215 #define NP(e,p) /* empty */
216 #define NGH(e,g,h) /* empty */
217 #define NGP(e,g,p) /* empty */
218 #define NHP(e,h,p) /* empty */
219 #define NGHP(e,g,h,p) /* empty */
221 #define ID(e) /* empty */
222 #define IS(e) /* empty */
223 #define ISG(e,g) /* empty */
225 #endif /* !defined(WITHOUT_NANA) */