aboutsummaryrefslogtreecommitdiff
path: root/webrtc/base/checks.h
diff options
context:
space:
mode:
authorhenrikg <henrikg@webrtc.org>2015-09-17 00:24:34 -0700
committerCommit bot <commit-bot@chromium.org>2015-09-17 07:24:51 +0000
commit91d6edef35e7275879c30ce16ecb8b6dc73c6e4a (patch)
tree313b64a3d88422a53f5b091f770a2d33dce823ce /webrtc/base/checks.h
parentc0ac6cad00f51b99fafaa7c19e2e63b3b542735b (diff)
downloadwebrtc-91d6edef35e7275879c30ce16ecb8b6dc73c6e4a.tar.gz
Add RTC_ prefix to (D)CHECKs and related macros.
We must remove dependency on Chromium, i.e. we can't use Chromium's base/logging.h. That means we need to define these macros in WebRTC also when doing Chromium builds. And this causes redefinition. Alternative solutions: * Check if we already have defined e.g. CHECK, and don't define them in that case. This makes us depend on include order in Chromium, which is not acceptable. * Don't allow using the macros in WebRTC headers. Error prone since if someone adds it there by mistake it may compile fine, but later break if a header in added or order is changed in Chromium. That will be confusing and hard to enforce. * Ensure that headers that are included by an embedder don't include our macros. This would require some heavy refactoring to be maintainable and enforcable. * Changes in Chromium for this is obviously not an option. BUG=chromium:468375 NOTRY=true Review URL: https://codereview.webrtc.org/1335923002 Cr-Commit-Position: refs/heads/master@{#9964}
Diffstat (limited to 'webrtc/base/checks.h')
-rw-r--r--webrtc/base/checks.h162
1 files changed, 80 insertions, 82 deletions
diff --git a/webrtc/base/checks.h b/webrtc/base/checks.h
index 521586844a..ad0954d410 100644
--- a/webrtc/base/checks.h
+++ b/webrtc/base/checks.h
@@ -25,50 +25,46 @@
// The macros here print a message to stderr and abort under various
// conditions. All will accept additional stream messages. For example:
-// DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
+// RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
//
-// - CHECK(x) is an assertion that x is always true, and that if it isn't, it's
-// better to terminate the process than to continue. During development, the
-// reason that it's better to terminate might simply be that the error
+// - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't,
+// it's better to terminate the process than to continue. During development,
+// the reason that it's better to terminate might simply be that the error
// handling code isn't in place yet; in production, the reason might be that
// the author of the code truly believes that x will always be true, but that
// she recognizes that if she is wrong, abrupt and unpleasant process
// termination is still better than carrying on with the assumption violated.
//
-// CHECK always evaluates its argument, so it's OK for x to have side
+// RTC_CHECK always evaluates its argument, so it's OK for x to have side
// effects.
//
-// - DCHECK(x) is the same as CHECK(x)---an assertion that x is always
+// - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always
// true---except that x will only be evaluated in debug builds; in production
// builds, x is simply assumed to be true. This is useful if evaluating x is
// expensive and the expected cost of failing to detect the violated
// assumption is acceptable. You should not handle cases where a production
// build fails to spot a violated condition, even those that would result in
// crashes. If the code needs to cope with the error, make it cope, but don't
-// call DCHECK; if the condition really can't occur, but you'd sleep better
-// at night knowing that the process will suicide instead of carrying on in
-// case you were wrong, use CHECK instead of DCHECK.
+// call RTC_DCHECK; if the condition really can't occur, but you'd sleep
+// better at night knowing that the process will suicide instead of carrying
+// on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK.
//
-// DCHECK only evaluates its argument in debug builds, so if x has visible
+// RTC_DCHECK only evaluates its argument in debug builds, so if x has visible
// side effects, you need to write e.g.
-// bool w = x; DCHECK(w);
+// bool w = x; RTC_DCHECK(w);
//
-// - CHECK_EQ, _NE, _GT, ..., and DCHECK_EQ, _NE, _GT, ... are specialized
-// variants of CHECK and DCHECK that print prettier messages if the condition
-// doesn't hold. Prefer them to raw CHECK and DCHECK.
+// - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are
+// specialized variants of RTC_CHECK and RTC_DCHECK that print prettier
+// messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and
+// RTC_DCHECK.
//
// - FATAL() aborts unconditionally.
namespace rtc {
-// The use of overrides/webrtc/base/logging.h in a Chromium build results in
-// redefined macro errors. Fortunately, Chromium's macros can be used as drop-in
-// replacements for the standalone versions.
-#ifndef WEBRTC_CHROMIUM_BUILD
-
// Helper macro which avoids evaluating the arguments to a stream if
// the condition doesn't hold.
-#define LAZY_STREAM(stream, condition) \
+#define RTC_LAZY_STREAM(stream, condition) \
!(condition) ? static_cast<void>(0) : rtc::FatalMessageVoidify() & (stream)
// The actual stream used isn't important. We reference condition in the code
@@ -76,30 +72,30 @@ namespace rtc {
// in a particularly convoluted way with an extra ?: because that appears to be
// the simplest construct that keeps Visual Studio from complaining about
// condition being unused).
-#define EAT_STREAM_PARAMETERS(condition) \
- (true ? true : !(condition)) \
- ? static_cast<void>(0) \
+#define RTC_EAT_STREAM_PARAMETERS(condition) \
+ (true ? true : !(condition)) \
+ ? static_cast<void>(0) \
: rtc::FatalMessageVoidify() & rtc::FatalMessage("", 0).stream()
-// CHECK dies with a fatal error if condition is not true. It is *not*
+// RTC_CHECK dies with a fatal error if condition is not true. It is *not*
// controlled by NDEBUG, so the check will be executed regardless of
// compilation mode.
//
-// We make sure CHECK et al. always evaluates their arguments, as
-// doing CHECK(FunctionWithSideEffect()) is a common idiom.
-#define CHECK(condition) \
- LAZY_STREAM(rtc::FatalMessage(__FILE__, __LINE__).stream(), !(condition)) \
- << "Check failed: " #condition << std::endl << "# "
+// We make sure RTC_CHECK et al. always evaluates their arguments, as
+// doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom.
+#define RTC_CHECK(condition) \
+ RTC_LAZY_STREAM(rtc::FatalMessage(__FILE__, __LINE__).stream(), \
+ !(condition)) \
+ << "Check failed: " #condition << std::endl << "# "
// Helper macro for binary operators.
-// Don't use this macro directly in your code, use CHECK_EQ et al below.
+// Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
//
// TODO(akalin): Rewrite this so that constructs like if (...)
-// CHECK_EQ(...) else { ... } work properly.
-#define CHECK_OP(name, op, val1, val2) \
- if (std::string* _result = \
- rtc::Check##name##Impl((val1), (val2), \
- #val1 " " #op " " #val2)) \
+// RTC_CHECK_EQ(...) else { ... } work properly.
+#define RTC_CHECK_OP(name, op, val1, val2) \
+ if (std::string* _result = \
+ rtc::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
rtc::FatalMessage(__FILE__, __LINE__, _result).stream()
// Build the error message string. This is separate from the "Impl"
@@ -134,55 +130,59 @@ std::string* MakeCheckOpString<std::string, std::string>(
const std::string&, const std::string&, const char* name);
#endif
-// Helper functions for CHECK_OP macro.
+// Helper functions for RTC_CHECK_OP macro.
// The (int, int) specialization works around the issue that the compiler
// will not instantiate the template version of the function on values of
// unnamed enum type - see comment below.
-#define DEFINE_CHECK_OP_IMPL(name, op) \
- template <class t1, class t2> \
- inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
- const char* names) { \
- if (v1 op v2) return NULL; \
- else return rtc::MakeCheckOpString(v1, v2, names); \
- } \
+#define DEFINE_RTC_CHECK_OP_IMPL(name, op) \
+ template <class t1, class t2> \
+ inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
+ const char* names) { \
+ if (v1 op v2) \
+ return NULL; \
+ else \
+ return rtc::MakeCheckOpString(v1, v2, names); \
+ } \
inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
- if (v1 op v2) return NULL; \
- else return rtc::MakeCheckOpString(v1, v2, names); \
+ if (v1 op v2) \
+ return NULL; \
+ else \
+ return rtc::MakeCheckOpString(v1, v2, names); \
}
-DEFINE_CHECK_OP_IMPL(EQ, ==)
-DEFINE_CHECK_OP_IMPL(NE, !=)
-DEFINE_CHECK_OP_IMPL(LE, <=)
-DEFINE_CHECK_OP_IMPL(LT, < )
-DEFINE_CHECK_OP_IMPL(GE, >=)
-DEFINE_CHECK_OP_IMPL(GT, > )
-#undef DEFINE_CHECK_OP_IMPL
-
-#define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
-#define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
-#define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
-#define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
-#define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
-#define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
-
-// The DCHECK macro is equivalent to CHECK except that it only generates code
-// in debug builds. It does reference the condition parameter in all cases,
+DEFINE_RTC_CHECK_OP_IMPL(EQ, ==)
+DEFINE_RTC_CHECK_OP_IMPL(NE, !=)
+DEFINE_RTC_CHECK_OP_IMPL(LE, <=)
+DEFINE_RTC_CHECK_OP_IMPL(LT, < )
+DEFINE_RTC_CHECK_OP_IMPL(GE, >=)
+DEFINE_RTC_CHECK_OP_IMPL(GT, > )
+#undef DEFINE_RTC_CHECK_OP_IMPL
+
+#define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(EQ, ==, val1, val2)
+#define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(NE, !=, val1, val2)
+#define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(LE, <=, val1, val2)
+#define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(LT, < , val1, val2)
+#define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(GE, >=, val1, val2)
+#define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(GT, > , val1, val2)
+
+// The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates
+// code in debug builds. It does reference the condition parameter in all cases,
// though, so callers won't risk getting warnings about unused variables.
#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
-#define DCHECK(condition) CHECK(condition)
-#define DCHECK_EQ(v1, v2) CHECK_EQ(v1, v2)
-#define DCHECK_NE(v1, v2) CHECK_NE(v1, v2)
-#define DCHECK_LE(v1, v2) CHECK_LE(v1, v2)
-#define DCHECK_LT(v1, v2) CHECK_LT(v1, v2)
-#define DCHECK_GE(v1, v2) CHECK_GE(v1, v2)
-#define DCHECK_GT(v1, v2) CHECK_GT(v1, v2)
+#define RTC_DCHECK(condition) RTC_CHECK(condition)
+#define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2)
+#define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2)
+#define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2)
+#define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2)
+#define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2)
+#define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2)
#else
-#define DCHECK(condition) EAT_STREAM_PARAMETERS(condition)
-#define DCHECK_EQ(v1, v2) EAT_STREAM_PARAMETERS((v1) == (v2))
-#define DCHECK_NE(v1, v2) EAT_STREAM_PARAMETERS((v1) != (v2))
-#define DCHECK_LE(v1, v2) EAT_STREAM_PARAMETERS((v1) <= (v2))
-#define DCHECK_LT(v1, v2) EAT_STREAM_PARAMETERS((v1) < (v2))
-#define DCHECK_GE(v1, v2) EAT_STREAM_PARAMETERS((v1) >= (v2))
-#define DCHECK_GT(v1, v2) EAT_STREAM_PARAMETERS((v1) > (v2))
+#define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition)
+#define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) == (v2))
+#define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) != (v2))
+#define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) <= (v2))
+#define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) < (v2))
+#define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) >= (v2))
+#define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) > (v2))
#endif
// This is identical to LogMessageVoidify but in name.
@@ -194,13 +194,11 @@ class FatalMessageVoidify {
void operator&(std::ostream&) { }
};
-#endif // WEBRTC_CHROMIUM_BUILD
-
#define RTC_UNREACHABLE_CODE_HIT false
-#define RTC_NOTREACHED() DCHECK(RTC_UNREACHABLE_CODE_HIT)
+#define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT)
#define FATAL() rtc::FatalMessage(__FILE__, __LINE__).stream()
-// TODO(ajm): Consider adding NOTIMPLEMENTED and NOTREACHED macros when
+// TODO(ajm): Consider adding RTC_NOTIMPLEMENTED macro when
// base/logging.h and system_wrappers/logging.h are consolidated such that we
// can match the Chromium behavior.
@@ -208,7 +206,7 @@ class FatalMessageVoidify {
class FatalMessage {
public:
FatalMessage(const char* file, int line);
- // Used for CHECK_EQ(), etc. Takes ownership of the given string.
+ // Used for RTC_CHECK_EQ(), etc. Takes ownership of the given string.
FatalMessage(const char* file, int line, std::string* result);
NO_RETURN ~FatalMessage();
@@ -224,7 +222,7 @@ class FatalMessage {
// remainder is zero.
template <typename T>
inline T CheckedDivExact(T a, T b) {
- CHECK_EQ(a % b, static_cast<T>(0));
+ RTC_CHECK_EQ(a % b, static_cast<T>(0));
return a / b;
}