blob: 9de6d47573cff73150b1ab9be75df980f0a71e9e [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__)
27#define RTC_NORETURN __attribute__ ((__noreturn__))
28#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
Karl Wiberge40468b2017-11-22 10:42:26 +010045#include "rtc_base/numerics/safe_compare.h"
Jonas Olssonf8e5c112018-06-14 13:14:22 +020046#include "rtc_base/system/inline.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047
48// The macros here print a message to stderr and abort under various
49// conditions. All will accept additional stream messages. For example:
50// RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
51//
52// - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't,
53// it's better to terminate the process than to continue. During development,
54// the reason that it's better to terminate might simply be that the error
55// handling code isn't in place yet; in production, the reason might be that
56// the author of the code truly believes that x will always be true, but that
57// she recognizes that if she is wrong, abrupt and unpleasant process
58// termination is still better than carrying on with the assumption violated.
59//
60// RTC_CHECK always evaluates its argument, so it's OK for x to have side
61// effects.
62//
63// - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always
64// true---except that x will only be evaluated in debug builds; in production
65// builds, x is simply assumed to be true. This is useful if evaluating x is
66// expensive and the expected cost of failing to detect the violated
67// assumption is acceptable. You should not handle cases where a production
68// build fails to spot a violated condition, even those that would result in
69// crashes. If the code needs to cope with the error, make it cope, but don't
70// call RTC_DCHECK; if the condition really can't occur, but you'd sleep
71// better at night knowing that the process will suicide instead of carrying
72// on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK.
73//
74// RTC_DCHECK only evaluates its argument in debug builds, so if x has visible
75// side effects, you need to write e.g.
76// bool w = x; RTC_DCHECK(w);
77//
78// - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are
79// specialized variants of RTC_CHECK and RTC_DCHECK that print prettier
80// messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and
81// RTC_DCHECK.
82//
83// - FATAL() aborts unconditionally.
84//
85// TODO(ajm): Ideally, checks.h would be combined with logging.h, but
86// consolidation with system_wrappers/logging.h should happen first.
87
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,
102 kVoidP,
Jonas Olssond000b0a2018-07-03 12:07:53 +0200103
104 // kCheckOp doesn't represent an argument type. Instead, it is sent as the
105 // first argument from RTC_CHECK_OP to make FatalLog use the next two
106 // arguments to build the special CHECK_OP error message
107 // (the "a == b (1 vs. 2)" bit).
108 kCheckOp,
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200109};
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200111RTC_NORETURN void FatalLog(const char* file,
112 int line,
113 const char* message,
114 const CheckArgType* fmt,
115 ...);
116
117// Wrapper for log arguments. Only ever make values of this type with the
118// MakeVal() functions.
119template <CheckArgType N, typename T>
120struct Val {
121 static constexpr CheckArgType Type() { return N; }
122 T GetVal() const { return val; }
123 T val;
124};
125
126inline Val<CheckArgType::kInt, int> MakeVal(int x) {
127 return {x};
128}
129inline Val<CheckArgType::kLong, long> MakeVal(long x) {
130 return {x};
131}
132inline Val<CheckArgType::kLongLong, long long> MakeVal(long long x) {
133 return {x};
134}
135inline Val<CheckArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
136 return {x};
137}
138inline Val<CheckArgType::kULong, unsigned long> MakeVal(unsigned long x) {
139 return {x};
140}
141inline Val<CheckArgType::kULongLong, unsigned long long> MakeVal(
142 unsigned long long x) {
143 return {x};
144}
145
146inline Val<CheckArgType::kDouble, double> MakeVal(double x) {
147 return {x};
148}
149inline Val<CheckArgType::kLongDouble, long double> MakeVal(long double x) {
150 return {x};
151}
152
153inline Val<CheckArgType::kCharP, const char*> MakeVal(const char* x) {
154 return {x};
155}
156inline Val<CheckArgType::kStdString, const std::string*> MakeVal(
157 const std::string& x) {
158 return {&x};
159}
160
161inline Val<CheckArgType::kVoidP, const void*> MakeVal(const void* x) {
162 return {x};
163}
164
165// Ephemeral type that represents the result of the logging << operator.
166template <typename... Ts>
167class LogStreamer;
168
169// Base case: Before the first << argument.
170template <>
171class LogStreamer<> final {
172 public:
173 template <
174 typename U,
175 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
176 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
177 U arg) const {
178 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
179 this);
180 }
181
182 template <
183 typename U,
184 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
185 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
186 const U& arg) const {
187 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
188 this);
189 }
190
191 template <typename... Us>
192 RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file,
193 const int line,
194 const char* message,
195 const Us&... args) {
196 static constexpr CheckArgType t[] = {Us::Type()..., CheckArgType::kEnd};
197 FatalLog(file, line, message, t, args.GetVal()...);
198 }
Jonas Olssond000b0a2018-07-03 12:07:53 +0200199
200 template <typename... Us>
201 RTC_NORETURN RTC_FORCE_INLINE static void CallCheckOp(const char* file,
202 const int line,
203 const char* message,
204 const Us&... args) {
205 static constexpr CheckArgType t[] = {CheckArgType::kCheckOp, Us::Type()...,
206 CheckArgType::kEnd};
207 FatalLog(file, line, message, t, args.GetVal()...);
208 }
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200209};
210
211// Inductive case: We've already seen at least one << argument. The most recent
212// one had type `T`, and the earlier ones had types `Ts`.
213template <typename T, typename... Ts>
214class LogStreamer<T, Ts...> final {
215 public:
216 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
217 : arg_(arg), prior_(prior) {}
218
219 template <
220 typename U,
221 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
222 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
223 operator<<(U arg) const {
224 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
225 MakeVal(arg), this);
226 }
227
228 template <
229 typename U,
230 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
231 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
232 operator<<(const U& arg) const {
233 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
234 MakeVal(arg), this);
235 }
236
237 template <typename... Us>
238 RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file,
239 const int line,
240 const char* message,
241 const Us&... args) const {
242 prior_->Call(file, line, message, arg_, args...);
243 }
244
Jonas Olssond000b0a2018-07-03 12:07:53 +0200245 template <typename... Us>
246 RTC_NORETURN RTC_FORCE_INLINE void CallCheckOp(const char* file,
247 const int line,
248 const char* message,
249 const Us&... args) const {
250 prior_->CallCheckOp(file, line, message, arg_, args...);
251 }
252
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200253 private:
254 // The most recent argument.
255 T arg_;
256
257 // Earlier arguments.
258 const LogStreamer<Ts...>* prior_;
259};
260
Jonas Olssond000b0a2018-07-03 12:07:53 +0200261template <bool isCheckOp>
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200262class FatalLogCall final {
263 public:
264 FatalLogCall(const char* file, int line, const char* message)
265 : file_(file), line_(line), message_(message) {}
266
267 // This can be any binary operator with precedence lower than <<.
268 template <typename... Ts>
269 RTC_NORETURN RTC_FORCE_INLINE void operator&(
270 const LogStreamer<Ts...>& streamer) {
Jonas Olssond000b0a2018-07-03 12:07:53 +0200271 isCheckOp ? streamer.CallCheckOp(file_, line_, message_)
272 : streamer.Call(file_, line_, message_);
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200273 }
274
275 private:
276 const char* file_;
277 int line_;
278 const char* message_;
279};
280} // namespace webrtc_checks_impl
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200281
282// The actual stream used isn't important. We reference |ignored| in the code
283// but don't evaluate it; this is to avoid "unused variable" warnings (we do so
284// in a particularly convoluted way with an extra ?: because that appears to be
285// the simplest construct that keeps Visual Studio from complaining about
286// condition being unused).
Jonas Olssond000b0a2018-07-03 12:07:53 +0200287#define RTC_EAT_STREAM_PARAMETERS(ignored) \
288 (true ? true : ((void)(ignored), true)) \
289 ? static_cast<void>(0) \
290 : rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200291 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200292
293// Call RTC_EAT_STREAM_PARAMETERS with an argument that fails to compile if
294// values of the same types as |a| and |b| can't be compared with the given
295// operation, and that would evaluate |a| and |b| if evaluated.
296#define RTC_EAT_STREAM_PARAMETERS_OP(op, a, b) \
297 RTC_EAT_STREAM_PARAMETERS(((void)rtc::Safe##op(a, b)))
298
299// RTC_CHECK dies with a fatal error if condition is not true. It is *not*
300// controlled by NDEBUG or anything else, so the check will be executed
301// regardless of compilation mode.
302//
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200303// We make sure RTC_CHECK et al. always evaluates |condition|, as
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200304// doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom.
Jonas Olssond000b0a2018-07-03 12:07:53 +0200305#define RTC_CHECK(condition) \
306 while (!(condition)) \
307 rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \
308 #condition) & \
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200309 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200310
311// Helper macro for binary operators.
312// Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
Jonas Olssond000b0a2018-07-03 12:07:53 +0200313#define RTC_CHECK_OP(name, op, val1, val2) \
314 while (!rtc::Safe##name((val1), (val2))) \
315 rtc::webrtc_checks_impl::FatalLogCall<true>(__FILE__, __LINE__, \
316 #val1 " " #op " " #val2) & \
317 rtc::webrtc_checks_impl::LogStreamer<>() << (val1) << (val2)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200318
319#define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2)
320#define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2)
321#define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(Le, <=, val1, val2)
322#define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(Lt, <, val1, val2)
323#define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(Ge, >=, val1, val2)
324#define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(Gt, >, val1, val2)
325
326// The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates
327// code in debug builds. It does reference the condition parameter in all cases,
328// though, so callers won't risk getting warnings about unused variables.
329#if RTC_DCHECK_IS_ON
330#define RTC_DCHECK(condition) RTC_CHECK(condition)
331#define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2)
332#define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2)
333#define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2)
334#define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2)
335#define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2)
336#define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2)
337#else
338#define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition)
339#define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Eq, v1, v2)
340#define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ne, v1, v2)
341#define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Le, v1, v2)
342#define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Lt, v1, v2)
343#define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ge, v1, v2)
344#define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Gt, v1, v2)
345#endif
346
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200347#define RTC_UNREACHABLE_CODE_HIT false
348#define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT)
349
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200350// TODO(bugs.webrtc.org/8454): Add an RTC_ prefix or rename differently.
Jonas Olssond000b0a2018-07-03 12:07:53 +0200351#define FATAL() \
352 rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \
353 "FATAL()") & \
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200354 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200355
356// Performs the integer division a/b and returns the result. CHECKs that the
357// remainder is zero.
358template <typename T>
359inline T CheckedDivExact(T a, T b) {
360 RTC_CHECK_EQ(a % b, 0) << a << " is not evenly divisible by " << b;
361 return a / b;
362}
363
364} // namespace rtc
365
366#else // __cplusplus not defined
367// C version. Lacks many features compared to the C++ version, but usage
368// guidelines are the same.
369
370#define RTC_CHECK(condition) \
371 do { \
372 if (!(condition)) { \
373 rtc_FatalMessage(__FILE__, __LINE__, "CHECK failed: " #condition); \
374 } \
375 } while (0)
376
377#define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b))
378#define RTC_CHECK_NE(a, b) RTC_CHECK((a) != (b))
379#define RTC_CHECK_LE(a, b) RTC_CHECK((a) <= (b))
380#define RTC_CHECK_LT(a, b) RTC_CHECK((a) < (b))
381#define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b))
382#define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b))
383
384#define RTC_DCHECK(condition) \
385 do { \
386 if (RTC_DCHECK_IS_ON && !(condition)) { \
387 rtc_FatalMessage(__FILE__, __LINE__, "DCHECK failed: " #condition); \
388 } \
389 } while (0)
390
391#define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b))
392#define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b))
393#define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b))
394#define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b))
395#define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b))
396#define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b))
397
398#endif // __cplusplus
kwiberg2e486462016-08-23 05:54:25 -0700399
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200400#endif // RTC_BASE_CHECKS_H_