henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_CHECKS_H_ |
| 12 | #define RTC_BASE_CHECKS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | // If you for some reson need to know if DCHECKs are on, test the value of |
| 15 | // RTC_DCHECK_IS_ON. (Test its value, not if it's defined; it'll always be |
| 16 | // defined, to either a true or a false value.) |
| 17 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 18 | #define RTC_DCHECK_IS_ON 1 |
| 19 | #else |
| 20 | #define RTC_DCHECK_IS_ON 0 |
| 21 | #endif |
| 22 | |
Fredrik Solenberg | 500e75b | 2018-05-23 11:49:01 +0200 | [diff] [blame] | 23 | // Annotate a function that will not return control flow to the caller. |
| 24 | #if defined(_MSC_VER) |
| 25 | #define RTC_NORETURN __declspec(noreturn) |
| 26 | #elif defined(__GNUC__) |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 27 | #define RTC_NORETURN __attribute__((__noreturn__)) |
Fredrik Solenberg | 500e75b | 2018-05-23 11:49:01 +0200 | [diff] [blame] | 28 | #else |
| 29 | #define RTC_NORETURN |
| 30 | #endif |
| 31 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
Fredrik Solenberg | 500e75b | 2018-05-23 11:49:01 +0200 | [diff] [blame] | 35 | RTC_NORETURN void rtc_FatalMessage(const char* file, int line, const char* msg); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 36 | #ifdef __cplusplus |
| 37 | } // extern "C" |
| 38 | #endif |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | // C++ version. |
| 42 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | #include <string> |
| 44 | |
Sebastian Jansson | f4e085a | 2019-05-20 18:34:07 +0200 | [diff] [blame] | 45 | #include "absl/meta/type_traits.h" |
Mirko Bonadei | d4a37a6 | 2019-03-11 10:31:22 +0100 | [diff] [blame] | 46 | #include "absl/strings/string_view.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 47 | #include "rtc_base/numerics/safe_compare.h" |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 48 | #include "rtc_base/system/inline.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 49 | |
| 50 | // The macros here print a message to stderr and abort under various |
| 51 | // conditions. All will accept additional stream messages. For example: |
| 52 | // RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar."; |
| 53 | // |
| 54 | // - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't, |
| 55 | // it's better to terminate the process than to continue. During development, |
| 56 | // the reason that it's better to terminate might simply be that the error |
| 57 | // handling code isn't in place yet; in production, the reason might be that |
| 58 | // the author of the code truly believes that x will always be true, but that |
| 59 | // she recognizes that if she is wrong, abrupt and unpleasant process |
| 60 | // termination is still better than carrying on with the assumption violated. |
| 61 | // |
| 62 | // RTC_CHECK always evaluates its argument, so it's OK for x to have side |
| 63 | // effects. |
| 64 | // |
| 65 | // - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always |
| 66 | // true---except that x will only be evaluated in debug builds; in production |
| 67 | // builds, x is simply assumed to be true. This is useful if evaluating x is |
| 68 | // expensive and the expected cost of failing to detect the violated |
| 69 | // assumption is acceptable. You should not handle cases where a production |
| 70 | // build fails to spot a violated condition, even those that would result in |
| 71 | // crashes. If the code needs to cope with the error, make it cope, but don't |
| 72 | // call RTC_DCHECK; if the condition really can't occur, but you'd sleep |
| 73 | // better at night knowing that the process will suicide instead of carrying |
| 74 | // on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK. |
| 75 | // |
| 76 | // RTC_DCHECK only evaluates its argument in debug builds, so if x has visible |
| 77 | // side effects, you need to write e.g. |
| 78 | // bool w = x; RTC_DCHECK(w); |
| 79 | // |
| 80 | // - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are |
| 81 | // specialized variants of RTC_CHECK and RTC_DCHECK that print prettier |
| 82 | // messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and |
| 83 | // RTC_DCHECK. |
| 84 | // |
| 85 | // - FATAL() aborts unconditionally. |
| 86 | // |
| 87 | // TODO(ajm): Ideally, checks.h would be combined with logging.h, but |
| 88 | // consolidation with system_wrappers/logging.h should happen first. |
| 89 | |
| 90 | namespace rtc { |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 91 | namespace webrtc_checks_impl { |
| 92 | enum class CheckArgType : int8_t { |
| 93 | kEnd = 0, |
| 94 | kInt, |
| 95 | kLong, |
| 96 | kLongLong, |
| 97 | kUInt, |
| 98 | kULong, |
| 99 | kULongLong, |
| 100 | kDouble, |
| 101 | kLongDouble, |
| 102 | kCharP, |
| 103 | kStdString, |
Mirko Bonadei | d4a37a6 | 2019-03-11 10:31:22 +0100 | [diff] [blame] | 104 | kStringView, |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 105 | kVoidP, |
Jonas Olsson | d000b0a | 2018-07-03 12:07:53 +0200 | [diff] [blame] | 106 | |
| 107 | // kCheckOp doesn't represent an argument type. Instead, it is sent as the |
| 108 | // first argument from RTC_CHECK_OP to make FatalLog use the next two |
| 109 | // arguments to build the special CHECK_OP error message |
| 110 | // (the "a == b (1 vs. 2)" bit). |
| 111 | kCheckOp, |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 112 | }; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 113 | |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 114 | RTC_NORETURN void FatalLog(const char* file, |
| 115 | int line, |
| 116 | const char* message, |
| 117 | const CheckArgType* fmt, |
| 118 | ...); |
| 119 | |
| 120 | // Wrapper for log arguments. Only ever make values of this type with the |
| 121 | // MakeVal() functions. |
| 122 | template <CheckArgType N, typename T> |
| 123 | struct Val { |
| 124 | static constexpr CheckArgType Type() { return N; } |
| 125 | T GetVal() const { return val; } |
| 126 | T val; |
| 127 | }; |
| 128 | |
Sebastian Jansson | b113862 | 2019-04-11 16:48:15 +0200 | [diff] [blame] | 129 | // Case for when we need to construct a temp string and then print that. |
| 130 | // (We can't use Val<CheckArgType::kStdString, const std::string*> |
| 131 | // because we need somewhere to store the temp string.) |
| 132 | struct ToStringVal { |
| 133 | static constexpr CheckArgType Type() { return CheckArgType::kStdString; } |
| 134 | const std::string* GetVal() const { return &val; } |
| 135 | std::string val; |
| 136 | }; |
| 137 | |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 138 | inline Val<CheckArgType::kInt, int> MakeVal(int x) { |
| 139 | return {x}; |
| 140 | } |
| 141 | inline Val<CheckArgType::kLong, long> MakeVal(long x) { |
| 142 | return {x}; |
| 143 | } |
| 144 | inline Val<CheckArgType::kLongLong, long long> MakeVal(long long x) { |
| 145 | return {x}; |
| 146 | } |
| 147 | inline Val<CheckArgType::kUInt, unsigned int> MakeVal(unsigned int x) { |
| 148 | return {x}; |
| 149 | } |
| 150 | inline Val<CheckArgType::kULong, unsigned long> MakeVal(unsigned long x) { |
| 151 | return {x}; |
| 152 | } |
| 153 | inline Val<CheckArgType::kULongLong, unsigned long long> MakeVal( |
| 154 | unsigned long long x) { |
| 155 | return {x}; |
| 156 | } |
| 157 | |
| 158 | inline Val<CheckArgType::kDouble, double> MakeVal(double x) { |
| 159 | return {x}; |
| 160 | } |
| 161 | inline Val<CheckArgType::kLongDouble, long double> MakeVal(long double x) { |
| 162 | return {x}; |
| 163 | } |
| 164 | |
| 165 | inline Val<CheckArgType::kCharP, const char*> MakeVal(const char* x) { |
| 166 | return {x}; |
| 167 | } |
| 168 | inline Val<CheckArgType::kStdString, const std::string*> MakeVal( |
| 169 | const std::string& x) { |
| 170 | return {&x}; |
| 171 | } |
Mirko Bonadei | d4a37a6 | 2019-03-11 10:31:22 +0100 | [diff] [blame] | 172 | inline Val<CheckArgType::kStringView, const absl::string_view*> MakeVal( |
| 173 | const absl::string_view& x) { |
| 174 | return {&x}; |
| 175 | } |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 176 | |
| 177 | inline Val<CheckArgType::kVoidP, const void*> MakeVal(const void* x) { |
| 178 | return {x}; |
| 179 | } |
| 180 | |
Amit Hilbuch | 10c5a93 | 2019-03-18 13:09:18 -0700 | [diff] [blame] | 181 | // The enum class types are not implicitly convertible to arithmetic types. |
Sebastian Jansson | f4e085a | 2019-05-20 18:34:07 +0200 | [diff] [blame] | 182 | template <typename T, |
| 183 | absl::enable_if_t<std::is_enum<T>::value && |
| 184 | !std::is_arithmetic<T>::value>* = nullptr> |
| 185 | inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal( |
| 186 | T x) { |
| 187 | return {static_cast<absl::underlying_type_t<T>>(x)}; |
Amit Hilbuch | 10c5a93 | 2019-03-18 13:09:18 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Sebastian Jansson | f4e085a | 2019-05-20 18:34:07 +0200 | [diff] [blame] | 190 | template <typename T, decltype(ToLogString(std::declval<T>()))* = nullptr> |
Sebastian Jansson | b113862 | 2019-04-11 16:48:15 +0200 | [diff] [blame] | 191 | ToStringVal MakeVal(const T& x) { |
| 192 | return {ToLogString(x)}; |
| 193 | } |
| 194 | |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 195 | // Ephemeral type that represents the result of the logging << operator. |
| 196 | template <typename... Ts> |
| 197 | class LogStreamer; |
| 198 | |
| 199 | // Base case: Before the first << argument. |
| 200 | template <> |
| 201 | class LogStreamer<> final { |
| 202 | public: |
Amit Hilbuch | 10c5a93 | 2019-03-18 13:09:18 -0700 | [diff] [blame] | 203 | template <typename U, |
Sebastian Jansson | f4e085a | 2019-05-20 18:34:07 +0200 | [diff] [blame] | 204 | typename V = decltype(MakeVal(std::declval<U>())), |
| 205 | absl::enable_if_t<std::is_arithmetic<U>::value || |
| 206 | std::is_enum<U>::value>* = nullptr> |
| 207 | RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const { |
| 208 | return LogStreamer<V>(MakeVal(arg), this); |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Amit Hilbuch | 10c5a93 | 2019-03-18 13:09:18 -0700 | [diff] [blame] | 211 | template <typename U, |
Sebastian Jansson | f4e085a | 2019-05-20 18:34:07 +0200 | [diff] [blame] | 212 | typename V = decltype(MakeVal(std::declval<U>())), |
| 213 | absl::enable_if_t<!std::is_arithmetic<U>::value && |
| 214 | !std::is_enum<U>::value>* = nullptr> |
| 215 | RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const { |
| 216 | return LogStreamer<V>(MakeVal(arg), this); |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | template <typename... Us> |
| 220 | RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file, |
| 221 | const int line, |
| 222 | const char* message, |
| 223 | const Us&... args) { |
| 224 | static constexpr CheckArgType t[] = {Us::Type()..., CheckArgType::kEnd}; |
| 225 | FatalLog(file, line, message, t, args.GetVal()...); |
| 226 | } |
Jonas Olsson | d000b0a | 2018-07-03 12:07:53 +0200 | [diff] [blame] | 227 | |
| 228 | template <typename... Us> |
| 229 | RTC_NORETURN RTC_FORCE_INLINE static void CallCheckOp(const char* file, |
| 230 | const int line, |
| 231 | const char* message, |
| 232 | const Us&... args) { |
| 233 | static constexpr CheckArgType t[] = {CheckArgType::kCheckOp, Us::Type()..., |
| 234 | CheckArgType::kEnd}; |
| 235 | FatalLog(file, line, message, t, args.GetVal()...); |
| 236 | } |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | // Inductive case: We've already seen at least one << argument. The most recent |
| 240 | // one had type `T`, and the earlier ones had types `Ts`. |
| 241 | template <typename T, typename... Ts> |
| 242 | class LogStreamer<T, Ts...> final { |
| 243 | public: |
| 244 | RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior) |
| 245 | : arg_(arg), prior_(prior) {} |
| 246 | |
Amit Hilbuch | 10c5a93 | 2019-03-18 13:09:18 -0700 | [diff] [blame] | 247 | template <typename U, |
Sebastian Jansson | f4e085a | 2019-05-20 18:34:07 +0200 | [diff] [blame] | 248 | typename V = decltype(MakeVal(std::declval<U>())), |
| 249 | absl::enable_if_t<std::is_arithmetic<U>::value || |
| 250 | std::is_enum<U>::value>* = nullptr> |
| 251 | RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const { |
| 252 | return LogStreamer<V, T, Ts...>(MakeVal(arg), this); |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Amit Hilbuch | 10c5a93 | 2019-03-18 13:09:18 -0700 | [diff] [blame] | 255 | template <typename U, |
Sebastian Jansson | f4e085a | 2019-05-20 18:34:07 +0200 | [diff] [blame] | 256 | typename V = decltype(MakeVal(std::declval<U>())), |
| 257 | absl::enable_if_t<!std::is_arithmetic<U>::value && |
| 258 | !std::is_enum<U>::value>* = nullptr> |
| 259 | RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const { |
| 260 | return LogStreamer<V, T, Ts...>(MakeVal(arg), this); |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | template <typename... Us> |
| 264 | RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file, |
| 265 | const int line, |
| 266 | const char* message, |
| 267 | const Us&... args) const { |
| 268 | prior_->Call(file, line, message, arg_, args...); |
| 269 | } |
| 270 | |
Jonas Olsson | d000b0a | 2018-07-03 12:07:53 +0200 | [diff] [blame] | 271 | template <typename... Us> |
| 272 | RTC_NORETURN RTC_FORCE_INLINE void CallCheckOp(const char* file, |
| 273 | const int line, |
| 274 | const char* message, |
| 275 | const Us&... args) const { |
| 276 | prior_->CallCheckOp(file, line, message, arg_, args...); |
| 277 | } |
| 278 | |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 279 | private: |
| 280 | // The most recent argument. |
| 281 | T arg_; |
| 282 | |
| 283 | // Earlier arguments. |
| 284 | const LogStreamer<Ts...>* prior_; |
| 285 | }; |
| 286 | |
Jonas Olsson | d000b0a | 2018-07-03 12:07:53 +0200 | [diff] [blame] | 287 | template <bool isCheckOp> |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 288 | class FatalLogCall final { |
| 289 | public: |
| 290 | FatalLogCall(const char* file, int line, const char* message) |
| 291 | : file_(file), line_(line), message_(message) {} |
| 292 | |
| 293 | // This can be any binary operator with precedence lower than <<. |
| 294 | template <typename... Ts> |
| 295 | RTC_NORETURN RTC_FORCE_INLINE void operator&( |
| 296 | const LogStreamer<Ts...>& streamer) { |
Jonas Olsson | d000b0a | 2018-07-03 12:07:53 +0200 | [diff] [blame] | 297 | isCheckOp ? streamer.CallCheckOp(file_, line_, message_) |
| 298 | : streamer.Call(file_, line_, message_); |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | private: |
| 302 | const char* file_; |
| 303 | int line_; |
| 304 | const char* message_; |
| 305 | }; |
| 306 | } // namespace webrtc_checks_impl |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 307 | |
| 308 | // The actual stream used isn't important. We reference |ignored| in the code |
| 309 | // but don't evaluate it; this is to avoid "unused variable" warnings (we do so |
| 310 | // in a particularly convoluted way with an extra ?: because that appears to be |
| 311 | // the simplest construct that keeps Visual Studio from complaining about |
| 312 | // condition being unused). |
Jonas Olsson | d000b0a | 2018-07-03 12:07:53 +0200 | [diff] [blame] | 313 | #define RTC_EAT_STREAM_PARAMETERS(ignored) \ |
| 314 | (true ? true : ((void)(ignored), true)) \ |
| 315 | ? static_cast<void>(0) \ |
| 316 | : rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \ |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 317 | rtc::webrtc_checks_impl::LogStreamer<>() |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 318 | |
| 319 | // Call RTC_EAT_STREAM_PARAMETERS with an argument that fails to compile if |
| 320 | // values of the same types as |a| and |b| can't be compared with the given |
| 321 | // operation, and that would evaluate |a| and |b| if evaluated. |
| 322 | #define RTC_EAT_STREAM_PARAMETERS_OP(op, a, b) \ |
| 323 | RTC_EAT_STREAM_PARAMETERS(((void)rtc::Safe##op(a, b))) |
| 324 | |
| 325 | // RTC_CHECK dies with a fatal error if condition is not true. It is *not* |
| 326 | // controlled by NDEBUG or anything else, so the check will be executed |
| 327 | // regardless of compilation mode. |
| 328 | // |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 329 | // We make sure RTC_CHECK et al. always evaluates |condition|, as |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 330 | // doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom. |
Jonas Olsson | d000b0a | 2018-07-03 12:07:53 +0200 | [diff] [blame] | 331 | #define RTC_CHECK(condition) \ |
| 332 | while (!(condition)) \ |
| 333 | rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \ |
| 334 | #condition) & \ |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 335 | rtc::webrtc_checks_impl::LogStreamer<>() |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 336 | |
| 337 | // Helper macro for binary operators. |
| 338 | // Don't use this macro directly in your code, use RTC_CHECK_EQ et al below. |
Jonas Olsson | d000b0a | 2018-07-03 12:07:53 +0200 | [diff] [blame] | 339 | #define RTC_CHECK_OP(name, op, val1, val2) \ |
| 340 | while (!rtc::Safe##name((val1), (val2))) \ |
| 341 | rtc::webrtc_checks_impl::FatalLogCall<true>(__FILE__, __LINE__, \ |
| 342 | #val1 " " #op " " #val2) & \ |
| 343 | rtc::webrtc_checks_impl::LogStreamer<>() << (val1) << (val2) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 344 | |
| 345 | #define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2) |
| 346 | #define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2) |
| 347 | #define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(Le, <=, val1, val2) |
| 348 | #define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(Lt, <, val1, val2) |
| 349 | #define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(Ge, >=, val1, val2) |
| 350 | #define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(Gt, >, val1, val2) |
| 351 | |
| 352 | // The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates |
| 353 | // code in debug builds. It does reference the condition parameter in all cases, |
| 354 | // though, so callers won't risk getting warnings about unused variables. |
| 355 | #if RTC_DCHECK_IS_ON |
| 356 | #define RTC_DCHECK(condition) RTC_CHECK(condition) |
| 357 | #define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2) |
| 358 | #define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2) |
| 359 | #define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2) |
| 360 | #define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2) |
| 361 | #define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2) |
| 362 | #define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2) |
| 363 | #else |
| 364 | #define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition) |
| 365 | #define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Eq, v1, v2) |
| 366 | #define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ne, v1, v2) |
| 367 | #define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Le, v1, v2) |
| 368 | #define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Lt, v1, v2) |
| 369 | #define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ge, v1, v2) |
| 370 | #define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Gt, v1, v2) |
| 371 | #endif |
| 372 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 373 | #define RTC_UNREACHABLE_CODE_HIT false |
| 374 | #define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT) |
| 375 | |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 376 | // TODO(bugs.webrtc.org/8454): Add an RTC_ prefix or rename differently. |
Jonas Olsson | d000b0a | 2018-07-03 12:07:53 +0200 | [diff] [blame] | 377 | #define FATAL() \ |
| 378 | rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \ |
| 379 | "FATAL()") & \ |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 380 | rtc::webrtc_checks_impl::LogStreamer<>() |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 381 | |
| 382 | // Performs the integer division a/b and returns the result. CHECKs that the |
| 383 | // remainder is zero. |
| 384 | template <typename T> |
| 385 | inline T CheckedDivExact(T a, T b) { |
| 386 | RTC_CHECK_EQ(a % b, 0) << a << " is not evenly divisible by " << b; |
| 387 | return a / b; |
| 388 | } |
| 389 | |
| 390 | } // namespace rtc |
| 391 | |
| 392 | #else // __cplusplus not defined |
| 393 | // C version. Lacks many features compared to the C++ version, but usage |
| 394 | // guidelines are the same. |
| 395 | |
| 396 | #define RTC_CHECK(condition) \ |
| 397 | do { \ |
| 398 | if (!(condition)) { \ |
| 399 | rtc_FatalMessage(__FILE__, __LINE__, "CHECK failed: " #condition); \ |
| 400 | } \ |
| 401 | } while (0) |
| 402 | |
| 403 | #define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b)) |
| 404 | #define RTC_CHECK_NE(a, b) RTC_CHECK((a) != (b)) |
| 405 | #define RTC_CHECK_LE(a, b) RTC_CHECK((a) <= (b)) |
| 406 | #define RTC_CHECK_LT(a, b) RTC_CHECK((a) < (b)) |
| 407 | #define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b)) |
| 408 | #define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b)) |
| 409 | |
| 410 | #define RTC_DCHECK(condition) \ |
| 411 | do { \ |
| 412 | if (RTC_DCHECK_IS_ON && !(condition)) { \ |
| 413 | rtc_FatalMessage(__FILE__, __LINE__, "DCHECK failed: " #condition); \ |
| 414 | } \ |
| 415 | } while (0) |
| 416 | |
| 417 | #define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b)) |
| 418 | #define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b)) |
| 419 | #define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b)) |
| 420 | #define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b)) |
| 421 | #define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b)) |
| 422 | #define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b)) |
| 423 | |
| 424 | #endif // __cplusplus |
kwiberg | 2e48646 | 2016-08-23 05:54:25 -0700 | [diff] [blame] | 425 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 426 | #endif // RTC_BASE_CHECKS_H_ |