blob: 627e301b6890984a36d4e84cd8deee69e8a5c242 [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"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049
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
90namespace rtc {
Jonas Olssonf8e5c112018-06-14 13:14:22 +020091namespace webrtc_checks_impl {
92enum 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 Bonadeid4a37a62019-03-11 10:31:22 +0100104 kStringView,
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200105 kVoidP,
Jonas Olssond000b0a2018-07-03 12:07:53 +0200106
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 Olssonf8e5c112018-06-14 13:14:22 +0200112};
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200114RTC_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.
122template <CheckArgType N, typename T>
123struct Val {
124 static constexpr CheckArgType Type() { return N; }
125 T GetVal() const { return val; }
126 T val;
127};
128
Sebastian Janssonb1138622019-04-11 16:48:15 +0200129// 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.)
132struct ToStringVal {
133 static constexpr CheckArgType Type() { return CheckArgType::kStdString; }
134 const std::string* GetVal() const { return &val; }
135 std::string val;
136};
137
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200138inline Val<CheckArgType::kInt, int> MakeVal(int x) {
139 return {x};
140}
141inline Val<CheckArgType::kLong, long> MakeVal(long x) {
142 return {x};
143}
144inline Val<CheckArgType::kLongLong, long long> MakeVal(long long x) {
145 return {x};
146}
147inline Val<CheckArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
148 return {x};
149}
150inline Val<CheckArgType::kULong, unsigned long> MakeVal(unsigned long x) {
151 return {x};
152}
153inline Val<CheckArgType::kULongLong, unsigned long long> MakeVal(
154 unsigned long long x) {
155 return {x};
156}
157
158inline Val<CheckArgType::kDouble, double> MakeVal(double x) {
159 return {x};
160}
161inline Val<CheckArgType::kLongDouble, long double> MakeVal(long double x) {
162 return {x};
163}
164
165inline Val<CheckArgType::kCharP, const char*> MakeVal(const char* x) {
166 return {x};
167}
168inline Val<CheckArgType::kStdString, const std::string*> MakeVal(
169 const std::string& x) {
170 return {&x};
171}
Mirko Bonadeid4a37a62019-03-11 10:31:22 +0100172inline Val<CheckArgType::kStringView, const absl::string_view*> MakeVal(
173 const absl::string_view& x) {
174 return {&x};
175}
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200176
177inline Val<CheckArgType::kVoidP, const void*> MakeVal(const void* x) {
178 return {x};
179}
180
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700181// The enum class types are not implicitly convertible to arithmetic types.
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200182template <typename T,
183 absl::enable_if_t<std::is_enum<T>::value &&
184 !std::is_arithmetic<T>::value>* = nullptr>
185inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
186 T x) {
187 return {static_cast<absl::underlying_type_t<T>>(x)};
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700188}
189
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200190template <typename T, decltype(ToLogString(std::declval<T>()))* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200191ToStringVal MakeVal(const T& x) {
192 return {ToLogString(x)};
193}
194
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200195// Ephemeral type that represents the result of the logging << operator.
196template <typename... Ts>
197class LogStreamer;
198
199// Base case: Before the first << argument.
200template <>
201class LogStreamer<> final {
202 public:
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700203 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200204 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 Olssonf8e5c112018-06-14 13:14:22 +0200209 }
210
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700211 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200212 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 Olssonf8e5c112018-06-14 13:14:22 +0200217 }
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 Olssond000b0a2018-07-03 12:07:53 +0200227
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 Olssonf8e5c112018-06-14 13:14:22 +0200237};
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`.
241template <typename T, typename... Ts>
242class LogStreamer<T, Ts...> final {
243 public:
244 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
245 : arg_(arg), prior_(prior) {}
246
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700247 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200248 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 Olssonf8e5c112018-06-14 13:14:22 +0200253 }
254
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700255 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200256 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 Olssonf8e5c112018-06-14 13:14:22 +0200261 }
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 Olssond000b0a2018-07-03 12:07:53 +0200271 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 Olssonf8e5c112018-06-14 13:14:22 +0200279 private:
280 // The most recent argument.
281 T arg_;
282
283 // Earlier arguments.
284 const LogStreamer<Ts...>* prior_;
285};
286
Jonas Olssond000b0a2018-07-03 12:07:53 +0200287template <bool isCheckOp>
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200288class 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 Olssond000b0a2018-07-03 12:07:53 +0200297 isCheckOp ? streamer.CallCheckOp(file_, line_, message_)
298 : streamer.Call(file_, line_, message_);
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200299 }
300
301 private:
302 const char* file_;
303 int line_;
304 const char* message_;
305};
306} // namespace webrtc_checks_impl
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200307
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 Olssond000b0a2018-07-03 12:07:53 +0200313#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 Olssonf8e5c112018-06-14 13:14:22 +0200317 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200318
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 Olssonf8e5c112018-06-14 13:14:22 +0200329// We make sure RTC_CHECK et al. always evaluates |condition|, as
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200330// doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom.
Jonas Olssond000b0a2018-07-03 12:07:53 +0200331#define RTC_CHECK(condition) \
332 while (!(condition)) \
333 rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \
334 #condition) & \
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200335 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200336
337// Helper macro for binary operators.
338// Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
Jonas Olssond000b0a2018-07-03 12:07:53 +0200339#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 Kjellanderec78f1c2017-06-29 07:52:50 +0200344
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 Kjellanderec78f1c2017-06-29 07:52:50 +0200373#define RTC_UNREACHABLE_CODE_HIT false
374#define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT)
375
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200376// TODO(bugs.webrtc.org/8454): Add an RTC_ prefix or rename differently.
Jonas Olssond000b0a2018-07-03 12:07:53 +0200377#define FATAL() \
378 rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \
379 "FATAL()") & \
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200380 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200381
382// Performs the integer division a/b and returns the result. CHECKs that the
383// remainder is zero.
384template <typename T>
385inline 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
kwiberg2e486462016-08-23 05:54:25 -0700425
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200426#endif // RTC_BASE_CHECKS_H_