assertions.h revision 1.1.1.1.10.3 1 /* $NetBSD: assertions.h,v 1.1.1.1.10.3 2011/01/23 21:47:50 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1997-1999 by Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and 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
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*
21 * Id: assertions.h,v 1.2.18.2 2008/10/15 03:57:21 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
37 void set_assertion_failure_callback(assertion_failure_callback f);
38 const char *assertion_type_to_text(assertion_type type);
39
40 #if defined(CHECK_ALL) || defined(__COVERITY__)
41 #define CHECK_REQUIRE 1
42 #define CHECK_ENSURE 1
43 #define CHECK_INSIST 1
44 #define CHECK_INVARIANT 1
45 #endif
46
47 #if defined(CHECK_NONE) && !defined(__COVERITY__)
48 #define CHECK_REQUIRE 0
49 #define CHECK_ENSURE 0
50 #define CHECK_INSIST 0
51 #define CHECK_INVARIANT 0
52 #endif
53
54 #ifndef CHECK_REQUIRE
55 #define CHECK_REQUIRE 1
56 #endif
57
58 #ifndef CHECK_ENSURE
59 #define CHECK_ENSURE 1
60 #endif
61
62 #ifndef CHECK_INSIST
63 #define CHECK_INSIST 1
64 #endif
65
66 #ifndef CHECK_INVARIANT
67 #define CHECK_INVARIANT 1
68 #endif
69
70 #if CHECK_REQUIRE != 0
71 #define REQUIRE(cond) \
72 ((void) ((cond) || \
73 ((__assertion_failed)(__FILE__, __LINE__, assert_require, \
74 #cond, 0), 0)))
75 #define REQUIRE_ERR(cond) \
76 ((void) ((cond) || \
77 ((__assertion_failed)(__FILE__, __LINE__, assert_require, \
78 #cond, 1), 0)))
79 #else
80 #define REQUIRE(cond) ((void) (cond))
81 #define REQUIRE_ERR(cond) ((void) (cond))
82 #endif /* CHECK_REQUIRE */
83
84 #if CHECK_ENSURE != 0
85 #define ENSURE(cond) \
86 ((void) ((cond) || \
87 ((__assertion_failed)(__FILE__, __LINE__, assert_ensure, \
88 #cond, 0), 0)))
89 #define ENSURE_ERR(cond) \
90 ((void) ((cond) || \
91 ((__assertion_failed)(__FILE__, __LINE__, assert_ensure, \
92 #cond, 1), 0)))
93 #else
94 #define ENSURE(cond) ((void) (cond))
95 #define ENSURE_ERR(cond) ((void) (cond))
96 #endif /* CHECK_ENSURE */
97
98 #if CHECK_INSIST != 0
99 #define INSIST(cond) \
100 ((void) ((cond) || \
101 ((__assertion_failed)(__FILE__, __LINE__, assert_insist, \
102 #cond, 0), 0)))
103 #define INSIST_ERR(cond) \
104 ((void) ((cond) || \
105 ((__assertion_failed)(__FILE__, __LINE__, assert_insist, \
106 #cond, 1), 0)))
107 #else
108 #define INSIST(cond) ((void) (cond))
109 #define INSIST_ERR(cond) ((void) (cond))
110 #endif /* CHECK_INSIST */
111
112 #if CHECK_INVARIANT != 0
113 #define INVARIANT(cond) \
114 ((void) ((cond) || \
115 ((__assertion_failed)(__FILE__, __LINE__, assert_invariant, \
116 #cond, 0), 0)))
117 #define INVARIANT_ERR(cond) \
118 ((void) ((cond) || \
119 ((__assertion_failed)(__FILE__, __LINE__, assert_invariant, \
120 #cond, 1), 0)))
121 #else
122 #define INVARIANT(cond) ((void) (cond))
123 #define INVARIANT_ERR(cond) ((void) (cond))
124 #endif /* CHECK_INVARIANT */
125 #endif /* ASSERTIONS_H */
126 /*! \file */
127