1 /*	$NetBSD: assertions.h,v 1.1.1.1 2009/04/12 15:33:33 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1997-2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * Id: assertions.h,v 1.5 2008/11/14 02:36:51 marka Exp
22  */
23 
24 #ifndef ASSERTIONS_H
25 #define ASSERTIONS_H		1
26 
27 typedef enum {
28 	assert_require, assert_ensure, assert_insist, assert_invariant
29 } assertion_type;
30 
31 typedef void (*assertion_failure_callback)(const char *, int, assertion_type,
32 					   const char *, int);
33 
34 /* coverity[+kill] */
35 extern assertion_failure_callback __assertion_failed;
36 void set_assertion_failure_callback(assertion_failure_callback f);
37 const char *assertion_type_to_text(assertion_type type);
38 
39 #if defined(CHECK_ALL) || defined(__COVERITY__)
40 #define CHECK_REQUIRE		1
41 #define CHECK_ENSURE		1
42 #define CHECK_INSIST		1
43 #define CHECK_INVARIANT		1
44 #endif
45 
46 #if defined(CHECK_NONE) && !defined(__COVERITY__)
47 #define CHECK_REQUIRE		0
48 #define CHECK_ENSURE		0
49 #define CHECK_INSIST		0
50 #define CHECK_INVARIANT		0
51 #endif
52 
53 #ifndef CHECK_REQUIRE
54 #define CHECK_REQUIRE		1
55 #endif
56 
57 #ifndef CHECK_ENSURE
58 #define CHECK_ENSURE		1
59 #endif
60 
61 #ifndef CHECK_INSIST
62 #define CHECK_INSIST		1
63 #endif
64 
65 #ifndef CHECK_INVARIANT
66 #define CHECK_INVARIANT		1
67 #endif
68 
69 #if CHECK_REQUIRE != 0
70 #define REQUIRE(cond) \
71 	((void) ((cond) || \
72 		 ((__assertion_failed)(__FILE__, __LINE__, assert_require, \
73 				       #cond, 0), 0)))
74 #define REQUIRE_ERR(cond) \
75 	((void) ((cond) || \
76 		 ((__assertion_failed)(__FILE__, __LINE__, assert_require, \
77 				       #cond, 1), 0)))
78 #else
79 #define REQUIRE(cond)		((void) (cond))
80 #define REQUIRE_ERR(cond)	((void) (cond))
81 #endif /* CHECK_REQUIRE */
82 
83 #if CHECK_ENSURE != 0
84 #define ENSURE(cond) \
85 	((void) ((cond) || \
86 		 ((__assertion_failed)(__FILE__, __LINE__, assert_ensure, \
87 				       #cond, 0), 0)))
88 #define ENSURE_ERR(cond) \
89 	((void) ((cond) || \
90 		 ((__assertion_failed)(__FILE__, __LINE__, assert_ensure, \
91 				       #cond, 1), 0)))
92 #else
93 #define ENSURE(cond)		((void) (cond))
94 #define ENSURE_ERR(cond)	((void) (cond))
95 #endif /* CHECK_ENSURE */
96 
97 #if CHECK_INSIST != 0
98 #define INSIST(cond) \
99 	((void) ((cond) || \
100 		 ((__assertion_failed)(__FILE__, __LINE__, assert_insist, \
101 				       #cond, 0), 0)))
102 #define INSIST_ERR(cond) \
103 	((void) ((cond) || \
104 		 ((__assertion_failed)(__FILE__, __LINE__, assert_insist, \
105 				       #cond, 1), 0)))
106 #else
107 #define INSIST(cond)		((void) (cond))
108 #define INSIST_ERR(cond)	((void) (cond))
109 #endif /* CHECK_INSIST */
110 
111 #if CHECK_INVARIANT != 0
112 #define INVARIANT(cond) \
113 	((void) ((cond) || \
114 		 ((__assertion_failed)(__FILE__, __LINE__, assert_invariant, \
115 				       #cond, 0), 0)))
116 #define INVARIANT_ERR(cond) \
117 	((void) ((cond) || \
118 		 ((__assertion_failed)(__FILE__, __LINE__, assert_invariant, \
119 				       #cond, 1), 0)))
120 #else
121 #define INVARIANT(cond)		((void) (cond))
122 #define INVARIANT_ERR(cond)	((void) (cond))
123 #endif /* CHECK_INVARIANT */
124 #endif /* ASSERTIONS_H */
125 /*! \file */
126