| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2006 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2006 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #ifdef __cplusplus | 29 #ifdef __cplusplus |
| 30 } // extern "C" | 30 } // extern "C" |
| 31 #endif | 31 #endif |
| 32 | 32 |
| 33 #ifdef __cplusplus | 33 #ifdef __cplusplus |
| 34 // C++ version. | 34 // C++ version. |
| 35 | 35 |
| 36 #include <sstream> | 36 #include <sstream> |
| 37 #include <string> | 37 #include <string> |
| 38 | 38 |
| 39 #include "webrtc/base/safe_compare.h" |
| 40 |
| 39 // The macros here print a message to stderr and abort under various | 41 // The macros here print a message to stderr and abort under various |
| 40 // conditions. All will accept additional stream messages. For example: | 42 // conditions. All will accept additional stream messages. For example: |
| 41 // RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar."; | 43 // RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar."; |
| 42 // | 44 // |
| 43 // - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't, | 45 // - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't, |
| 44 // it's better to terminate the process than to continue. During development, | 46 // it's better to terminate the process than to continue. During development, |
| 45 // the reason that it's better to terminate might simply be that the error | 47 // the reason that it's better to terminate might simply be that the error |
| 46 // handling code isn't in place yet; in production, the reason might be that | 48 // handling code isn't in place yet; in production, the reason might be that |
| 47 // the author of the code truly believes that x will always be true, but that | 49 // the author of the code truly believes that x will always be true, but that |
| 48 // she recognizes that if she is wrong, abrupt and unpleasant process | 50 // she recognizes that if she is wrong, abrupt and unpleasant process |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 // the simplest construct that keeps Visual Studio from complaining about | 91 // the simplest construct that keeps Visual Studio from complaining about |
| 90 // condition being unused). | 92 // condition being unused). |
| 91 #define RTC_EAT_STREAM_PARAMETERS(ignored) \ | 93 #define RTC_EAT_STREAM_PARAMETERS(ignored) \ |
| 92 (true ? true : ((void)(ignored), true)) \ | 94 (true ? true : ((void)(ignored), true)) \ |
| 93 ? static_cast<void>(0) \ | 95 ? static_cast<void>(0) \ |
| 94 : rtc::FatalMessageVoidify() & rtc::FatalMessage("", 0).stream() | 96 : rtc::FatalMessageVoidify() & rtc::FatalMessage("", 0).stream() |
| 95 | 97 |
| 96 // Call RTC_EAT_STREAM_PARAMETERS with an argument that fails to compile if | 98 // Call RTC_EAT_STREAM_PARAMETERS with an argument that fails to compile if |
| 97 // values of the same types as |a| and |b| can't be compared with the given | 99 // values of the same types as |a| and |b| can't be compared with the given |
| 98 // operation, and that would evaluate |a| and |b| if evaluated. | 100 // operation, and that would evaluate |a| and |b| if evaluated. |
| 99 #define RTC_EAT_STREAM_PARAMETERS_OP(op, a, b) \ | 101 #define RTC_EAT_STREAM_PARAMETERS_OP(op, a, b) \ |
| 100 RTC_EAT_STREAM_PARAMETERS(((void)sizeof(std::declval<decltype(a)>() \ | 102 RTC_EAT_STREAM_PARAMETERS(((void)rtc::safe_cmp::op(a, b))) |
| 101 op std::declval<decltype(b)>()), \ | |
| 102 (void)(a), (void)(b))) | |
| 103 | 103 |
| 104 // RTC_CHECK dies with a fatal error if condition is not true. It is *not* | 104 // RTC_CHECK dies with a fatal error if condition is not true. It is *not* |
| 105 // controlled by NDEBUG or anything else, so the check will be executed | 105 // controlled by NDEBUG or anything else, so the check will be executed |
| 106 // regardless of compilation mode. | 106 // regardless of compilation mode. |
| 107 // | 107 // |
| 108 // We make sure RTC_CHECK et al. always evaluates their arguments, as | 108 // We make sure RTC_CHECK et al. always evaluates their arguments, as |
| 109 // doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom. | 109 // doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom. |
| 110 #define RTC_CHECK(condition) \ | 110 #define RTC_CHECK(condition) \ |
| 111 RTC_LAZY_STREAM(rtc::FatalMessage(__FILE__, __LINE__).stream(), \ | 111 RTC_LAZY_STREAM(rtc::FatalMessage(__FILE__, __LINE__).stream(), \ |
| 112 !(condition)) \ | 112 !(condition)) \ |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 const unsigned int&, const unsigned long&, const char* names); | 151 const unsigned int&, const unsigned long&, const char* names); |
| 152 extern template | 152 extern template |
| 153 std::string* MakeCheckOpString<std::string, std::string>( | 153 std::string* MakeCheckOpString<std::string, std::string>( |
| 154 const std::string&, const std::string&, const char* name); | 154 const std::string&, const std::string&, const char* name); |
| 155 #endif | 155 #endif |
| 156 | 156 |
| 157 // Helper functions for RTC_CHECK_OP macro. | 157 // Helper functions for RTC_CHECK_OP macro. |
| 158 // The (int, int) specialization works around the issue that the compiler | 158 // The (int, int) specialization works around the issue that the compiler |
| 159 // will not instantiate the template version of the function on values of | 159 // will not instantiate the template version of the function on values of |
| 160 // unnamed enum type - see comment below. | 160 // unnamed enum type - see comment below. |
| 161 #define DEFINE_RTC_CHECK_OP_IMPL(name, op) \ | 161 #define DEFINE_RTC_CHECK_OP_IMPL(name) \ |
| 162 template <class t1, class t2> \ | 162 template <class t1, class t2> \ |
| 163 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ | 163 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
| 164 const char* names) { \ | 164 const char* names) { \ |
| 165 if (v1 op v2) \ | 165 if (rtc::safe_cmp::name(v1, v2)) \ |
| 166 return NULL; \ | 166 return NULL; \ |
| 167 else \ | 167 else \ |
| 168 return rtc::MakeCheckOpString(v1, v2, names); \ | 168 return rtc::MakeCheckOpString(v1, v2, names); \ |
| 169 } \ | 169 } \ |
| 170 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ | 170 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ |
| 171 if (v1 op v2) \ | 171 if (rtc::safe_cmp::name(v1, v2)) \ |
| 172 return NULL; \ | 172 return NULL; \ |
| 173 else \ | 173 else \ |
| 174 return rtc::MakeCheckOpString(v1, v2, names); \ | 174 return rtc::MakeCheckOpString(v1, v2, names); \ |
| 175 } | 175 } |
| 176 DEFINE_RTC_CHECK_OP_IMPL(EQ, ==) | 176 DEFINE_RTC_CHECK_OP_IMPL(Eq) |
| 177 DEFINE_RTC_CHECK_OP_IMPL(NE, !=) | 177 DEFINE_RTC_CHECK_OP_IMPL(Ne) |
| 178 DEFINE_RTC_CHECK_OP_IMPL(LE, <=) | 178 DEFINE_RTC_CHECK_OP_IMPL(Le) |
| 179 DEFINE_RTC_CHECK_OP_IMPL(LT, < ) | 179 DEFINE_RTC_CHECK_OP_IMPL(Lt) |
| 180 DEFINE_RTC_CHECK_OP_IMPL(GE, >=) | 180 DEFINE_RTC_CHECK_OP_IMPL(Ge) |
| 181 DEFINE_RTC_CHECK_OP_IMPL(GT, > ) | 181 DEFINE_RTC_CHECK_OP_IMPL(Gt) |
| 182 #undef DEFINE_RTC_CHECK_OP_IMPL | 182 #undef DEFINE_RTC_CHECK_OP_IMPL |
| 183 | 183 |
| 184 #define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(EQ, ==, val1, val2) | 184 #define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2) |
| 185 #define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(NE, !=, val1, val2) | 185 #define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2) |
| 186 #define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(LE, <=, val1, val2) | 186 #define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(Le, <=, val1, val2) |
| 187 #define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(LT, < , val1, val2) | 187 #define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(Lt, <, val1, val2) |
| 188 #define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(GE, >=, val1, val2) | 188 #define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(Ge, >=, val1, val2) |
| 189 #define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(GT, > , val1, val2) | 189 #define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(Gt, >, val1, val2) |
| 190 | 190 |
| 191 // The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates | 191 // The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates |
| 192 // code in debug builds. It does reference the condition parameter in all cases, | 192 // code in debug builds. It does reference the condition parameter in all cases, |
| 193 // though, so callers won't risk getting warnings about unused variables. | 193 // though, so callers won't risk getting warnings about unused variables. |
| 194 #if RTC_DCHECK_IS_ON | 194 #if RTC_DCHECK_IS_ON |
| 195 #define RTC_DCHECK(condition) RTC_CHECK(condition) | 195 #define RTC_DCHECK(condition) RTC_CHECK(condition) |
| 196 #define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2) | 196 #define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2) |
| 197 #define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2) | 197 #define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2) |
| 198 #define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2) | 198 #define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2) |
| 199 #define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2) | 199 #define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2) |
| 200 #define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2) | 200 #define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2) |
| 201 #define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2) | 201 #define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2) |
| 202 #else | 202 #else |
| 203 #define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition) | 203 #define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition) |
| 204 #define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(==, v1, v2) | 204 #define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Eq, v1, v2) |
| 205 #define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(!=, v1, v2) | 205 #define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ne, v1, v2) |
| 206 #define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(<=, v1, v2) | 206 #define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Le, v1, v2) |
| 207 #define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(<, v1, v2) | 207 #define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Lt, v1, v2) |
| 208 #define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(>=, v1, v2) | 208 #define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Ge, v1, v2) |
| 209 #define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(>, v1, v2) | 209 #define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS_OP(Gt, v1, v2) |
| 210 #endif | 210 #endif |
| 211 | 211 |
| 212 // This is identical to LogMessageVoidify but in name. | 212 // This is identical to LogMessageVoidify but in name. |
| 213 class FatalMessageVoidify { | 213 class FatalMessageVoidify { |
| 214 public: | 214 public: |
| 215 FatalMessageVoidify() { } | 215 FatalMessageVoidify() { } |
| 216 // This has to be an operator with a precedence lower than << but | 216 // This has to be an operator with a precedence lower than << but |
| 217 // higher than ?: | 217 // higher than ?: |
| 218 void operator&(std::ostream&) { } | 218 void operator&(std::ostream&) { } |
| 219 }; | 219 }; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 #define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b)) | 281 #define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b)) |
| 282 #define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b)) | 282 #define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b)) |
| 283 #define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b)) | 283 #define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b)) |
| 284 #define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b)) | 284 #define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b)) |
| 285 #define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b)) | 285 #define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b)) |
| 286 #define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b)) | 286 #define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b)) |
| 287 | 287 |
| 288 #endif // __cplusplus | 288 #endif // __cplusplus |
| 289 | 289 |
| 290 #endif // WEBRTC_BASE_CHECKS_H_ | 290 #endif // WEBRTC_BASE_CHECKS_H_ |
| OLD | NEW |