blob: b37df0daba98a4c04b95da29a84aae7ac8f3ac02 [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
43#include <sstream>
44#include <string>
45
Karl Wiberge40468b2017-11-22 10:42:26 +010046#include "rtc_base/numerics/safe_compare.h"
Jonas Olssonf8e5c112018-06-14 13:14:22 +020047#include "rtc_base/system/inline.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048
49// The macros here print a message to stderr and abort under various
50// conditions. All will accept additional stream messages. For example:
51// RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
52//
53// - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't,
54// it's better to terminate the process than to continue. During development,
55// the reason that it's better to terminate might simply be that the error
56// handling code isn't in place yet; in production, the reason might be that
57// the author of the code truly believes that x will always be true, but that
58// she recognizes that if she is wrong, abrupt and unpleasant process
59// termination is still better than carrying on with the assumption violated.
60//
61// RTC_CHECK always evaluates its argument, so it's OK for x to have side
62// effects.
63//
64// - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always
65// true---except that x will only be evaluated in debug builds; in production
66// builds, x is simply assumed to be true. This is useful if evaluating x is
67// expensive and the expected cost of failing to detect the violated
68// assumption is acceptable. You should not handle cases where a production
69// build fails to spot a violated condition, even those that would result in
70// crashes. If the code needs to cope with the error, make it cope, but don't
71// call RTC_DCHECK; if the condition really can't occur, but you'd sleep
72// better at night knowing that the process will suicide instead of carrying
73// on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK.
74//
75// RTC_DCHECK only evaluates its argument in debug builds, so if x has visible
76// side effects, you need to write e.g.
77// bool w = x; RTC_DCHECK(w);
78//
79// - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are
80// specialized variants of RTC_CHECK and RTC_DCHECK that print prettier
81// messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and
82// RTC_DCHECK.
83//
84// - FATAL() aborts unconditionally.
85//
86// TODO(ajm): Ideally, checks.h would be combined with logging.h, but
87// consolidation with system_wrappers/logging.h should happen first.
88
89namespace rtc {
Jonas Olssonf8e5c112018-06-14 13:14:22 +020090namespace webrtc_checks_impl {
91enum class CheckArgType : int8_t {
92 kEnd = 0,
93 kInt,
94 kLong,
95 kLongLong,
96 kUInt,
97 kULong,
98 kULongLong,
99 kDouble,
100 kLongDouble,
101 kCharP,
102 kStdString,
103 kVoidP,
104};
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200106RTC_NORETURN void FatalLog(const char* file,
107 int line,
108 const char* message,
109 const CheckArgType* fmt,
110 ...);
111
112// Wrapper for log arguments. Only ever make values of this type with the
113// MakeVal() functions.
114template <CheckArgType N, typename T>
115struct Val {
116 static constexpr CheckArgType Type() { return N; }
117 T GetVal() const { return val; }
118 T val;
119};
120
121inline Val<CheckArgType::kInt, int> MakeVal(int x) {
122 return {x};
123}
124inline Val<CheckArgType::kLong, long> MakeVal(long x) {
125 return {x};
126}
127inline Val<CheckArgType::kLongLong, long long> MakeVal(long long x) {
128 return {x};
129}
130inline Val<CheckArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
131 return {x};
132}
133inline Val<CheckArgType::kULong, unsigned long> MakeVal(unsigned long x) {
134 return {x};
135}
136inline Val<CheckArgType::kULongLong, unsigned long long> MakeVal(
137 unsigned long long x) {
138 return {x};
139}
140
141inline Val<CheckArgType::kDouble, double> MakeVal(double x) {
142 return {x};
143}
144inline Val<CheckArgType::kLongDouble, long double> MakeVal(long double x) {
145 return {x};
146}
147
148inline Val<CheckArgType::kCharP, const char*> MakeVal(const char* x) {
149 return {x};
150}
151inline Val<CheckArgType::kStdString, const std::string*> MakeVal(
152 const std::string& x) {
153 return {&x};
154}
155
156inline Val<CheckArgType::kVoidP, const void*> MakeVal(const void* x) {
157 return {x};
158}
159
160// Ephemeral type that represents the result of the logging << operator.
161template <typename... Ts>
162class LogStreamer;
163
164// Base case: Before the first << argument.
165template <>
166class LogStreamer<> final {
167 public:
168 template <
169 typename U,
170 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
171 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
172 U arg) const {
173 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
174 this);
175 }
176
177 template <
178 typename U,
179 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
180 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
181 const U& arg) const {
182 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
183 this);
184 }
185
186 template <typename... Us>
187 RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file,
188 const int line,
189 const char* message,
190 const Us&... args) {
191 static constexpr CheckArgType t[] = {Us::Type()..., CheckArgType::kEnd};
192 FatalLog(file, line, message, t, args.GetVal()...);
193 }
194};
195
196// Inductive case: We've already seen at least one << argument. The most recent
197// one had type `T`, and the earlier ones had types `Ts`.
198template <typename T, typename... Ts>
199class LogStreamer<T, Ts...> final {
200 public:
201 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
202 : arg_(arg), prior_(prior) {}
203
204 template <
205 typename U,
206 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
207 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
208 operator<<(U arg) const {
209 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
210 MakeVal(arg), this);
211 }
212
213 template <
214 typename U,
215 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
216 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
217 operator<<(const U& arg) const {
218 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
219 MakeVal(arg), this);
220 }
221
222 template <typename... Us>
223 RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file,
224 const int line,
225 const char* message,
226 const Us&... args) const {
227 prior_->Call(file, line, message, arg_, args...);
228 }
229
230 private:
231 // The most recent argument.
232 T arg_;
233
234 // Earlier arguments.
235 const LogStreamer<Ts...>* prior_;
236};
237
238class FatalLogCall final {
239 public:
240 FatalLogCall(const char* file, int line, const char* message)
241 : file_(file), line_(line), message_(message) {}
242
243 // This can be any binary operator with precedence lower than <<.
244 template <typename... Ts>
245 RTC_NORETURN RTC_FORCE_INLINE void operator&(
246 const LogStreamer<Ts...>& streamer) {
247 streamer.Call(file_, line_, message_);
248 }
249
250 private:
251 const char* file_;
252 int line_;
253 const char* message_;
254};
255} // namespace webrtc_checks_impl
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200256
257// The actual stream used isn't important. We reference |ignored| in the code
258// but don't evaluate it; this is to avoid "unused variable" warnings (we do so
259// in a particularly convoluted way with an extra ?: because that appears to be
260// the simplest construct that keeps Visual Studio from complaining about
261// condition being unused).
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200262#define RTC_EAT_STREAM_PARAMETERS(ignored) \
263 (true ? true : ((void)(ignored), true)) \
264 ? static_cast<void>(0) \
265 : rtc::webrtc_checks_impl::FatalLogCall("", 0, "") & \
266 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200267
268// Call RTC_EAT_STREAM_PARAMETERS with an argument that fails to compile if
269// values of the same types as |a| and |b| can't be compared with the given
270// operation, and that would evaluate |a| and |b| if evaluated.
271#define RTC_EAT_STREAM_PARAMETERS_OP(op, a, b) \
272 RTC_EAT_STREAM_PARAMETERS(((void)rtc::Safe##op(a, b)))
273
274// RTC_CHECK dies with a fatal error if condition is not true. It is *not*
275// controlled by NDEBUG or anything else, so the check will be executed
276// regardless of compilation mode.
277//
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200278// We make sure RTC_CHECK et al. always evaluates |condition|, as
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200279// doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom.
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200280#define RTC_CHECK(condition) \
281 while (!(condition)) \
282 rtc::webrtc_checks_impl::FatalLogCall(__FILE__, __LINE__, #condition) & \
283 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200284
285// Helper macro for binary operators.
286// Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200287// RTC_CHECK_EQ(...) else { ... } work properly.
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200288#define RTC_CHECK_OP(name, op, val1, val2) \
289 while (std::string* _result = \
290 rtc::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
291 rtc::webrtc_checks_impl::FatalLogCall(__FILE__, __LINE__, \
292 _result->c_str()) & \
293 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200294
295// Build the error message string. This is separate from the "Impl"
296// function template because it is not performance critical and so can
297// be out of line, while the "Impl" code should be inline. Caller
298// takes ownership of the returned string.
299template<class t1, class t2>
300std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200301 // TODO(jonasolsson): Use absl::StrCat() instead.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200302 std::ostringstream ss;
303 ss << names << " (" << v1 << " vs. " << v2 << ")";
304 std::string* msg = new std::string(ss.str());
305 return msg;
306}
307
308// MSVC doesn't like complex extern templates and DLLs.
309#if !defined(COMPILER_MSVC)
310// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
311// in logging.cc.
312extern template std::string* MakeCheckOpString<int, int>(
313 const int&, const int&, const char* names);
314extern template
315std::string* MakeCheckOpString<unsigned long, unsigned long>(
316 const unsigned long&, const unsigned long&, const char* names);
317extern template
318std::string* MakeCheckOpString<unsigned long, unsigned int>(
319 const unsigned long&, const unsigned int&, const char* names);
320extern template
321std::string* MakeCheckOpString<unsigned int, unsigned long>(
322 const unsigned int&, const unsigned long&, const char* names);
323extern template
324std::string* MakeCheckOpString<std::string, std::string>(
325 const std::string&, const std::string&, const char* name);
326#endif
327
328// Helper functions for RTC_CHECK_OP macro.
329// The (int, int) specialization works around the issue that the compiler
330// will not instantiate the template version of the function on values of
331// unnamed enum type - see comment below.
332#define DEFINE_RTC_CHECK_OP_IMPL(name) \
333 template <class t1, class t2> \
334 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
335 const char* names) { \
336 if (rtc::Safe##name(v1, v2)) \
337 return nullptr; \
338 else \
339 return rtc::MakeCheckOpString(v1, v2, names); \
340 } \
341 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
342 if (rtc::Safe##name(v1, v2)) \
343 return nullptr; \
344 else \
345 return rtc::MakeCheckOpString(v1, v2, names); \
346 }
347DEFINE_RTC_CHECK_OP_IMPL(Eq)
348DEFINE_RTC_CHECK_OP_IMPL(Ne)
349DEFINE_RTC_CHECK_OP_IMPL(Le)
350DEFINE_RTC_CHECK_OP_IMPL(Lt)
351DEFINE_RTC_CHECK_OP_IMPL(Ge)
352DEFINE_RTC_CHECK_OP_IMPL(Gt)
353#undef DEFINE_RTC_CHECK_OP_IMPL
354
355#define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2)
356#define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2)
357#define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(Le, <=, val1, val2)
358#define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(Lt, <, val1, val2)
359#define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(Ge, >=, val1, val2)
360#define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(Gt, >, val1, val2)
361
362// The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates
363// code in debug builds. It does reference the condition parameter in all cases,
364// though, so callers won't risk getting warnings about unused variables.
365#if RTC_DCHECK_IS_ON
366#define RTC_DCHECK(condition) RTC_CHECK(condition)
367#define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2)
368#define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2)
369#define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2)
370#define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2)
371#define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2)
372#define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2)
373#else
374#define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition)
375#define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Eq, v1, v2)
376#define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ne, v1, v2)
377#define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Le, v1, v2)
378#define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Lt, v1, v2)
379#define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ge, v1, v2)
380#define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Gt, v1, v2)
381#endif
382
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200383#define RTC_UNREACHABLE_CODE_HIT false
384#define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT)
385
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200386// TODO(bugs.webrtc.org/8454): Add an RTC_ prefix or rename differently.
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200387#define FATAL() \
388 rtc::webrtc_checks_impl::FatalLogCall(__FILE__, __LINE__, "FATAL()") & \
389 rtc::webrtc_checks_impl::LogStreamer<>()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200390
391// Performs the integer division a/b and returns the result. CHECKs that the
392// remainder is zero.
393template <typename T>
394inline T CheckedDivExact(T a, T b) {
395 RTC_CHECK_EQ(a % b, 0) << a << " is not evenly divisible by " << b;
396 return a / b;
397}
398
399} // namespace rtc
400
401#else // __cplusplus not defined
402// C version. Lacks many features compared to the C++ version, but usage
403// guidelines are the same.
404
405#define RTC_CHECK(condition) \
406 do { \
407 if (!(condition)) { \
408 rtc_FatalMessage(__FILE__, __LINE__, "CHECK failed: " #condition); \
409 } \
410 } while (0)
411
412#define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b))
413#define RTC_CHECK_NE(a, b) RTC_CHECK((a) != (b))
414#define RTC_CHECK_LE(a, b) RTC_CHECK((a) <= (b))
415#define RTC_CHECK_LT(a, b) RTC_CHECK((a) < (b))
416#define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b))
417#define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b))
418
419#define RTC_DCHECK(condition) \
420 do { \
421 if (RTC_DCHECK_IS_ON && !(condition)) { \
422 rtc_FatalMessage(__FILE__, __LINE__, "DCHECK failed: " #condition); \
423 } \
424 } while (0)
425
426#define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b))
427#define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b))
428#define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b))
429#define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b))
430#define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b))
431#define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b))
432
433#endif // __cplusplus
kwiberg2e486462016-08-23 05:54:25 -0700434
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200435#endif // RTC_BASE_CHECKS_H_