diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-02 00:41:12 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-02 00:41:12 +0000 |
commit | e150c038b4bc92ce5518688737d32bc5208fa250 (patch) | |
tree | 55049a108f68c9804276c4603f0f0c6c32c12589 | |
parent | 2a040cd88b04d101c2b1612c6c3c92cb6f68176b (diff) | |
download | chromium_src-e150c038b4bc92ce5518688737d32bc5208fa250.zip chromium_src-e150c038b4bc92ce5518688737d32bc5208fa250.tar.gz chromium_src-e150c038b4bc92ce5518688737d32bc5208fa250.tar.bz2 |
Reland r40289: "Add support for CHECK_* macros.""
Make sure DEFINE_CHECK_OP_IMPL isn't omitted for official builds.
Review URL: http://codereview.chromium.org/661325
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40327 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/allocator/allocator_unittests.cc | 3 | ||||
-rw-r--r-- | base/logging.h | 115 | ||||
-rw-r--r-- | base/process_util_unittest.cc | 2 | ||||
-rw-r--r-- | base/rand_util_posix.cc | 2 | ||||
-rw-r--r-- | base/rand_util_win.cc | 2 |
5 files changed, 79 insertions, 45 deletions
diff --git a/base/allocator/allocator_unittests.cc b/base/allocator/allocator_unittests.cc index 919cfab..fdda081 100644 --- a/base/allocator/allocator_unittests.cc +++ b/base/allocator/allocator_unittests.cc @@ -402,7 +402,7 @@ TEST(Allocators, Realloc1) { // The larger the start-size, the larger the non-reallocing delta. for (int d = 0; d < s*2; ++d) { void* new_p = realloc(p, start_sizes[s] + deltas[d]); - CHECK(p == new_p); // realloc should not allocate new memory + CHECK_EQ(p, new_p); // realloc should not allocate new memory } // Test again, but this time reallocing smaller first. for (int d = 0; d < s*2; ++d) { @@ -487,4 +487,3 @@ int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } - diff --git a/base/logging.h b/base/logging.h index 1aa1162..00f3f71 100644 --- a/base/logging.h +++ b/base/logging.h @@ -332,6 +332,55 @@ std::string* MakeCheckOpString(const int& v1, return MakeCheckOpStringIntInt(v1, v2, names); } +// Helper macro for binary operators. +// Don't use this macro directly in your code, use CHECK_EQ et al below. +#define CHECK_OP(name, op, val1, val2) \ + if (logging::CheckOpString _result = \ + logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \ + logging::LogMessage(__FILE__, __LINE__, _result).stream() + +// Helper functions for string comparisons. +// To avoid bloat, the definitions are in logging.cc. +#define DECLARE_CHECK_STROP_IMPL(func, expected) \ + std::string* Check##func##expected##Impl(const char* s1, \ + const char* s2, \ + const char* names); +DECLARE_CHECK_STROP_IMPL(strcmp, true) +DECLARE_CHECK_STROP_IMPL(strcmp, false) +DECLARE_CHECK_STROP_IMPL(_stricmp, true) +DECLARE_CHECK_STROP_IMPL(_stricmp, false) +#undef DECLARE_CHECK_STROP_IMPL + +// Helper macro for string comparisons. +// Don't use this macro directly in your code, use CHECK_STREQ et al below. +#define CHECK_STROP(func, op, expected, s1, s2) \ + while (CheckOpString _result = \ + logging::Check##func##expected##Impl((s1), (s2), \ + #s1 " " #op " " #s2)) \ + LOG(FATAL) << *_result.str_ + +// String (char*) equality/inequality checks. +// CASE versions are case-insensitive. +// +// Note that "s1" and "s2" may be temporary strings which are destroyed +// by the compiler at the end of the current "full expression" +// (e.g. CHECK_STREQ(Foo().c_str(), Bar().c_str())). + +#define CHECK_STREQ(s1, s2) CHECK_STROP(strcmp, ==, true, s1, s2) +#define CHECK_STRNE(s1, s2) CHECK_STROP(strcmp, !=, false, s1, s2) +#define CHECK_STRCASEEQ(s1, s2) CHECK_STROP(_stricmp, ==, true, s1, s2) +#define CHECK_STRCASENE(s1, s2) CHECK_STROP(_stricmp, !=, false, s1, s2) + +#define CHECK_INDEX(I,A) CHECK(I < (sizeof(A)/sizeof(A[0]))) +#define CHECK_BOUND(B,A) CHECK(B <= (sizeof(A)/sizeof(A[0]))) + +#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) + // Plus some debug-logging macros that get compiled to nothing for production // // DEBUG_MODE is for uses like @@ -444,11 +493,8 @@ enum { DEBUG_MODE = 0 }; // debug-only checking. not executed in NDEBUG mode. enum { DEBUG_MODE = 1 }; -#define DCHECK(condition) \ - LOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". " - -#define DPCHECK(condition) \ - PLOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". " +#define DCHECK(condition) CHECK(condition) +#define DPCHECK(condition) PCHECK(condition) // Helper macro for binary operators. // Don't use this macro directly in your code, use DCHECK_EQ et al below. @@ -457,18 +503,6 @@ enum { DEBUG_MODE = 1 }; logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \ logging::LogMessage(__FILE__, __LINE__, _result).stream() -// Helper functions for string comparisons. -// To avoid bloat, the definitions are in logging.cc. -#define DECLARE_DCHECK_STROP_IMPL(func, expected) \ - std::string* Check##func##expected##Impl(const char* s1, \ - const char* s2, \ - const char* names); -DECLARE_DCHECK_STROP_IMPL(strcmp, true) -DECLARE_DCHECK_STROP_IMPL(strcmp, false) -DECLARE_DCHECK_STROP_IMPL(_stricmp, true) -DECLARE_DCHECK_STROP_IMPL(_stricmp, false) -#undef DECLARE_DCHECK_STROP_IMPL - // Helper macro for string comparisons. // Don't use this macro directly in your code, use CHECK_STREQ et al below. #define DCHECK_STROP(func, op, expected, s1, s2) \ @@ -560,29 +594,6 @@ extern bool g_enable_dcheck; #endif // NDEBUG -// Helper functions for DCHECK_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_DCHECK_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 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 MakeCheckOpString(v1, v2, names); \ - } -DEFINE_DCHECK_OP_IMPL(EQ, ==) -DEFINE_DCHECK_OP_IMPL(NE, !=) -DEFINE_DCHECK_OP_IMPL(LE, <=) -DEFINE_DCHECK_OP_IMPL(LT, < ) -DEFINE_DCHECK_OP_IMPL(GE, >=) -DEFINE_DCHECK_OP_IMPL(GT, > ) -#undef DEFINE_DCHECK_OP_IMPL - // Equality/Inequality checks - compare two values, and log a LOG_FATAL message // including the two values when the result is not as expected. The values // must have operator<<(ostream, ...) defined. @@ -611,6 +622,30 @@ DEFINE_DCHECK_OP_IMPL(GT, > ) #endif // OMIT_DLOG_AND_DCHECK #undef OMIT_DLOG_AND_DCHECK + +// Helper functions for 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 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 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 NOTREACHED() DCHECK(false) // Redefine the standard assert to use our nice log files diff --git a/base/process_util_unittest.cc b/base/process_util_unittest.cc index f9c0b50..3170b6a 100644 --- a/base/process_util_unittest.cc +++ b/base/process_util_unittest.cc @@ -262,7 +262,7 @@ int ProcessUtilTest::CountOpenFDsInChild() { int num_open_files = -1; ssize_t bytes_read = HANDLE_EINTR(read(fds[0], &num_open_files, sizeof(num_open_files))); - CHECK(bytes_read == static_cast<ssize_t>(sizeof(num_open_files))); + CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(num_open_files))); CHECK(WaitForSingleProcess(handle, 1000)); base::CloseProcessHandle(handle); diff --git a/base/rand_util_posix.cc b/base/rand_util_posix.cc index a2bdeb8..c65551d 100644 --- a/base/rand_util_posix.cc +++ b/base/rand_util_posix.cc @@ -22,7 +22,7 @@ class URandomFd { public: URandomFd() { fd_ = open("/dev/urandom", O_RDONLY); - CHECK(fd_ >= 0) << "Cannot open /dev/urandom: " << errno; + CHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno; } ~URandomFd() { diff --git a/base/rand_util_win.cc b/base/rand_util_win.cc index 13e8fb2..ec0411e 100644 --- a/base/rand_util_win.cc +++ b/base/rand_util_win.cc @@ -13,7 +13,7 @@ namespace { uint32 RandUint32() { uint32 number; - CHECK(rand_s(&number) == 0); + CHECK_EQ(rand_s(&number), 0); return number; } |