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