Home | History | Annotate | Line # | Download | only in isc
      1  1.5  christos /*	$NetBSD: assertions.h,v 1.5 2009/04/12 17:07:16 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.5  christos  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
      5  1.5  christos  * Copyright (C) 1997-2001  Internet Software Consortium.
      6  1.1  christos  *
      7  1.5  christos  * Permission to use, copy, modify, and/or 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.5  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
     12  1.5  christos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
     13  1.5  christos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
     14  1.5  christos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
     15  1.5  christos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
     16  1.5  christos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
     17  1.5  christos  * PERFORMANCE OF THIS SOFTWARE.
     18  1.1  christos  */
     19  1.1  christos 
     20  1.1  christos /*
     21  1.5  christos  * Id: assertions.h,v 1.5 2008/11/14 02:36:51 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.5  christos /* coverity[+kill] */
     35  1.1  christos extern assertion_failure_callback __assertion_failed;
     36  1.1  christos void set_assertion_failure_callback(assertion_failure_callback f);
     37  1.1  christos const char *assertion_type_to_text(assertion_type type);
     38  1.1  christos 
     39  1.5  christos #if defined(CHECK_ALL) || defined(__COVERITY__)
     40  1.1  christos #define CHECK_REQUIRE		1
     41  1.1  christos #define CHECK_ENSURE		1
     42  1.1  christos #define CHECK_INSIST		1
     43  1.1  christos #define CHECK_INVARIANT		1
     44  1.1  christos #endif
     45  1.1  christos 
     46  1.5  christos #if defined(CHECK_NONE) && !defined(__COVERITY__)
     47  1.1  christos #define CHECK_REQUIRE		0
     48  1.1  christos #define CHECK_ENSURE		0
     49  1.1  christos #define CHECK_INSIST		0
     50  1.1  christos #define CHECK_INVARIANT		0
     51  1.1  christos #endif
     52  1.1  christos 
     53  1.3  uebayasi #ifdef _DIAGNOSTIC
     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.3  uebayasi #endif	/* _DIAGNOSTIC */
     70  1.1  christos 
     71  1.1  christos #if CHECK_REQUIRE != 0
     72  1.1  christos #define REQUIRE(cond) \
     73  1.1  christos 	((void) ((cond) || \
     74  1.1  christos 		 ((__assertion_failed)(__FILE__, __LINE__, assert_require, \
     75  1.1  christos 				       #cond, 0), 0)))
     76  1.1  christos #define REQUIRE_ERR(cond) \
     77  1.1  christos 	((void) ((cond) || \
     78  1.1  christos 		 ((__assertion_failed)(__FILE__, __LINE__, assert_require, \
     79  1.1  christos 				       #cond, 1), 0)))
     80  1.1  christos #else
     81  1.1  christos #define REQUIRE(cond)		((void) (cond))
     82  1.1  christos #define REQUIRE_ERR(cond)	((void) (cond))
     83  1.1  christos #endif /* CHECK_REQUIRE */
     84  1.1  christos 
     85  1.1  christos #if CHECK_ENSURE != 0
     86  1.1  christos #define ENSURE(cond) \
     87  1.1  christos 	((void) ((cond) || \
     88  1.1  christos 		 ((__assertion_failed)(__FILE__, __LINE__, assert_ensure, \
     89  1.1  christos 				       #cond, 0), 0)))
     90  1.1  christos #define ENSURE_ERR(cond) \
     91  1.1  christos 	((void) ((cond) || \
     92  1.1  christos 		 ((__assertion_failed)(__FILE__, __LINE__, assert_ensure, \
     93  1.1  christos 				       #cond, 1), 0)))
     94  1.1  christos #else
     95  1.1  christos #define ENSURE(cond)		((void) (cond))
     96  1.1  christos #define ENSURE_ERR(cond)	((void) (cond))
     97  1.1  christos #endif /* CHECK_ENSURE */
     98  1.1  christos 
     99  1.1  christos #if CHECK_INSIST != 0
    100  1.1  christos #define INSIST(cond) \
    101  1.1  christos 	((void) ((cond) || \
    102  1.1  christos 		 ((__assertion_failed)(__FILE__, __LINE__, assert_insist, \
    103  1.1  christos 				       #cond, 0), 0)))
    104  1.1  christos #define INSIST_ERR(cond) \
    105  1.1  christos 	((void) ((cond) || \
    106  1.1  christos 		 ((__assertion_failed)(__FILE__, __LINE__, assert_insist, \
    107  1.1  christos 				       #cond, 1), 0)))
    108  1.1  christos #else
    109  1.4     lukem #if !defined(__lint__)
    110  1.1  christos #define INSIST(cond)		((void) (cond))
    111  1.1  christos #define INSIST_ERR(cond)	((void) (cond))
    112  1.4     lukem #else /* !__lint__ */
    113  1.4     lukem #define INSIST(cond)
    114  1.4     lukem #define INSIST_ERR(cond)
    115  1.4     lukem #endif /* !__lint__ */
    116  1.1  christos #endif /* CHECK_INSIST */
    117  1.1  christos 
    118  1.1  christos #if CHECK_INVARIANT != 0
    119  1.1  christos #define INVARIANT(cond) \
    120  1.1  christos 	((void) ((cond) || \
    121  1.1  christos 		 ((__assertion_failed)(__FILE__, __LINE__, assert_invariant, \
    122  1.1  christos 				       #cond, 0), 0)))
    123  1.1  christos #define INVARIANT_ERR(cond) \
    124  1.1  christos 	((void) ((cond) || \
    125  1.1  christos 		 ((__assertion_failed)(__FILE__, __LINE__, assert_invariant, \
    126  1.1  christos 				       #cond, 1), 0)))
    127  1.1  christos #else
    128  1.1  christos #define INVARIANT(cond)		((void) (cond))
    129  1.1  christos #define INVARIANT_ERR(cond)	((void) (cond))
    130  1.1  christos #endif /* CHECK_INVARIANT */
    131  1.1  christos #endif /* ASSERTIONS_H */
    132  1.2  christos /*! \file */
    133