blob: 681361a3d29794ab65bad98c42f7f12d3ee41b55 [file] [log] [blame]
henrike@webrtc.org47be73b2014-05-13 18:00:261/*
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
henrike@webrtc.org47be73b2014-05-13 18:00:2611#ifndef WEBRTC_BASE_CHECKS_H_
12#define WEBRTC_BASE_CHECKS_H_
13
andrew@webrtc.org54ade8b2014-08-28 16:28:2614#include <sstream>
andrew@webrtc.orga70332f2014-09-02 19:00:4515#include <string>
andrew@webrtc.org54ade8b2014-08-28 16:28:2616
andrew@webrtc.org54ade8b2014-08-28 16:28:2617#include "webrtc/typedefs.h"
18
19// The macros here print a message to stderr and abort under various
20// conditions. All will accept additional stream messages. For example:
henrikg5c075c82015-09-17 07:24:3421// RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
andrew@webrtc.org54ade8b2014-08-28 16:28:2622//
henrikg5c075c82015-09-17 07:24:3423// - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't,
24// it's better to terminate the process than to continue. During development,
25// the reason that it's better to terminate might simply be that the error
andrew@webrtc.org54ade8b2014-08-28 16:28:2626// handling code isn't in place yet; in production, the reason might be that
27// the author of the code truly believes that x will always be true, but that
28// she recognizes that if she is wrong, abrupt and unpleasant process
29// termination is still better than carrying on with the assumption violated.
30//
henrikg5c075c82015-09-17 07:24:3431// RTC_CHECK always evaluates its argument, so it's OK for x to have side
kwiberg@webrtc.org19cf9442014-10-08 12:19:5632// effects.
33//
henrikg5c075c82015-09-17 07:24:3434// - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always
andrew@webrtc.org54ade8b2014-08-28 16:28:2635// true---except that x will only be evaluated in debug builds; in production
36// builds, x is simply assumed to be true. This is useful if evaluating x is
37// expensive and the expected cost of failing to detect the violated
38// assumption is acceptable. You should not handle cases where a production
39// build fails to spot a violated condition, even those that would result in
40// crashes. If the code needs to cope with the error, make it cope, but don't
henrikg5c075c82015-09-17 07:24:3441// call RTC_DCHECK; if the condition really can't occur, but you'd sleep
42// better at night knowing that the process will suicide instead of carrying
43// on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK.
andrew@webrtc.org54ade8b2014-08-28 16:28:2644//
henrikg5c075c82015-09-17 07:24:3445// RTC_DCHECK only evaluates its argument in debug builds, so if x has visible
kwiberg@webrtc.org19cf9442014-10-08 12:19:5646// side effects, you need to write e.g.
henrikg5c075c82015-09-17 07:24:3447// bool w = x; RTC_DCHECK(w);
kwiberg@webrtc.org19cf9442014-10-08 12:19:5648//
henrikg5c075c82015-09-17 07:24:3449// - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are
50// specialized variants of RTC_CHECK and RTC_DCHECK that print prettier
51// messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and
52// RTC_DCHECK.
andrew@webrtc.org54ade8b2014-08-28 16:28:2653//
54// - FATAL() aborts unconditionally.
henrikg6fd7bf52015-09-17 09:06:1155//
56// TODO(ajm): Ideally, checks.h would be combined with logging.h, but
57// consolidation with system_wrappers/logging.h should happen first.
andrew@webrtc.org54ade8b2014-08-28 16:28:2658
henrike@webrtc.orgb2eea5c2014-05-14 18:24:1359namespace rtc {
60
andrew@webrtc.org54ade8b2014-08-28 16:28:2661// Helper macro which avoids evaluating the arguments to a stream if
62// the condition doesn't hold.
henrikg5c075c82015-09-17 07:24:3463#define RTC_LAZY_STREAM(stream, condition) \
andrew@webrtc.orga70332f2014-09-02 19:00:4564 !(condition) ? static_cast<void>(0) : rtc::FatalMessageVoidify() & (stream)
andrew@webrtc.org54ade8b2014-08-28 16:28:2665
kwiberg@webrtc.org074735e2014-12-11 08:32:3066// The actual stream used isn't important. We reference condition in the code
67// but don't evaluate it; this is to avoid "unused variable" warnings (we do so
68// in a particularly convoluted way with an extra ?: because that appears to be
69// the simplest construct that keeps Visual Studio from complaining about
70// condition being unused).
henrikg5c075c82015-09-17 07:24:3471#define RTC_EAT_STREAM_PARAMETERS(condition) \
72 (true ? true : !(condition)) \
73 ? static_cast<void>(0) \
kwiberg@webrtc.org074735e2014-12-11 08:32:3074 : rtc::FatalMessageVoidify() & rtc::FatalMessage("", 0).stream()
andrew@webrtc.org54ade8b2014-08-28 16:28:2675
henrikg5c075c82015-09-17 07:24:3476// RTC_CHECK dies with a fatal error if condition is not true. It is *not*
andrew@webrtc.org54ade8b2014-08-28 16:28:2677// controlled by NDEBUG, so the check will be executed regardless of
78// compilation mode.
79//
henrikg5c075c82015-09-17 07:24:3480// We make sure RTC_CHECK et al. always evaluates their arguments, as
81// doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom.
82#define RTC_CHECK(condition) \
83 RTC_LAZY_STREAM(rtc::FatalMessage(__FILE__, __LINE__).stream(), \
84 !(condition)) \
85 << "Check failed: " #condition << std::endl << "# "
andrew@webrtc.org54ade8b2014-08-28 16:28:2686
87// Helper macro for binary operators.
henrikg5c075c82015-09-17 07:24:3488// Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
andrew@webrtc.org54ade8b2014-08-28 16:28:2689//
90// TODO(akalin): Rewrite this so that constructs like if (...)
henrikg5c075c82015-09-17 07:24:3491// RTC_CHECK_EQ(...) else { ... } work properly.
92#define RTC_CHECK_OP(name, op, val1, val2) \
93 if (std::string* _result = \
94 rtc::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
andrew@webrtc.org54ade8b2014-08-28 16:28:2695 rtc::FatalMessage(__FILE__, __LINE__, _result).stream()
96
97// Build the error message string. This is separate from the "Impl"
98// function template because it is not performance critical and so can
99// be out of line, while the "Impl" code should be inline. Caller
100// takes ownership of the returned string.
101template<class t1, class t2>
102std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
103 std::ostringstream ss;
104 ss << names << " (" << v1 << " vs. " << v2 << ")";
105 std::string* msg = new std::string(ss.str());
106 return msg;
107}
108
109// MSVC doesn't like complex extern templates and DLLs.
110#if !defined(COMPILER_MSVC)
111// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
112// in logging.cc.
113extern template std::string* MakeCheckOpString<int, int>(
114 const int&, const int&, const char* names);
115extern template
116std::string* MakeCheckOpString<unsigned long, unsigned long>(
117 const unsigned long&, const unsigned long&, const char* names);
118extern template
119std::string* MakeCheckOpString<unsigned long, unsigned int>(
120 const unsigned long&, const unsigned int&, const char* names);
121extern template
122std::string* MakeCheckOpString<unsigned int, unsigned long>(
123 const unsigned int&, const unsigned long&, const char* names);
124extern template
125std::string* MakeCheckOpString<std::string, std::string>(
126 const std::string&, const std::string&, const char* name);
127#endif
128
henrikg5c075c82015-09-17 07:24:34129// Helper functions for RTC_CHECK_OP macro.
andrew@webrtc.org54ade8b2014-08-28 16:28:26130// The (int, int) specialization works around the issue that the compiler
131// will not instantiate the template version of the function on values of
132// unnamed enum type - see comment below.
henrikg5c075c82015-09-17 07:24:34133#define DEFINE_RTC_CHECK_OP_IMPL(name, op) \
134 template <class t1, class t2> \
135 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
136 const char* names) { \
137 if (v1 op v2) \
138 return NULL; \
139 else \
140 return rtc::MakeCheckOpString(v1, v2, names); \
141 } \
andrew@webrtc.org54ade8b2014-08-28 16:28:26142 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
henrikg5c075c82015-09-17 07:24:34143 if (v1 op v2) \
144 return NULL; \
145 else \
146 return rtc::MakeCheckOpString(v1, v2, names); \
andrew@webrtc.org54ade8b2014-08-28 16:28:26147 }
henrikg5c075c82015-09-17 07:24:34148DEFINE_RTC_CHECK_OP_IMPL(EQ, ==)
149DEFINE_RTC_CHECK_OP_IMPL(NE, !=)
150DEFINE_RTC_CHECK_OP_IMPL(LE, <=)
151DEFINE_RTC_CHECK_OP_IMPL(LT, < )
152DEFINE_RTC_CHECK_OP_IMPL(GE, >=)
153DEFINE_RTC_CHECK_OP_IMPL(GT, > )
154#undef DEFINE_RTC_CHECK_OP_IMPL
andrew@webrtc.org54ade8b2014-08-28 16:28:26155
henrikg5c075c82015-09-17 07:24:34156#define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(EQ, ==, val1, val2)
157#define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(NE, !=, val1, val2)
158#define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(LE, <=, val1, val2)
159#define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(LT, < , val1, val2)
160#define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(GE, >=, val1, val2)
161#define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(GT, > , val1, val2)
andrew@webrtc.org54ade8b2014-08-28 16:28:26162
henrikg5c075c82015-09-17 07:24:34163// The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates
164// code in debug builds. It does reference the condition parameter in all cases,
kwiberg@webrtc.org074735e2014-12-11 08:32:30165// though, so callers won't risk getting warnings about unused variables.
henrik.lundin@webrtc.org5fa39622014-09-15 11:19:35166#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
Karl Wiberg882e2212015-10-26 18:51:29167#define RTC_DCHECK_IS_ON 1
henrikg5c075c82015-09-17 07:24:34168#define RTC_DCHECK(condition) RTC_CHECK(condition)
169#define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2)
170#define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2)
171#define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2)
172#define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2)
173#define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2)
174#define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2)
andrew@webrtc.org54ade8b2014-08-28 16:28:26175#else
Karl Wiberg882e2212015-10-26 18:51:29176#define RTC_DCHECK_IS_ON 0
henrikg5c075c82015-09-17 07:24:34177#define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition)
178#define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) == (v2))
179#define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) != (v2))
180#define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) <= (v2))
181#define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) < (v2))
182#define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) >= (v2))
183#define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) > (v2))
andrew@webrtc.org54ade8b2014-08-28 16:28:26184#endif
185
andrew@webrtc.orga70332f2014-09-02 19:00:45186// This is identical to LogMessageVoidify but in name.
187class FatalMessageVoidify {
188 public:
189 FatalMessageVoidify() { }
190 // This has to be an operator with a precedence lower than << but
191 // higher than ?:
192 void operator&(std::ostream&) { }
193};
194
magjed@webrtc.org5f8ced02015-03-04 11:25:46195#define RTC_UNREACHABLE_CODE_HIT false
henrikg5c075c82015-09-17 07:24:34196#define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT)
magjed@webrtc.org5f8ced02015-03-04 11:25:46197
andrew@webrtc.org54ade8b2014-08-28 16:28:26198#define FATAL() rtc::FatalMessage(__FILE__, __LINE__).stream()
henrikg5c075c82015-09-17 07:24:34199// TODO(ajm): Consider adding RTC_NOTIMPLEMENTED macro when
andrew@webrtc.org54ade8b2014-08-28 16:28:26200// base/logging.h and system_wrappers/logging.h are consolidated such that we
201// can match the Chromium behavior.
202
203// Like a stripped-down LogMessage from logging.h, except that it aborts.
204class FatalMessage {
205 public:
206 FatalMessage(const char* file, int line);
henrikg5c075c82015-09-17 07:24:34207 // Used for RTC_CHECK_EQ(), etc. Takes ownership of the given string.
andrew@webrtc.org54ade8b2014-08-28 16:28:26208 FatalMessage(const char* file, int line, std::string* result);
209 NO_RETURN ~FatalMessage();
210
211 std::ostream& stream() { return stream_; }
212
213 private:
214 void Init(const char* file, int line);
215
216 std::ostringstream stream_;
217};
henrike@webrtc.org47be73b2014-05-13 18:00:26218
henrik.lundin@webrtc.org49fcf042015-01-26 11:08:53219// Performs the integer division a/b and returns the result. CHECKs that the
220// remainder is zero.
221template <typename T>
222inline T CheckedDivExact(T a, T b) {
henrikg5c075c82015-09-17 07:24:34223 RTC_CHECK_EQ(a % b, static_cast<T>(0));
henrik.lundin@webrtc.org49fcf042015-01-26 11:08:53224 return a / b;
225}
226
henrike@webrtc.orgb2eea5c2014-05-14 18:24:13227} // namespace rtc
henrike@webrtc.org47be73b2014-05-13 18:00:26228
henrike@webrtc.org47be73b2014-05-13 18:00:26229#endif // WEBRTC_BASE_CHECKS_H_