henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 11 | #ifndef WEBRTC_BASE_CHECKS_H_ |
| 12 | #define WEBRTC_BASE_CHECKS_H_ |
| 13 | |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 14 | #include <sstream> |
andrew@webrtc.org | a70332f | 2014-09-02 19:00:45 | [diff] [blame] | 15 | #include <string> |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 16 | |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 17 | #include "webrtc/typedefs.h" |
| 18 | |
| 19 | // The macros here print a message to stderr and abort under various |
| 20 | // conditions. All will accept additional stream messages. For example: |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 21 | // RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar."; |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 22 | // |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 23 | // - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't, |
| 24 | // it's better to terminate the process than to continue. During development, |
| 25 | // the reason that it's better to terminate might simply be that the error |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 26 | // handling code isn't in place yet; in production, the reason might be that |
| 27 | // the author of the code truly believes that x will always be true, but that |
| 28 | // she recognizes that if she is wrong, abrupt and unpleasant process |
| 29 | // termination is still better than carrying on with the assumption violated. |
| 30 | // |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 31 | // RTC_CHECK always evaluates its argument, so it's OK for x to have side |
kwiberg@webrtc.org | 19cf944 | 2014-10-08 12:19:56 | [diff] [blame] | 32 | // effects. |
| 33 | // |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 34 | // - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 35 | // true---except that x will only be evaluated in debug builds; in production |
| 36 | // builds, x is simply assumed to be true. This is useful if evaluating x is |
| 37 | // expensive and the expected cost of failing to detect the violated |
| 38 | // assumption is acceptable. You should not handle cases where a production |
| 39 | // build fails to spot a violated condition, even those that would result in |
| 40 | // crashes. If the code needs to cope with the error, make it cope, but don't |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 41 | // call RTC_DCHECK; if the condition really can't occur, but you'd sleep |
| 42 | // better at night knowing that the process will suicide instead of carrying |
| 43 | // on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK. |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 44 | // |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 45 | // RTC_DCHECK only evaluates its argument in debug builds, so if x has visible |
kwiberg@webrtc.org | 19cf944 | 2014-10-08 12:19:56 | [diff] [blame] | 46 | // side effects, you need to write e.g. |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 47 | // bool w = x; RTC_DCHECK(w); |
kwiberg@webrtc.org | 19cf944 | 2014-10-08 12:19:56 | [diff] [blame] | 48 | // |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 49 | // - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are |
| 50 | // specialized variants of RTC_CHECK and RTC_DCHECK that print prettier |
| 51 | // messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and |
| 52 | // RTC_DCHECK. |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 53 | // |
| 54 | // - FATAL() aborts unconditionally. |
henrikg | 6fd7bf5 | 2015-09-17 09:06:11 | [diff] [blame] | 55 | // |
| 56 | // TODO(ajm): Ideally, checks.h would be combined with logging.h, but |
| 57 | // consolidation with system_wrappers/logging.h should happen first. |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 58 | |
henrike@webrtc.org | b2eea5c | 2014-05-14 18:24:13 | [diff] [blame] | 59 | namespace rtc { |
| 60 | |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 61 | // Helper macro which avoids evaluating the arguments to a stream if |
| 62 | // the condition doesn't hold. |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 63 | #define RTC_LAZY_STREAM(stream, condition) \ |
andrew@webrtc.org | a70332f | 2014-09-02 19:00:45 | [diff] [blame] | 64 | !(condition) ? static_cast<void>(0) : rtc::FatalMessageVoidify() & (stream) |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 65 | |
kwiberg@webrtc.org | 074735e | 2014-12-11 08:32:30 | [diff] [blame] | 66 | // The actual stream used isn't important. We reference condition in the code |
| 67 | // but don't evaluate it; this is to avoid "unused variable" warnings (we do so |
| 68 | // in a particularly convoluted way with an extra ?: because that appears to be |
| 69 | // the simplest construct that keeps Visual Studio from complaining about |
| 70 | // condition being unused). |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 71 | #define RTC_EAT_STREAM_PARAMETERS(condition) \ |
| 72 | (true ? true : !(condition)) \ |
| 73 | ? static_cast<void>(0) \ |
kwiberg@webrtc.org | 074735e | 2014-12-11 08:32:30 | [diff] [blame] | 74 | : rtc::FatalMessageVoidify() & rtc::FatalMessage("", 0).stream() |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 75 | |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 76 | // RTC_CHECK dies with a fatal error if condition is not true. It is *not* |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 77 | // controlled by NDEBUG, so the check will be executed regardless of |
| 78 | // compilation mode. |
| 79 | // |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 80 | // We make sure RTC_CHECK et al. always evaluates their arguments, as |
| 81 | // doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom. |
| 82 | #define RTC_CHECK(condition) \ |
| 83 | RTC_LAZY_STREAM(rtc::FatalMessage(__FILE__, __LINE__).stream(), \ |
| 84 | !(condition)) \ |
| 85 | << "Check failed: " #condition << std::endl << "# " |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 86 | |
| 87 | // Helper macro for binary operators. |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 88 | // Don't use this macro directly in your code, use RTC_CHECK_EQ et al below. |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 89 | // |
| 90 | // TODO(akalin): Rewrite this so that constructs like if (...) |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 91 | // RTC_CHECK_EQ(...) else { ... } work properly. |
| 92 | #define RTC_CHECK_OP(name, op, val1, val2) \ |
| 93 | if (std::string* _result = \ |
| 94 | rtc::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \ |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 95 | rtc::FatalMessage(__FILE__, __LINE__, _result).stream() |
| 96 | |
| 97 | // Build the error message string. This is separate from the "Impl" |
| 98 | // function template because it is not performance critical and so can |
| 99 | // be out of line, while the "Impl" code should be inline. Caller |
| 100 | // takes ownership of the returned string. |
| 101 | template<class t1, class t2> |
| 102 | std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { |
| 103 | std::ostringstream ss; |
| 104 | ss << names << " (" << v1 << " vs. " << v2 << ")"; |
| 105 | std::string* msg = new std::string(ss.str()); |
| 106 | return msg; |
| 107 | } |
| 108 | |
| 109 | // MSVC doesn't like complex extern templates and DLLs. |
| 110 | #if !defined(COMPILER_MSVC) |
| 111 | // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated |
| 112 | // in logging.cc. |
| 113 | extern template std::string* MakeCheckOpString<int, int>( |
| 114 | const int&, const int&, const char* names); |
| 115 | extern template |
| 116 | std::string* MakeCheckOpString<unsigned long, unsigned long>( |
| 117 | const unsigned long&, const unsigned long&, const char* names); |
| 118 | extern template |
| 119 | std::string* MakeCheckOpString<unsigned long, unsigned int>( |
| 120 | const unsigned long&, const unsigned int&, const char* names); |
| 121 | extern template |
| 122 | std::string* MakeCheckOpString<unsigned int, unsigned long>( |
| 123 | const unsigned int&, const unsigned long&, const char* names); |
| 124 | extern template |
| 125 | std::string* MakeCheckOpString<std::string, std::string>( |
| 126 | const std::string&, const std::string&, const char* name); |
| 127 | #endif |
| 128 | |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 129 | // Helper functions for RTC_CHECK_OP macro. |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 130 | // The (int, int) specialization works around the issue that the compiler |
| 131 | // will not instantiate the template version of the function on values of |
| 132 | // unnamed enum type - see comment below. |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 133 | #define DEFINE_RTC_CHECK_OP_IMPL(name, op) \ |
| 134 | template <class t1, class t2> \ |
| 135 | inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
| 136 | const char* names) { \ |
| 137 | if (v1 op v2) \ |
| 138 | return NULL; \ |
| 139 | else \ |
| 140 | return rtc::MakeCheckOpString(v1, v2, names); \ |
| 141 | } \ |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 142 | inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 143 | if (v1 op v2) \ |
| 144 | return NULL; \ |
| 145 | else \ |
| 146 | return rtc::MakeCheckOpString(v1, v2, names); \ |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 147 | } |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 148 | DEFINE_RTC_CHECK_OP_IMPL(EQ, ==) |
| 149 | DEFINE_RTC_CHECK_OP_IMPL(NE, !=) |
| 150 | DEFINE_RTC_CHECK_OP_IMPL(LE, <=) |
| 151 | DEFINE_RTC_CHECK_OP_IMPL(LT, < ) |
| 152 | DEFINE_RTC_CHECK_OP_IMPL(GE, >=) |
| 153 | DEFINE_RTC_CHECK_OP_IMPL(GT, > ) |
| 154 | #undef DEFINE_RTC_CHECK_OP_IMPL |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 155 | |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 156 | #define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(EQ, ==, val1, val2) |
| 157 | #define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(NE, !=, val1, val2) |
| 158 | #define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(LE, <=, val1, val2) |
| 159 | #define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(LT, < , val1, val2) |
| 160 | #define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(GE, >=, val1, val2) |
| 161 | #define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(GT, > , val1, val2) |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 162 | |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 163 | // The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates |
| 164 | // code in debug builds. It does reference the condition parameter in all cases, |
kwiberg@webrtc.org | 074735e | 2014-12-11 08:32:30 | [diff] [blame] | 165 | // though, so callers won't risk getting warnings about unused variables. |
henrik.lundin@webrtc.org | 5fa3962 | 2014-09-15 11:19:35 | [diff] [blame] | 166 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
Karl Wiberg | 882e221 | 2015-10-26 18:51:29 | [diff] [blame] | 167 | #define RTC_DCHECK_IS_ON 1 |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 168 | #define RTC_DCHECK(condition) RTC_CHECK(condition) |
| 169 | #define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2) |
| 170 | #define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2) |
| 171 | #define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2) |
| 172 | #define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2) |
| 173 | #define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2) |
| 174 | #define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2) |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 175 | #else |
Karl Wiberg | 882e221 | 2015-10-26 18:51:29 | [diff] [blame] | 176 | #define RTC_DCHECK_IS_ON 0 |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 177 | #define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition) |
| 178 | #define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) == (v2)) |
| 179 | #define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) != (v2)) |
| 180 | #define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) <= (v2)) |
| 181 | #define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) < (v2)) |
| 182 | #define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) >= (v2)) |
| 183 | #define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) > (v2)) |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 184 | #endif |
| 185 | |
andrew@webrtc.org | a70332f | 2014-09-02 19:00:45 | [diff] [blame] | 186 | // This is identical to LogMessageVoidify but in name. |
| 187 | class FatalMessageVoidify { |
| 188 | public: |
| 189 | FatalMessageVoidify() { } |
| 190 | // This has to be an operator with a precedence lower than << but |
| 191 | // higher than ?: |
| 192 | void operator&(std::ostream&) { } |
| 193 | }; |
| 194 | |
magjed@webrtc.org | 5f8ced0 | 2015-03-04 11:25:46 | [diff] [blame] | 195 | #define RTC_UNREACHABLE_CODE_HIT false |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 196 | #define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT) |
magjed@webrtc.org | 5f8ced0 | 2015-03-04 11:25:46 | [diff] [blame] | 197 | |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 198 | #define FATAL() rtc::FatalMessage(__FILE__, __LINE__).stream() |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 199 | // TODO(ajm): Consider adding RTC_NOTIMPLEMENTED macro when |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 200 | // base/logging.h and system_wrappers/logging.h are consolidated such that we |
| 201 | // can match the Chromium behavior. |
| 202 | |
| 203 | // Like a stripped-down LogMessage from logging.h, except that it aborts. |
| 204 | class FatalMessage { |
| 205 | public: |
| 206 | FatalMessage(const char* file, int line); |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 207 | // Used for RTC_CHECK_EQ(), etc. Takes ownership of the given string. |
andrew@webrtc.org | 54ade8b | 2014-08-28 16:28:26 | [diff] [blame] | 208 | FatalMessage(const char* file, int line, std::string* result); |
| 209 | NO_RETURN ~FatalMessage(); |
| 210 | |
| 211 | std::ostream& stream() { return stream_; } |
| 212 | |
| 213 | private: |
| 214 | void Init(const char* file, int line); |
| 215 | |
| 216 | std::ostringstream stream_; |
| 217 | }; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 218 | |
henrik.lundin@webrtc.org | 49fcf04 | 2015-01-26 11:08:53 | [diff] [blame] | 219 | // Performs the integer division a/b and returns the result. CHECKs that the |
| 220 | // remainder is zero. |
| 221 | template <typename T> |
| 222 | inline T CheckedDivExact(T a, T b) { |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 223 | RTC_CHECK_EQ(a % b, static_cast<T>(0)); |
henrik.lundin@webrtc.org | 49fcf04 | 2015-01-26 11:08:53 | [diff] [blame] | 224 | return a / b; |
| 225 | } |
| 226 | |
henrike@webrtc.org | b2eea5c | 2014-05-14 18:24:13 | [diff] [blame] | 227 | } // namespace rtc |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 228 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 229 | #endif // WEBRTC_BASE_CHECKS_H_ |