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