blob: 17d32cb19aa3c51ac52ad2a04deb37f96954b1da [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_CHECKS_H_
12#define RTC_BASE_CHECKS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014// 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 Solenberg500e75b2018-05-23 11:49:01 +020023// 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 Olssona4d87372019-07-05 19:08:33 +020027#define RTC_NORETURN __attribute__((__noreturn__))
Fredrik Solenberg500e75b2018-05-23 11:49:01 +020028#else
29#define RTC_NORETURN
30#endif
31
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032#ifdef __cplusplus
33extern "C" {
34#endif
Fredrik Solenberg500e75b2018-05-23 11:49:01 +020035RTC_NORETURN void rtc_FatalMessage(const char* file, int line, const char* msg);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020036#ifdef __cplusplus
37} // extern "C"
38#endif
39
40#ifdef __cplusplus
41// C++ version.
42
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043#include <string>
44
Sebastian Janssonf4e085a2019-05-20 18:34:07 +020045#include "absl/meta/type_traits.h"
Mirko Bonadeid4a37a62019-03-11 10:31:22 +010046#include "absl/strings/string_view.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010047#include "rtc_base/numerics/safe_compare.h"
Jonas Olssonf8e5c112018-06-14 13:14:22 +020048#include "rtc_base/system/inline.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020049#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050
51// The macros here print a message to stderr and abort under various
52// conditions. All will accept additional stream messages. For example:
53// RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
54//
55// - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't,
56// it's better to terminate the process than to continue. During development,
57// the reason that it's better to terminate might simply be that the error
58// handling code isn't in place yet; in production, the reason might be that
59// the author of the code truly believes that x will always be true, but that
60// she recognizes that if she is wrong, abrupt and unpleasant process
61// termination is still better than carrying on with the assumption violated.
62//
63// RTC_CHECK always evaluates its argument, so it's OK for x to have side
64// effects.
65//
66// - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always
67// true---except that x will only be evaluated in debug builds; in production
68// builds, x is simply assumed to be true. This is useful if evaluating x is
69// expensive and the expected cost of failing to detect the violated
70// assumption is acceptable. You should not handle cases where a production
71// build fails to spot a violated condition, even those that would result in
72// crashes. If the code needs to cope with the error, make it cope, but don't
73// call RTC_DCHECK; if the condition really can't occur, but you'd sleep
74// better at night knowing that the process will suicide instead of carrying
75// on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK.
76//
77// RTC_DCHECK only evaluates its argument in debug builds, so if x has visible
78// side effects, you need to write e.g.
79// bool w = x; RTC_DCHECK(w);
80//
81// - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are
82// specialized variants of RTC_CHECK and RTC_DCHECK that print prettier
83// messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and
84// RTC_DCHECK.
85//
86// - FATAL() aborts unconditionally.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087
88namespace rtc {
Jonas Olssonf8e5c112018-06-14 13:14:22 +020089namespace webrtc_checks_impl {
90enum class CheckArgType : int8_t {
91 kEnd = 0,
92 kInt,
93 kLong,
94 kLongLong,
95 kUInt,
96 kULong,
97 kULongLong,
98 kDouble,
99 kLongDouble,
100 kCharP,
101 kStdString,
Mirko Bonadeid4a37a62019-03-11 10:31:22 +0100102 kStringView,
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200103 kVoidP,
Jonas Olssond000b0a2018-07-03 12:07:53 +0200104
105 // kCheckOp doesn't represent an argument type. Instead, it is sent as the
106 // first argument from RTC_CHECK_OP to make FatalLog use the next two
107 // arguments to build the special CHECK_OP error message
108 // (the "a == b (1 vs. 2)" bit).
109 kCheckOp,
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200110};
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200112RTC_NORETURN RTC_EXPORT void FatalLog(const char* file,
113 int line,
114 const char* message,
115 const CheckArgType* fmt,
116 ...);
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200117
118// Wrapper for log arguments. Only ever make values of this type with the
119// MakeVal() functions.
120template <CheckArgType N, typename T>
121struct Val {
122 static constexpr CheckArgType Type() { return N; }
123 T GetVal() const { return val; }
124 T val;
125};
126
Sebastian Janssonb1138622019-04-11 16:48:15 +0200127// Case for when we need to construct a temp string and then print that.
128// (We can't use Val<CheckArgType::kStdString, const std::string*>
129// because we need somewhere to store the temp string.)
130struct ToStringVal {
131 static constexpr CheckArgType Type() { return CheckArgType::kStdString; }
132 const std::string* GetVal() const { return &val; }
133 std::string val;
134};
135
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200136inline Val<CheckArgType::kInt, int> MakeVal(int x) {
137 return {x};
138}
139inline Val<CheckArgType::kLong, long> MakeVal(long x) {
140 return {x};
141}
142inline Val<CheckArgType::kLongLong, long long> MakeVal(long long x) {
143 return {x};
144}
145inline Val<CheckArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
146 return {x};
147}
148inline Val<CheckArgType::kULong, unsigned long> MakeVal(unsigned long x) {
149 return {x};
150}
151inline Val<CheckArgType::kULongLong, unsigned long long> MakeVal(
152 unsigned long long x) {
153 return {x};
154}
155
156inline Val<CheckArgType::kDouble, double> MakeVal(double x) {
157 return {x};
158}
159inline Val<CheckArgType::kLongDouble, long double> MakeVal(long double x) {
160 return {x};
161}
162
163inline Val<CheckArgType::kCharP, const char*> MakeVal(const char* x) {
164 return {x};
165}
166inline Val<CheckArgType::kStdString, const std::string*> MakeVal(
167 const std::string& x) {
168 return {&x};
169}
Mirko Bonadeid4a37a62019-03-11 10:31:22 +0100170inline Val<CheckArgType::kStringView, const absl::string_view*> MakeVal(
171 const absl::string_view& x) {
172 return {&x};
173}
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200174
175inline Val<CheckArgType::kVoidP, const void*> MakeVal(const void* x) {
176 return {x};
177}
178
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700179// The enum class types are not implicitly convertible to arithmetic types.
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200180template <typename T,
181 absl::enable_if_t<std::is_enum<T>::value &&
182 !std::is_arithmetic<T>::value>* = nullptr>
183inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
184 T x) {
185 return {static_cast<absl::underlying_type_t<T>>(x)};
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700186}
187
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200188template <typename T, decltype(ToLogString(std::declval<T>()))* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200189ToStringVal MakeVal(const T& x) {
190 return {ToLogString(x)};
191}
192
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200193// Ephemeral type that represents the result of the logging << operator.
194template <typename... Ts>
195class LogStreamer;
196
197// Base case: Before the first << argument.
198template <>
199class LogStreamer<> final {
200 public:
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700201 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200202 typename V = decltype(MakeVal(std::declval<U>())),
203 absl::enable_if_t<std::is_arithmetic<U>::value ||
204 std::is_enum<U>::value>* = nullptr>
205 RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
206 return LogStreamer<V>(MakeVal(arg), this);
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200207 }
208
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700209 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200210 typename V = decltype(MakeVal(std::declval<U>())),
211 absl::enable_if_t<!std::is_arithmetic<U>::value &&
212 !std::is_enum<U>::value>* = nullptr>
213 RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
214 return LogStreamer<V>(MakeVal(arg), this);
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200215 }
216
217 template <typename... Us>
218 RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file,
219 const int line,
220 const char* message,
221 const Us&... args) {
222 static constexpr CheckArgType t[] = {Us::Type()..., CheckArgType::kEnd};
223 FatalLog(file, line, message, t, args.GetVal()...);
224 }
Jonas Olssond000b0a2018-07-03 12:07:53 +0200225
226 template <typename... Us>
227 RTC_NORETURN RTC_FORCE_INLINE static void CallCheckOp(const char* file,
228 const int line,
229 const char* message,
230 const Us&... args) {
231 static constexpr CheckArgType t[] = {CheckArgType::kCheckOp, Us::Type()...,
232 CheckArgType::kEnd};
233 FatalLog(file, line, message, t, args.GetVal()...);
234 }
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200235};
236
237// Inductive case: We've already seen at least one << argument. The most recent
238// one had type `T`, and the earlier ones had types `Ts`.
239template <typename T, typename... Ts>
240class LogStreamer<T, Ts...> final {
241 public:
242 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
243 : arg_(arg), prior_(prior) {}
244
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700245 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200246 typename V = decltype(MakeVal(std::declval<U>())),
247 absl::enable_if_t<std::is_arithmetic<U>::value ||
248 std::is_enum<U>::value>* = nullptr>
249 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
250 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200251 }
252
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700253 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200254 typename V = decltype(MakeVal(std::declval<U>())),
255 absl::enable_if_t<!std::is_arithmetic<U>::value &&
256 !std::is_enum<U>::value>* = nullptr>
257 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
258 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200259 }
260
261 template <typename... Us>
262 RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file,
263 const int line,
264 const char* message,
265 const Us&... args) const {
266 prior_->Call(file, line, message, arg_, args...);
267 }
268
Jonas Olssond000b0a2018-07-03 12:07:53 +0200269 template <typename... Us>
270 RTC_NORETURN RTC_FORCE_INLINE void CallCheckOp(const char* file,
271 const int line,
272 const char* message,
273 const Us&... args) const {
274 prior_->CallCheckOp(file, line, message, arg_, args...);
275 }
276
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200277 private:
278 // The most recent argument.
279 T arg_;
280
281 // Earlier arguments.
282 const LogStreamer<Ts...>* prior_;
283};
284
Jonas Olssond000b0a2018-07-03 12:07:53 +0200285template <bool isCheckOp>
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200286class FatalLogCall final {
287 public:
288 FatalLogCall(const char* file, int line, const char* message)
289 : file_(file), line_(line), message_(message) {}
290
291 // This can be any binary operator with precedence lower than <<.
292 template <typename... Ts>
293 RTC_NORETURN RTC_FORCE_INLINE void operator&(
294 const LogStreamer<Ts...>& streamer) {
Jonas Olssond000b0a2018-07-03 12:07:53 +0200295 isCheckOp ? streamer.CallCheckOp(file_, line_, message_)
296 : streamer.Call(file_, line_, message_);
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200297 }
298
299 private:
300 const char* file_;
301 int line_;
302 const char* message_;
303};
304} // namespace webrtc_checks_impl
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200305
306// The actual stream used isn't important. We reference |ignored| in the code
307// but don't evaluate it; this is to avoid "unused variable" warnings (we do so
308// in a particularly convoluted way with an extra ?: because that appears to be
309// the simplest construct that keeps Visual Studio from complaining about
310// condition being unused).
Jonas Olssond000b0a2018-07-03 12:07:53 +0200311#define RTC_EAT_STREAM_PARAMETERS(ignored) \
312 (true ? true : ((void)(ignored), true)) \
313 ? static_cast<void>(0) \
314 : rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200315 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200316
317// Call RTC_EAT_STREAM_PARAMETERS with an argument that fails to compile if
318// values of the same types as |a| and |b| can't be compared with the given
319// operation, and that would evaluate |a| and |b| if evaluated.
320#define RTC_EAT_STREAM_PARAMETERS_OP(op, a, b) \
321 RTC_EAT_STREAM_PARAMETERS(((void)rtc::Safe##op(a, b)))
322
323// RTC_CHECK dies with a fatal error if condition is not true. It is *not*
324// controlled by NDEBUG or anything else, so the check will be executed
325// regardless of compilation mode.
326//
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200327// We make sure RTC_CHECK et al. always evaluates |condition|, as
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200328// doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom.
Jonas Olssond000b0a2018-07-03 12:07:53 +0200329#define RTC_CHECK(condition) \
330 while (!(condition)) \
331 rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \
332 #condition) & \
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200333 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200334
335// Helper macro for binary operators.
336// Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
Jonas Olssond000b0a2018-07-03 12:07:53 +0200337#define RTC_CHECK_OP(name, op, val1, val2) \
338 while (!rtc::Safe##name((val1), (val2))) \
339 rtc::webrtc_checks_impl::FatalLogCall<true>(__FILE__, __LINE__, \
340 #val1 " " #op " " #val2) & \
341 rtc::webrtc_checks_impl::LogStreamer<>() << (val1) << (val2)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200342
343#define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2)
344#define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2)
345#define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(Le, <=, val1, val2)
346#define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(Lt, <, val1, val2)
347#define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(Ge, >=, val1, val2)
348#define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(Gt, >, val1, val2)
349
350// The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates
351// code in debug builds. It does reference the condition parameter in all cases,
352// though, so callers won't risk getting warnings about unused variables.
353#if RTC_DCHECK_IS_ON
354#define RTC_DCHECK(condition) RTC_CHECK(condition)
355#define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2)
356#define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2)
357#define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2)
358#define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2)
359#define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2)
360#define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2)
361#else
362#define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition)
363#define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Eq, v1, v2)
364#define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ne, v1, v2)
365#define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Le, v1, v2)
366#define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Lt, v1, v2)
367#define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ge, v1, v2)
368#define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Gt, v1, v2)
369#endif
370
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200371#define RTC_UNREACHABLE_CODE_HIT false
372#define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT)
373
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200374// TODO(bugs.webrtc.org/8454): Add an RTC_ prefix or rename differently.
Jonas Olssond000b0a2018-07-03 12:07:53 +0200375#define FATAL() \
376 rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \
377 "FATAL()") & \
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200378 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200379
380// Performs the integer division a/b and returns the result. CHECKs that the
381// remainder is zero.
382template <typename T>
383inline T CheckedDivExact(T a, T b) {
384 RTC_CHECK_EQ(a % b, 0) << a << " is not evenly divisible by " << b;
385 return a / b;
386}
387
388} // namespace rtc
389
390#else // __cplusplus not defined
391// C version. Lacks many features compared to the C++ version, but usage
392// guidelines are the same.
393
394#define RTC_CHECK(condition) \
395 do { \
396 if (!(condition)) { \
397 rtc_FatalMessage(__FILE__, __LINE__, "CHECK failed: " #condition); \
398 } \
399 } while (0)
400
401#define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b))
402#define RTC_CHECK_NE(a, b) RTC_CHECK((a) != (b))
403#define RTC_CHECK_LE(a, b) RTC_CHECK((a) <= (b))
404#define RTC_CHECK_LT(a, b) RTC_CHECK((a) < (b))
405#define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b))
406#define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b))
407
408#define RTC_DCHECK(condition) \
409 do { \
410 if (RTC_DCHECK_IS_ON && !(condition)) { \
411 rtc_FatalMessage(__FILE__, __LINE__, "DCHECK failed: " #condition); \
412 } \
413 } while (0)
414
415#define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b))
416#define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b))
417#define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b))
418#define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b))
419#define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b))
420#define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b))
421
422#endif // __cplusplus
kwiberg2e486462016-08-23 05:54:25 -0700423
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200424#endif // RTC_BASE_CHECKS_H_