summaryrefslogtreecommitdiff
path: root/external/bsd/kyua-cli/dist/utils/sanity.hpp
blob: 402c05e32fb8a52135d70393a7cda4b5c68f56af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2010 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/// \file utils/sanity.hpp
///
/// Set of macros that replace the standard assert(3) macro with more semantical
/// expressivity and meaningful diagnostics.  Code should never use assert(3)
/// directly.
///
/// In general, the checks performed by the macros in this code are only
/// executed if the code is built with debugging support (that is, if the NDEBUG
/// macro is NOT defined).

#if !defined(UTILS_SANITY_HPP)
#define UTILS_SANITY_HPP

#include <cstddef>
#include <string>

#include "utils/defs.hpp"

namespace utils {


/// Enumeration to define the assertion type.
///
/// The assertion type is used by the module to format the assertion messages
/// appropriately.
enum assert_type {
    invariant,
    postcondition,
    precondition,
    unreachable,
};


void sanity_failure(const assert_type, const char*, const size_t,
                    const std::string&) UTILS_NORETURN;


void install_crash_handlers(const std::string&);


}  // namespace utils


/// \def _UTILS_ASSERT(type, expr, message)
/// \brief Performs an assertion check.
///
/// This macro is internal and should not be used directly.
///
/// Ensures that the given expression expr is true and, if not, terminates
/// execution by calling utils::sanity_failure().  The check is only performed
/// in debug builds.
///
/// \param type The assertion type as defined by assert_type.
/// \param expr A boolean expression.
/// \param message A string describing the nature of the error.
#if !defined(NDEBUG)
#   define _UTILS_ASSERT(type, expr, message) \
    do { \
        if (!(expr)) \
            utils::sanity_failure(type, __FILE__, __LINE__, message); \
    } while (0)
#else  // defined(NDEBUG)
#   define _UTILS_ASSERT(type, expr, message) do {} while (0)
#endif  // !defined(NDEBUG)


/// Ensures that an invariant holds.
///
/// If the invariant does not hold, execution is immediately terminated.  The
/// check is only performed in debug builds.
///
/// The error message printed by this macro is a textual representation of the
/// boolean condition.  If you want to provide a custom error message, use
/// INV_MSG instead.
///
/// \param expr A boolean expression describing the invariant.
#define INV(expr) _UTILS_ASSERT(utils::invariant, expr, #expr)


/// Ensures that an invariant holds using a custom error message.
///
/// If the invariant does not hold, execution is immediately terminated.  The
/// check is only performed in debug builds.
///
/// \param expr A boolean expression describing the invariant.
/// \param msg The error message to print if the condition is false.
#define INV_MSG(expr, msg) _UTILS_ASSERT(utils::invariant, expr, msg)


/// Ensures that a precondition holds.
///
/// If the precondition does not hold, execution is immediately terminated.  The
/// check is only performed in debug builds.
///
/// The error message printed by this macro is a textual representation of the
/// boolean condition.  If you want to provide a custom error message, use
/// PRE_MSG instead.
///
/// \param expr A boolean expression describing the precondition.
#define PRE(expr) _UTILS_ASSERT(utils::precondition, expr, #expr)


/// Ensures that a precondition holds using a custom error message.
///
/// If the precondition does not hold, execution is immediately terminated.  The
/// check is only performed in debug builds.
///
/// \param expr A boolean expression describing the precondition.
/// \param msg The error message to print if the condition is false.
#define PRE_MSG(expr, msg) _UTILS_ASSERT(utils::precondition, expr, msg)


/// Ensures that an postcondition holds.
///
/// If the postcondition does not hold, execution is immediately terminated.
/// The check is only performed in debug builds.
///
/// The error message printed by this macro is a textual representation of the
/// boolean condition.  If you want to provide a custom error message, use
/// POST_MSG instead.
///
/// \param expr A boolean expression describing the postcondition.
#define POST(expr) _UTILS_ASSERT(utils::postcondition, expr, #expr)


/// Ensures that a postcondition holds using a custom error message.
///
/// If the postcondition does not hold, execution is immediately terminated.
/// The check is only performed in debug builds.
///
/// \param expr A boolean expression describing the postcondition.
/// \param msg The error message to print if the condition is false.
#define POST_MSG(expr, msg) _UTILS_ASSERT(utils::postcondition, expr, msg)


/// Ensures that a code path is not reached.
///
/// If the code path in which this macro is located is reached, execution is
/// immediately terminated.  Given that such a condition is critical for the
/// execution of the program (and to prevent build failures due to some code
/// paths not initializing variables, for example), this condition is fatal both
/// in debug and production builds.
///
/// The error message printed by this macro is a textual representation of the
/// boolean condition.  If you want to provide a custom error message, use
/// POST_MSG instead.
#define UNREACHABLE UNREACHABLE_MSG("")


/// Ensures that a code path is not reached using a custom error message.
///
/// If the code path in which this macro is located is reached, execution is
/// immediately terminated.  Given that such a condition is critical for the
/// execution of the program (and to prevent build failures due to some code
/// paths not initializing variables, for example), this condition is fatal both
/// in debug and production builds.
///
/// \param msg The error message to print if the condition is false.
#define UNREACHABLE_MSG(msg) \
    do { \
        utils::sanity_failure(utils::unreachable, __FILE__, __LINE__, msg); \
    } while (0)


#endif  // !defined(UTILS_SANITY_HPP)