| 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 |
| 11 | #ifndef RTC_BASE_CHECKS_H_ |
| 12 | #define RTC_BASE_CHECKS_H_ |
| 13 |
| 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 |
| 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__) |
| 27 | #define RTC_NORETURN __attribute__((__noreturn__)) |
| 28 | #else |
| 29 | #define RTC_NORETURN |
| 30 | #endif |
| 31 |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | RTC_NORETURN void rtc_FatalMessage(const char* file, int line, const char* msg); |
| 36 | #ifdef __cplusplus |
| 37 | } // extern "C" |
| 38 | #endif |
| 39 |
| 40 | #ifdef RTC_DISABLE_CHECK_MSG |
| 41 | #define RTC_CHECK_MSG_ENABLED 0 |
| 42 | #else |
| 43 | #define RTC_CHECK_MSG_ENABLED 1 |
| 44 | #endif |
| 45 |
| 46 | #if RTC_CHECK_MSG_ENABLED |
| 47 | #define RTC_CHECK_EVAL_MESSAGE(message) message |
| 48 | #else |
| 49 | #define RTC_CHECK_EVAL_MESSAGE(message) "" |
| 50 | #endif |
| 51 |
| 52 | #ifdef __cplusplus |
| 53 | // C++ version. |
| 54 |
| 55 | #include <string> |
| 56 |
| 57 | #include "absl/meta/type_traits.h" |
| 58 | #include "absl/strings/string_view.h" |
| 59 | #include "api/scoped_refptr.h" |
| 60 | #include "rtc_base/numerics/safe_compare.h" |
| 61 | #include "rtc_base/system/inline.h" |
| 62 | #include "rtc_base/system/rtc_export.h" |
| 63 |
| 64 | // The macros here print a message to stderr and abort under various |
| 65 | // conditions. All will accept additional stream messages. For example: |
| 66 | // RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar."; |
| 67 | // |
| 68 | // - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't, |
| 69 | // it's better to terminate the process than to continue. During development, |
| 70 | // the reason that it's better to terminate might simply be that the error |
| 71 | // handling code isn't in place yet; in production, the reason might be that |
| 72 | // the author of the code truly believes that x will always be true, but that |
| 73 | // they recognizes that if they are wrong, abrupt and unpleasant process |
| 74 | // termination is still better than carrying on with the assumption violated. |
| 75 | // |
| 76 | // RTC_CHECK always evaluates its argument, so it's OK for x to have side |
| 77 | // effects. |
| 78 | // |
| 79 | // - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always |
| 80 | // true---except that x will only be evaluated in debug builds; in production |
| 81 | // builds, x is simply assumed to be true. This is useful if evaluating x is |
| 82 | // expensive and the expected cost of failing to detect the violated |
| 83 | // assumption is acceptable. You should not handle cases where a production |
| 84 | // build fails to spot a violated condition, even those that would result in |
| 85 | // crashes. If the code needs to cope with the error, make it cope, but don't |
| 86 | // call RTC_DCHECK; if the condition really can't occur, but you'd sleep |
| 87 | // better at night knowing that the process will suicide instead of carrying |
| 88 | // on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK. |
| 89 | // |
| 90 | // RTC_DCHECK only evaluates its argument in debug builds, so if x has visible |
| 91 | // side effects, you need to write e.g. |
| 92 | // bool w = x; RTC_DCHECK(w); |
| 93 | // |
| 94 | // - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are |
| 95 | // specialized variants of RTC_CHECK and RTC_DCHECK that print prettier |
| 96 | // messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and |
| 97 | // RTC_DCHECK. |
| 98 | // |
| 99 | // - RTC_FATAL() aborts unconditionally. |
| 100 |
| 101 | namespace rtc { |
| 102 | namespace webrtc_checks_impl { |
| 103 | enum class CheckArgType : int8_t { |
| 104 | kEnd = 0, |
| 105 | kInt, |
| 106 | kLong, |
| 107 | kLongLong, |
| 108 | kUInt, |
| 109 | kULong, |
| 110 | kULongLong, |
| 111 | kDouble, |
| 112 | kLongDouble, |
| 113 | kCharP, |
| 114 | kStdString, |
| 115 | kStringView, |
| 116 | kVoidP, |
| 117 |
| 118 | // kCheckOp doesn't represent an argument type. Instead, it is sent as the |
| 119 | // first argument from RTC_CHECK_OP to make FatalLog use the next two |
| 120 | // arguments to build the special CHECK_OP error message |
| 121 | // (the "a == b (1 vs. 2)" bit). |
| 122 | kCheckOp, |
| 123 | }; |
| 124 |
| 125 | // These two functions are public so they can be overridden from |
| 126 | // webrtc_overrides in chromium. |
| 127 | RTC_NORETURN void WriteFatalLog(const char* file, |
| 128 | int line, |
| 129 | absl::string_view output); |
| 130 | RTC_NORETURN void WriteFatalLog(absl::string_view output); |
| 131 |
| 132 | #if RTC_CHECK_MSG_ENABLED |
| 133 | RTC_NORETURN RTC_EXPORT void FatalLog(const char* file, |
| 134 | int line, |
| 135 | const char* message, |
| 136 | const CheckArgType* fmt, |
| 137 | ...); |
| 138 | #else |
| 139 | RTC_NORETURN RTC_EXPORT void FatalLog(const char* file, int line); |
| 140 | #endif |
| 141 |
| 142 | // Wrapper for log arguments. Only ever make values of this type with the |
| 143 | // MakeVal() functions. |
| 144 | template <CheckArgType N, typename T> |
| 145 | struct Val { |
| 146 | static constexpr CheckArgType Type() { return N; } |
| 147 | T GetVal() const { return val; } |
| 148 | T val; |
| 149 | }; |
| 150 |
| 151 | // Case for when we need to construct a temp string and then print that. |
| 152 | // (We can't use Val<CheckArgType::kStdString, const std::string*> |
| 153 | // because we need somewhere to store the temp string.) |
| 154 | struct ToStringVal { |
| 155 | static constexpr CheckArgType Type() { return CheckArgType::kStdString; } |
| 156 | const std::string* GetVal() const { return &val; } |
| 157 | std::string val; |
| 158 | }; |
| 159 |
| 160 | inline Val<CheckArgType::kInt, int> MakeVal(int x) { |
| 161 | return {x}; |
| 162 | } |
| 163 | inline Val<CheckArgType::kLong, long> MakeVal(long x) { |
| 164 | return {x}; |
| 165 | } |
| 166 | inline Val<CheckArgType::kLongLong, long long> MakeVal(long long x) { |
| 167 | return {x}; |
| 168 | } |
| 169 | inline Val<CheckArgType::kUInt, unsigned int> MakeVal(unsigned int x) { |
| 170 | return {x}; |
| 171 | } |
| 172 | inline Val<CheckArgType::kULong, unsigned long> MakeVal(unsigned long x) { |
| 173 | return {x}; |
| 174 | } |
| 175 | inline Val<CheckArgType::kULongLong, unsigned long long> MakeVal( |
| 176 | unsigned long long x) { |
| 177 | return {x}; |
| 178 | } |
| 179 |
| 180 | inline Val<CheckArgType::kDouble, double> MakeVal(double x) { |
| 181 | return {x}; |
| 182 | } |
| 183 | inline Val<CheckArgType::kLongDouble, long double> MakeVal(long double x) { |
| 184 | return {x}; |
| 185 | } |
| 186 |
| 187 | inline Val<CheckArgType::kCharP, const char*> MakeVal(const char* x) { |
| 188 | return {x}; |
| 189 | } |
| 190 | inline Val<CheckArgType::kStdString, const std::string*> MakeVal( |
| 191 | const std::string& x) { |
| 192 | return {&x}; |
| 193 | } |
| 194 | inline Val<CheckArgType::kStringView, const absl::string_view*> MakeVal( |
| 195 | const absl::string_view& x) { |
| 196 | return {&x}; |
| 197 | } |
| 198 |
| 199 | inline Val<CheckArgType::kVoidP, const void*> MakeVal(const void* x) { |
| 200 | return {x}; |
| 201 | } |
| 202 |
| 203 | template <typename T> |
| 204 | inline Val<CheckArgType::kVoidP, const void*> MakeVal( |
| 205 | const rtc::scoped_refptr<T>& p) { |
| 206 | return {p.get()}; |
| 207 | } |
| 208 |
| 209 | // The enum class types are not implicitly convertible to arithmetic types. |
| 210 | template <typename T, |
| 211 | absl::enable_if_t<std::is_enum<T>::value && |
| 212 | !std::is_arithmetic<T>::value>* = nullptr> |
| 213 | inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal( |
| 214 | T x) { |
| 215 | return {static_cast<absl::underlying_type_t<T>>(x)}; |
| 216 | } |
| 217 |
| 218 | template <typename T, decltype(ToLogString(std::declval<T>()))* = nullptr> |
| 219 | ToStringVal MakeVal(const T& x) { |
| 220 | return {ToLogString(x)}; |
| 221 | } |
| 222 |
| 223 | // Ephemeral type that represents the result of the logging << operator. |
| 224 | template <typename... Ts> |
| 225 | class LogStreamer; |
| 226 |
| 227 | // Base case: Before the first << argument. |
| 228 | template <> |
| 229 | class LogStreamer<> final { |
| 230 | public: |
| 231 | template <typename U, |
| 232 | typename V = decltype(MakeVal(std::declval<U>())), |
| 233 | absl::enable_if_t<std::is_arithmetic<U>::value || |
| 234 | std::is_enum<U>::value>* = nullptr> |
| 235 | RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const { |
| 236 | return LogStreamer<V>(MakeVal(arg), this); |
| 237 | } |
| 238 |
| 239 | template <typename U, |
| 240 | typename V = decltype(MakeVal(std::declval<U>())), |
| 241 | absl::enable_if_t<!std::is_arithmetic<U>::value && |
| 242 | !std::is_enum<U>::value>* = nullptr> |
| 243 | RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const { |
| 244 | return LogStreamer<V>(MakeVal(arg), this); |
| 245 | } |
| 246 |
| 247 | #if RTC_CHECK_MSG_ENABLED |
| 248 | template <typename... Us> |
| 249 | RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file, |
| 250 | const int line, |
| 251 | const char* message, |
| 252 | const Us&... args) { |
| 253 | static constexpr CheckArgType t[] = {Us::Type()..., CheckArgType::kEnd}; |
| 254 | FatalLog(file, line, message, t, args.GetVal()...); |
| 255 | } |
| 256 |
| 257 | template <typename... Us> |
| 258 | RTC_NORETURN RTC_FORCE_INLINE static void CallCheckOp(const char* file, |
| 259 | const int line, |
| 260 | const char* message, |
| 261 | const Us&... args) { |
| 262 | static constexpr CheckArgType t[] = {CheckArgType::kCheckOp, Us::Type()..., |
| 263 | CheckArgType::kEnd}; |
| 264 | FatalLog(file, line, message, t, args.GetVal()...); |
| 265 | } |
| 266 | #else |
| 267 | template <typename... Us> |
| 268 | RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file, |
| 269 | const int line) { |
| 270 | FatalLog(file, line); |
| 271 | } |
| 272 | #endif |
| 273 | }; |
| 274 |
| 275 | // Inductive case: We've already seen at least one << argument. The most recent |
| 276 | // one had type `T`, and the earlier ones had types `Ts`. |
| 277 | template <typename T, typename... Ts> |
| 278 | class LogStreamer<T, Ts...> final { |
| 279 | public: |
| 280 | RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior) |
| 281 | : arg_(arg), prior_(prior) {} |
| 282 |
| 283 | template <typename U, |
| 284 | typename V = decltype(MakeVal(std::declval<U>())), |
| 285 | absl::enable_if_t<std::is_arithmetic<U>::value || |
| 286 | std::is_enum<U>::value>* = nullptr> |
| 287 | RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const { |
| 288 | return LogStreamer<V, T, Ts...>(MakeVal(arg), this); |
| 289 | } |
| 290 |
| 291 | template <typename U, |
| 292 | typename V = decltype(MakeVal(std::declval<U>())), |
| 293 | absl::enable_if_t<!std::is_arithmetic<U>::value && |
| 294 | !std::is_enum<U>::value>* = nullptr> |
| 295 | RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const { |
| 296 | return LogStreamer<V, T, Ts...>(MakeVal(arg), this); |
| 297 | } |
| 298 |
| 299 | #if RTC_CHECK_MSG_ENABLED |
| 300 | template <typename... Us> |
| 301 | RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file, |
| 302 | const int line, |
| 303 | const char* message, |
| 304 | const Us&... args) const { |
| 305 | prior_->Call(file, line, message, arg_, args...); |
| 306 | } |
| 307 |
| 308 | template <typename... Us> |
| 309 | RTC_NORETURN RTC_FORCE_INLINE void CallCheckOp(const char* file, |
| 310 | const int line, |
| 311 | const char* message, |
| 312 | const Us&... args) const { |
| 313 | prior_->CallCheckOp(file, line, message, arg_, args...); |
| 314 | } |
| 315 | #else |
| 316 | template <typename... Us> |
| 317 | RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file, |
| 318 | const int line) const { |
| 319 | prior_->Call(file, line); |
| 320 | } |
| 321 | #endif |
| 322 |
| 323 | private: |
| 324 | // The most recent argument. |
| 325 | T arg_; |
| 326 |
| 327 | // Earlier arguments. |
| 328 | const LogStreamer<Ts...>* prior_; |
| 329 | }; |
| 330 |
| 331 | template <bool isCheckOp> |
| 332 | class FatalLogCall final { |
| 333 | public: |
| 334 | FatalLogCall(const char* file, int line, const char* message) |
| 335 | : file_(file), line_(line), message_(message) {} |
| 336 |
| 337 | // This can be any binary operator with precedence lower than <<. |
| 338 | template <typename... Ts> |
| 339 | RTC_NORETURN RTC_FORCE_INLINE void operator&( |
| 340 | const LogStreamer<Ts...>& streamer) { |
| 341 | #if RTC_CHECK_MSG_ENABLED |
| 342 | isCheckOp ? streamer.CallCheckOp(file_, line_, message_) |
| 343 | : streamer.Call(file_, line_, message_); |
| 344 | #else |
| 345 | streamer.Call(file_, line_); |
| 346 | #endif |
| 347 | } |
| 348 |
| 349 | private: |
| 350 | const char* file_; |
| 351 | int line_; |
| 352 | const char* message_; |
| 353 | }; |
| 354 |
| 355 | #if RTC_DCHECK_IS_ON |
| 356 |
| 357 | // Be helpful, and include file and line in the RTC_CHECK_NOTREACHED error |
| 358 | // message. |
| 359 | #define RTC_UNREACHABLE_FILE_AND_LINE_CALL_ARGS __FILE__, __LINE__ |
| 360 | RTC_NORETURN RTC_EXPORT void UnreachableCodeReached(const char* file, int line); |
| 361 |
| 362 | #else |
| 363 |
| 364 | // Be mindful of binary size, and don't include file and line in the |
| 365 | // RTC_CHECK_NOTREACHED error message. |
| 366 | #define RTC_UNREACHABLE_FILE_AND_LINE_CALL_ARGS |
| 367 | RTC_NORETURN RTC_EXPORT void UnreachableCodeReached(); |
| 368 |
| 369 | #endif |
| 370 |
| 371 | } // namespace webrtc_checks_impl |
| 372 |
| 373 | // The actual stream used isn't important. We reference `ignored` in the code |
| 374 | // but don't evaluate it; this is to avoid "unused variable" warnings (we do so |
| 375 | // in a particularly convoluted way with an extra ?: because that appears to be |
| 376 | // the simplest construct that keeps Visual Studio from complaining about |
| 377 | // condition being unused). |
| 378 | #define RTC_EAT_STREAM_PARAMETERS(ignored) \ |
| 379 | (true ? true : ((void)(ignored), true)) \ |
| 380 | ? static_cast<void>(0) \ |
| 381 | : ::rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \ |
| 382 | ::rtc::webrtc_checks_impl::LogStreamer<>() |
| 383 |
| 384 | // Call RTC_EAT_STREAM_PARAMETERS with an argument that fails to compile if |
| 385 | // values of the same types as `a` and `b` can't be compared with the given |
| 386 | // operation, and that would evaluate `a` and `b` if evaluated. |
| 387 | #define RTC_EAT_STREAM_PARAMETERS_OP(op, a, b) \ |
| 388 | RTC_EAT_STREAM_PARAMETERS(((void)::rtc::Safe##op(a, b))) |
| 389 |
| 390 | // RTC_CHECK dies with a fatal error if condition is not true. It is *not* |
| 391 | // controlled by NDEBUG or anything else, so the check will be executed |
| 392 | // regardless of compilation mode. |
| 393 | // |
| 394 | // We make sure RTC_CHECK et al. always evaluates `condition`, as |
| 395 | // doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom. |
| 396 | // |
| 397 | // RTC_CHECK_OP is a helper macro for binary operators. |
| 398 | // Don't use this macro directly in your code, use RTC_CHECK_EQ et al below. |
| 399 | #if RTC_CHECK_MSG_ENABLED |
| 400 | #define RTC_CHECK(condition) \ |
| 401 | (condition) ? static_cast<void>(0) \ |
| 402 | : ::rtc::webrtc_checks_impl::FatalLogCall<false>( \ |
| 403 | __FILE__, __LINE__, #condition) & \ |
| 404 | ::rtc::webrtc_checks_impl::LogStreamer<>() |
| 405 |
| 406 | #define RTC_CHECK_OP(name, op, val1, val2) \ |
| 407 | ::rtc::Safe##name((val1), (val2)) \ |
| 408 | ? static_cast<void>(0) \ |
| 409 | : ::rtc::webrtc_checks_impl::FatalLogCall<true>( \ |
| 410 | __FILE__, __LINE__, #val1 " " #op " " #val2) & \ |
| 411 | ::rtc::webrtc_checks_impl::LogStreamer<>() << (val1) << (val2) |
| 412 | #else |
| 413 | #define RTC_CHECK(condition) \ |
| 414 | (condition) ? static_cast<void>(0) \ |
| 415 | : true ? ::rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \ |
| 416 | "") & \ |
| 417 | ::rtc::webrtc_checks_impl::LogStreamer<>() \ |
| 418 | : ::rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \ |
| 419 | ::rtc::webrtc_checks_impl::LogStreamer<>() |
| 420 |
| 421 | #define RTC_CHECK_OP(name, op, val1, val2) \ |
| 422 | ::rtc::Safe##name((val1), (val2)) ? static_cast<void>(0) \ |
| 423 | : true ? ::rtc::webrtc_checks_impl::FatalLogCall<true>(__FILE__, __LINE__, \ |
| 424 | "") & \ |
| 425 | ::rtc::webrtc_checks_impl::LogStreamer<>() \ |
| 426 | : ::rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \ |
| 427 | ::rtc::webrtc_checks_impl::LogStreamer<>() |
| 428 | #endif |
| 429 |
| 430 | #define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2) |
| 431 | #define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2) |
| 432 | #define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(Le, <=, val1, val2) |
| 433 | #define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(Lt, <, val1, val2) |
| 434 | #define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(Ge, >=, val1, val2) |
| 435 | #define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(Gt, >, val1, val2) |
| 436 |
| 437 | // The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates |
| 438 | // code in debug builds. It does reference the condition parameter in all cases, |
| 439 | // though, so callers won't risk getting warnings about unused variables. |
| 440 | #if RTC_DCHECK_IS_ON |
| 441 | #define RTC_DCHECK(condition) RTC_CHECK(condition) |
| 442 | #define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2) |
| 443 | #define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2) |
| 444 | #define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2) |
| 445 | #define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2) |
| 446 | #define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2) |
| 447 | #define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2) |
| 448 | #else |
| 449 | #define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition) |
| 450 | #define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Eq, v1, v2) |
| 451 | #define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ne, v1, v2) |
| 452 | #define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Le, v1, v2) |
| 453 | #define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Lt, v1, v2) |
| 454 | #define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ge, v1, v2) |
| 455 | #define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Gt, v1, v2) |
| 456 | #endif |
| 457 |
| 458 | #define RTC_UNREACHABLE_CODE_HIT false |
| 459 | #define RTC_DCHECK_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT) |
| 460 |
| 461 | // Kills the process with an error message. Never returns. Use when you wish to |
| 462 | // assert that a point in the code is never reached. |
| 463 | #define RTC_CHECK_NOTREACHED() \ |
| 464 | do { \ |
| 465 | ::rtc::webrtc_checks_impl::UnreachableCodeReached( \ |
| 466 | RTC_UNREACHABLE_FILE_AND_LINE_CALL_ARGS); \ |
| 467 | } while (0) |
| 468 |
| 469 | #define RTC_FATAL() \ |
| 470 | ::rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \ |
| 471 | "FATAL()") & \ |
| 472 | ::rtc::webrtc_checks_impl::LogStreamer<>() |
| 473 |
| 474 | // Performs the integer division a/b and returns the result. CHECKs that the |
| 475 | // remainder is zero. |
| 476 | template <typename T> |
| 477 | inline T CheckedDivExact(T a, T b) { |
| 478 | RTC_CHECK_EQ(a % b, 0) << a << " is not evenly divisible by " << b; |
| 479 | return a / b; |
| 480 | } |
| 481 |
| 482 | } // namespace rtc |
| 483 |
| 484 | #else // __cplusplus not defined |
| 485 | // C version. Lacks many features compared to the C++ version, but usage |
| 486 | // guidelines are the same. |
| 487 |
| 488 | #define RTC_CHECK(condition) \ |
| 489 | do { \ |
| 490 | if (!(condition)) { \ |
| 491 | rtc_FatalMessage(__FILE__, __LINE__, \ |
| 492 | RTC_CHECK_EVAL_MESSAGE("CHECK failed: " #condition)); \ |
| 493 | } \ |
| 494 | } while (0) |
| 495 |
| 496 | #define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b)) |
| 497 | #define RTC_CHECK_NE(a, b) RTC_CHECK((a) != (b)) |
| 498 | #define RTC_CHECK_LE(a, b) RTC_CHECK((a) <= (b)) |
| 499 | #define RTC_CHECK_LT(a, b) RTC_CHECK((a) < (b)) |
| 500 | #define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b)) |
| 501 | #define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b)) |
| 502 |
| 503 | #define RTC_DCHECK(condition) \ |
| 504 | do { \ |
| 505 | if (RTC_DCHECK_IS_ON && !(condition)) { \ |
| 506 | rtc_FatalMessage(__FILE__, __LINE__, \ |
| 507 | RTC_CHECK_EVAL_MESSAGE("DCHECK failed: " #condition)); \ |
| 508 | } \ |
| 509 | } while (0) |
| 510 |
| 511 | #define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b)) |
| 512 | #define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b)) |
| 513 | #define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b)) |
| 514 | #define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b)) |
| 515 | #define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b)) |
| 516 | #define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b)) |
| 517 |
| 518 | #endif // __cplusplus |
| 519 |
| 520 | #endif // RTC_BASE_CHECKS_H_ |
| 521 |