| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef RTC_BASE_CHECKS_H_ |
| 12 |
#define RTC_BASE_CHECKS_H_ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 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 |
} |
| 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 |
|
| 54 |
|
| 55 |
#include <string> |
| 56 |
|
| 57 |
#include "absl/meta/type_traits.h" |
| 58 |
#include "absl/strings/string_view.h" |
| 59 |
#include "rtc_base/numerics/safe_compare.h" |
| 60 |
#include "rtc_base/system/inline.h" |
| 61 |
#include "rtc_base/system/rtc_export.h" |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
namespace rtc { |
| 101 |
namespace webrtc_checks_impl { |
| 102 |
enum class CheckArgType : int8_t { |
| 103 |
kEnd = 0, |
| 104 |
kInt, |
| 105 |
kLong, |
| 106 |
kLongLong, |
| 107 |
kUInt, |
| 108 |
kULong, |
| 109 |
kULongLong, |
| 110 |
kDouble, |
| 111 |
kLongDouble, |
| 112 |
kCharP, |
| 113 |
kStdString, |
| 114 |
kStringView, |
| 115 |
kVoidP, |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
kCheckOp, |
| 122 |
}; |
| 123 |
|
| 124 |
#if RTC_CHECK_MSG_ENABLED |
| 125 |
RTC_NORETURN RTC_EXPORT void FatalLog(const char* file, |
| 126 |
int line, |
| 127 |
const char* message, |
| 128 |
const CheckArgType* fmt, |
| 129 |
...); |
| 130 |
#else |
| 131 |
RTC_NORETURN RTC_EXPORT void FatalLog(const char* file, int line); |
| 132 |
#endif |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
template <CheckArgType N, typename T> |
| 137 |
struct Val { |
| 138 |
static constexpr CheckArgType Type() { return N; } |
| 139 |
T GetVal() const { return val; } |
| 140 |
T val; |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
struct ToStringVal { |
| 147 |
static constexpr CheckArgType Type() { return CheckArgType::kStdString; } |
| 148 |
const std::string* GetVal() const { return &val; } |
| 149 |
std::string val; |
| 150 |
}; |
| 151 |
|
| 152 |
inline Val<CheckArgType::kInt, int> MakeVal(int x) { |
| 153 |
return {x}; |
| 154 |
} |
| 155 |
inline Val<CheckArgType::kLong, long> MakeVal(long x) { |
| 156 |
return {x}; |
| 157 |
} |
| 158 |
inline Val<CheckArgType::kLongLong, long long> MakeVal(long long x) { |
| 159 |
return {x}; |
| 160 |
} |
| 161 |
inline Val<CheckArgType::kUInt, unsigned int> MakeVal(unsigned int x) { |
| 162 |
return {x}; |
| 163 |
} |
| 164 |
inline Val<CheckArgType::kULong, unsigned long> MakeVal(unsigned long x) { |
| 165 |
return {x}; |
| 166 |
} |
| 167 |
inline Val<CheckArgType::kULongLong, unsigned long long> MakeVal( |
| 168 |
unsigned long long x) { |
| 169 |
return {x}; |
| 170 |
} |
| 171 |
|
| 172 |
inline Val<CheckArgType::kDouble, double> MakeVal(double x) { |
| 173 |
return {x}; |
| 174 |
} |
| 175 |
inline Val<CheckArgType::kLongDouble, long double> MakeVal(long double x) { |
| 176 |
return {x}; |
| 177 |
} |
| 178 |
|
| 179 |
inline Val<CheckArgType::kCharP, const char*> MakeVal(const char* x) { |
| 180 |
return {x}; |
| 181 |
} |
| 182 |
inline Val<CheckArgType::kStdString, const std::string*> MakeVal( |
| 183 |
const std::string& x) { |
| 184 |
return {&x}; |
| 185 |
} |
| 186 |
inline Val<CheckArgType::kStringView, const absl::string_view*> MakeVal( |
| 187 |
const absl::string_view& x) { |
| 188 |
return {&x}; |
| 189 |
} |
| 190 |
|
| 191 |
inline Val<CheckArgType::kVoidP, const void*> MakeVal(const void* x) { |
| 192 |
return {x}; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
template <typename T, |
| 197 |
absl::enable_if_t<std::is_enum<T>::value && |
| 198 |
!std::is_arithmetic<T>::value>* = nullptr> |
| 199 |
inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal( |
| 200 |
T x) { |
| 201 |
return {static_cast<absl::underlying_type_t<T>>(x)}; |
| 202 |
} |
| 203 |
|
| 204 |
template <typename T, decltype(ToLogString(std::declval<T>()))* = nullptr> |
| 205 |
ToStringVal MakeVal(const T& x) { |
| 206 |
return {ToLogString(x)}; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
template <typename... Ts> |
| 211 |
class LogStreamer; |
| 212 |
|
| 213 |
|
| 214 |
template <> |
| 215 |
class LogStreamer<> final { |
| 216 |
public: |
| 217 |
template <typename U, |
| 218 |
typename V = decltype(MakeVal(std::declval<U>())), |
| 219 |
absl::enable_if_t<std::is_arithmetic<U>::value || |
| 220 |
std::is_enum<U>::value>* = nullptr> |
| 221 |
RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const { |
| 222 |
return LogStreamer<V>(MakeVal(arg), this); |
| 223 |
} |
| 224 |
|
| 225 |
template <typename U, |
| 226 |
typename V = decltype(MakeVal(std::declval<U>())), |
| 227 |
absl::enable_if_t<!std::is_arithmetic<U>::value && |
| 228 |
!std::is_enum<U>::value>* = nullptr> |
| 229 |
RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const { |
| 230 |
return LogStreamer<V>(MakeVal(arg), this); |
| 231 |
} |
| 232 |
|
| 233 |
#if RTC_CHECK_MSG_ENABLED |
| 234 |
template <typename... Us> |
| 235 |
RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file, |
| 236 |
const int line, |
| 237 |
const char* message, |
| 238 |
const Us&... args) { |
| 239 |
static constexpr CheckArgType t[] = {Us::Type()..., CheckArgType::kEnd}; |
| 240 |
FatalLog(file, line, message, t, args.GetVal()...); |
| 241 |
} |
| 242 |
|
| 243 |
template <typename... Us> |
| 244 |
RTC_NORETURN RTC_FORCE_INLINE static void CallCheckOp(const char* file, |
| 245 |
const int line, |
| 246 |
const char* message, |
| 247 |
const Us&... args) { |
| 248 |
static constexpr CheckArgType t[] = {CheckArgType::kCheckOp, Us::Type()..., |
| 249 |
CheckArgType::kEnd}; |
| 250 |
FatalLog(file, line, message, t, args.GetVal()...); |
| 251 |
} |
| 252 |
#else |
| 253 |
template <typename... Us> |
| 254 |
RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file, |
| 255 |
const int line) { |
| 256 |
FatalLog(file, line); |
| 257 |
} |
| 258 |
#endif |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
template <typename T, typename... Ts> |
| 264 |
class LogStreamer<T, Ts...> final { |
| 265 |
public: |
| 266 |
RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior) |
| 267 |
: arg_(arg), prior_(prior) {} |
| 268 |
|
| 269 |
template <typename U, |
| 270 |
typename V = decltype(MakeVal(std::declval<U>())), |
| 271 |
absl::enable_if_t<std::is_arithmetic<U>::value || |
| 272 |
std::is_enum<U>::value>* = nullptr> |
| 273 |
RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const { |
| 274 |
return LogStreamer<V, T, Ts...>(MakeVal(arg), this); |
| 275 |
} |
| 276 |
|
| 277 |
template <typename U, |
| 278 |
typename V = decltype(MakeVal(std::declval<U>())), |
| 279 |
absl::enable_if_t<!std::is_arithmetic<U>::value && |
| 280 |
!std::is_enum<U>::value>* = nullptr> |
| 281 |
RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const { |
| 282 |
return LogStreamer<V, T, Ts...>(MakeVal(arg), this); |
| 283 |
} |
| 284 |
|
| 285 |
#if RTC_CHECK_MSG_ENABLED |
| 286 |
template <typename... Us> |
| 287 |
RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file, |
| 288 |
const int line, |
| 289 |
const char* message, |
| 290 |
const Us&... args) const { |
| 291 |
prior_->Call(file, line, message, arg_, args...); |
| 292 |
} |
| 293 |
|
| 294 |
template <typename... Us> |
| 295 |
RTC_NORETURN RTC_FORCE_INLINE void CallCheckOp(const char* file, |
| 296 |
const int line, |
| 297 |
const char* message, |
| 298 |
const Us&... args) const { |
| 299 |
prior_->CallCheckOp(file, line, message, arg_, args...); |
| 300 |
} |
| 301 |
#else |
| 302 |
template <typename... Us> |
| 303 |
RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file, |
| 304 |
const int line) const { |
| 305 |
prior_->Call(file, line); |
| 306 |
} |
| 307 |
#endif |
| 308 |
|
| 309 |
private: |
| 310 |
|
| 311 |
T arg_; |
| 312 |
|
| 313 |
|
| 314 |
const LogStreamer<Ts...>* prior_; |
| 315 |
}; |
| 316 |
|
| 317 |
template <bool isCheckOp> |
| 318 |
class FatalLogCall final { |
| 319 |
public: |
| 320 |
FatalLogCall(const char* file, int line, const char* message) |
| 321 |
: file_(file), line_(line), message_(message) {} |
| 322 |
|
| 323 |
|
| 324 |
template <typename... Ts> |
| 325 |
RTC_NORETURN RTC_FORCE_INLINE void operator&( |
| 326 |
const LogStreamer<Ts...>& streamer) { |
| 327 |
#if RTC_CHECK_MSG_ENABLED |
| 328 |
isCheckOp ? streamer.CallCheckOp(file_, line_, message_) |
| 329 |
: streamer.Call(file_, line_, message_); |
| 330 |
#else |
| 331 |
streamer.Call(file_, line_); |
| 332 |
#endif |
| 333 |
} |
| 334 |
|
| 335 |
private: |
| 336 |
const char* file_; |
| 337 |
int line_; |
| 338 |
const char* message_; |
| 339 |
}; |
| 340 |
|
| 341 |
#if RTC_DCHECK_IS_ON |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
#define RTC_UNREACHABLE_FILE_AND_LINE_CALL_ARGS __FILE__, __LINE__ |
| 346 |
RTC_NORETURN RTC_EXPORT void UnreachableCodeReached(const char* file, int line); |
| 347 |
|
| 348 |
#else |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
#define RTC_UNREACHABLE_FILE_AND_LINE_CALL_ARGS |
| 353 |
RTC_NORETURN RTC_EXPORT void UnreachableCodeReached(); |
| 354 |
|
| 355 |
#endif |
| 356 |
|
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
#define RTC_EAT_STREAM_PARAMETERS(ignored) \ |
| 365 |
(true ? true : ((void)(ignored), true)) \ |
| 366 |
? static_cast<void>(0) \ |
| 367 |
: ::rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \ |
| 368 |
::rtc::webrtc_checks_impl::LogStreamer<>() |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
#define RTC_EAT_STREAM_PARAMETERS_OP(op, a, b) \ |
| 374 |
RTC_EAT_STREAM_PARAMETERS(((void)::rtc::Safe##op(a, b))) |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
#if RTC_CHECK_MSG_ENABLED |
| 386 |
#define RTC_CHECK(condition) \ |
| 387 |
(condition) ? static_cast<void>(0) \ |
| 388 |
: ::rtc::webrtc_checks_impl::FatalLogCall<false>( \ |
| 389 |
__FILE__, __LINE__, #condition) & \ |
| 390 |
::rtc::webrtc_checks_impl::LogStreamer<>() |
| 391 |
|
| 392 |
#define RTC_CHECK_OP(name, op, val1, val2) \ |
| 393 |
::rtc::Safe##name((val1), (val2)) \ |
| 394 |
? static_cast<void>(0) \ |
| 395 |
: ::rtc::webrtc_checks_impl::FatalLogCall<true>( \ |
| 396 |
__FILE__, __LINE__, #val1 " " #op " " #val2) & \ |
| 397 |
::rtc::webrtc_checks_impl::LogStreamer<>() << (val1) << (val2) |
| 398 |
#else |
| 399 |
#define RTC_CHECK(condition) \ |
| 400 |
(condition) \ |
| 401 |
? static_cast<void>(0) \ |
| 402 |
: true ? ::rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, \ |
| 403 |
__LINE__, "") & \ |
| 404 |
::rtc::webrtc_checks_impl::LogStreamer<>() \ |
| 405 |
: ::rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \ |
| 406 |
::rtc::webrtc_checks_impl::LogStreamer<>() |
| 407 |
|
| 408 |
#define RTC_CHECK_OP(name, op, val1, val2) \ |
| 409 |
::rtc::Safe##name((val1), (val2)) \ |
| 410 |
? static_cast<void>(0) \ |
| 411 |
: true ? ::rtc::webrtc_checks_impl::FatalLogCall<true>(__FILE__, \ |
| 412 |
__LINE__, "") & \ |
| 413 |
::rtc::webrtc_checks_impl::LogStreamer<>() \ |
| 414 |
: ::rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \ |
| 415 |
::rtc::webrtc_checks_impl::LogStreamer<>() |
| 416 |
#endif |
| 417 |
|
| 418 |
#define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2) |
| 419 |
#define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2) |
| 420 |
#define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(Le, <=, val1, val2) |
| 421 |
#define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(Lt, <, val1, val2) |
| 422 |
#define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(Ge, >=, val1, val2) |
| 423 |
#define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(Gt, >, val1, val2) |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
#if RTC_DCHECK_IS_ON |
| 429 |
#define RTC_DCHECK(condition) RTC_CHECK(condition) |
| 430 |
#define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2) |
| 431 |
#define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2) |
| 432 |
#define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2) |
| 433 |
#define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2) |
| 434 |
#define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2) |
| 435 |
#define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2) |
| 436 |
#else |
| 437 |
#define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition) |
| 438 |
#define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Eq, v1, v2) |
| 439 |
#define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ne, v1, v2) |
| 440 |
#define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Le, v1, v2) |
| 441 |
#define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Lt, v1, v2) |
| 442 |
#define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ge, v1, v2) |
| 443 |
#define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Gt, v1, v2) |
| 444 |
#endif |
| 445 |
|
| 446 |
#define RTC_UNREACHABLE_CODE_HIT false |
| 447 |
#define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT) |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
#define RTC_CHECK_NOTREACHED() \ |
| 452 |
do { \ |
| 453 |
::rtc::webrtc_checks_impl::UnreachableCodeReached( \ |
| 454 |
RTC_UNREACHABLE_FILE_AND_LINE_CALL_ARGS); \ |
| 455 |
} while (0) |
| 456 |
|
| 457 |
#define RTC_FATAL() \ |
| 458 |
::rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \ |
| 459 |
"FATAL()") & \ |
| 460 |
::rtc::webrtc_checks_impl::LogStreamer<>() |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
template <typename T> |
| 465 |
inline T CheckedDivExact(T a, T b) { |
| 466 |
RTC_CHECK_EQ(a % b, 0) << a << " is not evenly divisible by " << b; |
| 467 |
return a / b; |
| 468 |
} |
| 469 |
|
| 470 |
} |
| 471 |
|
| 472 |
#else |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
#define RTC_CHECK(condition) \ |
| 477 |
do { \ |
| 478 |
if (!(condition)) { \ |
| 479 |
rtc_FatalMessage(__FILE__, __LINE__, \ |
| 480 |
RTC_CHECK_EVAL_MESSAGE("CHECK failed: " #condition)); \ |
| 481 |
} \ |
| 482 |
} while (0) |
| 483 |
|
| 484 |
#define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b)) |
| 485 |
#define RTC_CHECK_NE(a, b) RTC_CHECK((a) != (b)) |
| 486 |
#define RTC_CHECK_LE(a, b) RTC_CHECK((a) <= (b)) |
| 487 |
#define RTC_CHECK_LT(a, b) RTC_CHECK((a) < (b)) |
| 488 |
#define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b)) |
| 489 |
#define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b)) |
| 490 |
|
| 491 |
#define RTC_DCHECK(condition) \ |
| 492 |
do { \ |
| 493 |
if (RTC_DCHECK_IS_ON && !(condition)) { \ |
| 494 |
rtc_FatalMessage(__FILE__, __LINE__, \ |
| 495 |
RTC_CHECK_EVAL_MESSAGE("DCHECK failed: " #condition)); \ |
| 496 |
} \ |
| 497 |
} while (0) |
| 498 |
|
| 499 |
#define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b)) |
| 500 |
#define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b)) |
| 501 |
#define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b)) |
| 502 |
#define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b)) |
| 503 |
#define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b)) |
| 504 |
#define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b)) |
| 505 |
|
| 506 |
#endif |
| 507 |
|
| 508 |
#endif |