1// Copyright 2010 The Kyua Authors. 2// All rights reserved. 3// 4// Redistribution and use in source and binary forms, with or without 5// modification, are permitted provided that the following conditions are 6// met: 7// 8// * Redistributions of source code must retain the above copyright 9// notice, this list of conditions and the following disclaimer. 10// * Redistributions in binary form must reproduce the above copyright 11// notice, this list of conditions and the following disclaimer in the 12// documentation and/or other materials provided with the distribution. 13// * Neither the name of Google Inc. nor the names of its contributors 14// may be used to endorse or promote products derived from this software 15// without specific prior written permission. 16// 17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29/// \file utils/sanity.hpp 30/// 31/// Set of macros that replace the standard assert macro with more semantical 32/// expressivity and meaningful diagnostics. Code should never use assert 33/// directly. 34/// 35/// In general, the checks performed by the macros in this code are only 36/// executed if the code is built with debugging support (that is, if the NDEBUG 37/// macro is NOT defined). 38 39#if !defined(UTILS_SANITY_HPP) 40#define UTILS_SANITY_HPP 41 42#include "utils/sanity_fwd.hpp" 43 44#include <cstddef> 45#include <string> 46 47#include "utils/defs.hpp" 48 49namespace utils { 50 51 52void sanity_failure(const assert_type, const char*, const size_t, 53 const std::string&) UTILS_NORETURN; 54 55 56void install_crash_handlers(const std::string&); 57 58 59} // namespace utils 60 61 62/// \def _UTILS_ASSERT(type, expr, message) 63/// \brief Performs an assertion check. 64/// 65/// This macro is internal and should not be used directly. 66/// 67/// Ensures that the given expression expr is true and, if not, terminates 68/// execution by calling utils::sanity_failure(). The check is only performed 69/// in debug builds. 70/// 71/// \param type The assertion type as defined by assert_type. 72/// \param expr A boolean expression. 73/// \param message A string describing the nature of the error. 74#if !defined(NDEBUG) 75# define _UTILS_ASSERT(type, expr, message) \ 76 do { \ 77 if (!(expr)) \ 78 utils::sanity_failure(type, __FILE__, __LINE__, message); \ 79 } while (0) 80#else // defined(NDEBUG) 81# define _UTILS_ASSERT(type, expr, message) do {} while (0) 82#endif // !defined(NDEBUG) 83 84 85/// Ensures that an invariant holds. 86/// 87/// If the invariant does not hold, execution is immediately terminated. The 88/// check is only performed in debug builds. 89/// 90/// The error message printed by this macro is a textual representation of the 91/// boolean condition. If you want to provide a custom error message, use 92/// INV_MSG instead. 93/// 94/// \param expr A boolean expression describing the invariant. 95#define INV(expr) _UTILS_ASSERT(utils::invariant, expr, #expr) 96 97 98/// Ensures that an invariant holds using a custom error message. 99/// 100/// If the invariant does not hold, execution is immediately terminated. The 101/// check is only performed in debug builds. 102/// 103/// \param expr A boolean expression describing the invariant. 104/// \param msg The error message to print if the condition is false. 105#define INV_MSG(expr, msg) _UTILS_ASSERT(utils::invariant, expr, msg) 106 107 108/// Ensures that a precondition holds. 109/// 110/// If the precondition does not hold, execution is immediately terminated. The 111/// check is only performed in debug builds. 112/// 113/// The error message printed by this macro is a textual representation of the 114/// boolean condition. If you want to provide a custom error message, use 115/// PRE_MSG instead. 116/// 117/// \param expr A boolean expression describing the precondition. 118#define PRE(expr) _UTILS_ASSERT(utils::precondition, expr, #expr) 119 120 121/// Ensures that a precondition holds using a custom error message. 122/// 123/// If the precondition does not hold, execution is immediately terminated. The 124/// check is only performed in debug builds. 125/// 126/// \param expr A boolean expression describing the precondition. 127/// \param msg The error message to print if the condition is false. 128#define PRE_MSG(expr, msg) _UTILS_ASSERT(utils::precondition, expr, msg) 129 130 131/// Ensures that an postcondition holds. 132/// 133/// If the postcondition does not hold, execution is immediately terminated. 134/// The check is only performed in debug builds. 135/// 136/// The error message printed by this macro is a textual representation of the 137/// boolean condition. If you want to provide a custom error message, use 138/// POST_MSG instead. 139/// 140/// \param expr A boolean expression describing the postcondition. 141#define POST(expr) _UTILS_ASSERT(utils::postcondition, expr, #expr) 142 143 144/// Ensures that a postcondition holds using a custom error message. 145/// 146/// If the postcondition does not hold, execution is immediately terminated. 147/// The check is only performed in debug builds. 148/// 149/// \param expr A boolean expression describing the postcondition. 150/// \param msg The error message to print if the condition is false. 151#define POST_MSG(expr, msg) _UTILS_ASSERT(utils::postcondition, expr, msg) 152 153 154/// Ensures that a code path is not reached. 155/// 156/// If the code path in which this macro is located is reached, execution is 157/// immediately terminated. Given that such a condition is critical for the 158/// execution of the program (and to prevent build failures due to some code 159/// paths not initializing variables, for example), this condition is fatal both 160/// in debug and production builds. 161/// 162/// The error message printed by this macro is a textual representation of the 163/// boolean condition. If you want to provide a custom error message, use 164/// POST_MSG instead. 165#define UNREACHABLE UNREACHABLE_MSG("") 166 167 168/// Ensures that a code path is not reached using a custom error message. 169/// 170/// If the code path in which this macro is located is reached, execution is 171/// immediately terminated. Given that such a condition is critical for the 172/// execution of the program (and to prevent build failures due to some code 173/// paths not initializing variables, for example), this condition is fatal both 174/// in debug and production builds. 175/// 176/// \param msg The error message to print if the condition is false. 177#define UNREACHABLE_MSG(msg) \ 178 do { \ 179 utils::sanity_failure(utils::unreachable, __FILE__, __LINE__, msg); \ 180 } while (0) 181 182 183#endif // !defined(UTILS_SANITY_HPP) 184