Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 1 | /* $NetBSD: assertions.h,v 1.5 2009/04/12 17:07:16 christos Exp $ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2 | |
| 3 | /* |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 4 | * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") |
| 5 | * Copyright (C) 1997-2001 Internet Software Consortium. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 6 | * |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 7 | * Permission to use, copy, modify, and/or distribute this software for any |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | * |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 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. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 21 | * Id: assertions.h,v 1.5 2008/11/14 02:36:51 marka Exp |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 34 | /* coverity[+kill] */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 39 | #if defined(CHECK_ALL) || defined(__COVERITY__) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | #define CHECK_REQUIRE 1 |
| 41 | #define CHECK_ENSURE 1 |
| 42 | #define CHECK_INSIST 1 |
| 43 | #define CHECK_INVARIANT 1 |
| 44 | #endif |
| 45 | |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 46 | #if defined(CHECK_NONE) && !defined(__COVERITY__) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | #define CHECK_REQUIRE 0 |
| 48 | #define CHECK_ENSURE 0 |
| 49 | #define CHECK_INSIST 0 |
| 50 | #define CHECK_INVARIANT 0 |
| 51 | #endif |
| 52 | |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 53 | #ifdef _DIAGNOSTIC |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 69 | #endif /* _DIAGNOSTIC */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 70 | |
| 71 | #if CHECK_REQUIRE != 0 |
| 72 | #define REQUIRE(cond) \ |
| 73 | ((void) ((cond) || \ |
| 74 | ((__assertion_failed)(__FILE__, __LINE__, assert_require, \ |
| 75 | #cond, 0), 0))) |
| 76 | #define REQUIRE_ERR(cond) \ |
| 77 | ((void) ((cond) || \ |
| 78 | ((__assertion_failed)(__FILE__, __LINE__, assert_require, \ |
| 79 | #cond, 1), 0))) |
| 80 | #else |
| 81 | #define REQUIRE(cond) ((void) (cond)) |
| 82 | #define REQUIRE_ERR(cond) ((void) (cond)) |
| 83 | #endif /* CHECK_REQUIRE */ |
| 84 | |
| 85 | #if CHECK_ENSURE != 0 |
| 86 | #define ENSURE(cond) \ |
| 87 | ((void) ((cond) || \ |
| 88 | ((__assertion_failed)(__FILE__, __LINE__, assert_ensure, \ |
| 89 | #cond, 0), 0))) |
| 90 | #define ENSURE_ERR(cond) \ |
| 91 | ((void) ((cond) || \ |
| 92 | ((__assertion_failed)(__FILE__, __LINE__, assert_ensure, \ |
| 93 | #cond, 1), 0))) |
| 94 | #else |
| 95 | #define ENSURE(cond) ((void) (cond)) |
| 96 | #define ENSURE_ERR(cond) ((void) (cond)) |
| 97 | #endif /* CHECK_ENSURE */ |
| 98 | |
| 99 | #if CHECK_INSIST != 0 |
| 100 | #define INSIST(cond) \ |
| 101 | ((void) ((cond) || \ |
| 102 | ((__assertion_failed)(__FILE__, __LINE__, assert_insist, \ |
| 103 | #cond, 0), 0))) |
| 104 | #define INSIST_ERR(cond) \ |
| 105 | ((void) ((cond) || \ |
| 106 | ((__assertion_failed)(__FILE__, __LINE__, assert_insist, \ |
| 107 | #cond, 1), 0))) |
| 108 | #else |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 109 | #if !defined(__lint__) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 110 | #define INSIST(cond) ((void) (cond)) |
| 111 | #define INSIST_ERR(cond) ((void) (cond)) |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 112 | #else /* !__lint__ */ |
| 113 | #define INSIST(cond) |
| 114 | #define INSIST_ERR(cond) |
| 115 | #endif /* !__lint__ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 116 | #endif /* CHECK_INSIST */ |
| 117 | |
| 118 | #if CHECK_INVARIANT != 0 |
| 119 | #define INVARIANT(cond) \ |
| 120 | ((void) ((cond) || \ |
| 121 | ((__assertion_failed)(__FILE__, __LINE__, assert_invariant, \ |
| 122 | #cond, 0), 0))) |
| 123 | #define INVARIANT_ERR(cond) \ |
| 124 | ((void) ((cond) || \ |
| 125 | ((__assertion_failed)(__FILE__, __LINE__, assert_invariant, \ |
| 126 | #cond, 1), 0))) |
| 127 | #else |
| 128 | #define INVARIANT(cond) ((void) (cond)) |
| 129 | #define INVARIANT_ERR(cond) ((void) (cond)) |
| 130 | #endif /* CHECK_INVARIANT */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 131 | #endif /* ASSERTIONS_H */ |
Elliott Hughes | ab44f52 | 2012-10-23 16:05:09 -0700 | [diff] [blame] | 132 | /*! \file */ |