Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: src/base/logging.h

Issue 877753007: Reland "Initial switch to Chromium-style CHECK_* and DCHECK_* macros.". (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: VS201x now happy? Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/base/compiler-specific.h ('k') | src/base/logging.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_BASE_LOGGING_H_ 5 #ifndef V8_BASE_LOGGING_H_
6 #define V8_BASE_LOGGING_H_ 6 #define V8_BASE_LOGGING_H_
7 7
8 #include <stdint.h> 8 #include <cstring>
9 #include <string.h> 9 #include <sstream>
10 #include <string>
10 11
11 #include "src/base/build_config.h" 12 #include "src/base/build_config.h"
12 13
13 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...); 14 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...);
14 15
15 16
16 // The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during 17 // The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during
17 // development, but they should not be relied on in the final product. 18 // development, but they should not be relied on in the final product.
18 #ifdef DEBUG 19 #ifdef DEBUG
19 #define FATAL(msg) \ 20 #define FATAL(msg) \
20 V8_Fatal(__FILE__, __LINE__, "%s", (msg)) 21 V8_Fatal(__FILE__, __LINE__, "%s", (msg))
21 #define UNIMPLEMENTED() \ 22 #define UNIMPLEMENTED() \
22 V8_Fatal(__FILE__, __LINE__, "unimplemented code") 23 V8_Fatal(__FILE__, __LINE__, "unimplemented code")
23 #define UNREACHABLE() \ 24 #define UNREACHABLE() \
24 V8_Fatal(__FILE__, __LINE__, "unreachable code") 25 V8_Fatal(__FILE__, __LINE__, "unreachable code")
25 #else 26 #else
26 #define FATAL(msg) \ 27 #define FATAL(msg) \
27 V8_Fatal("", 0, "%s", (msg)) 28 V8_Fatal("", 0, "%s", (msg))
28 #define UNIMPLEMENTED() \ 29 #define UNIMPLEMENTED() \
29 V8_Fatal("", 0, "unimplemented code") 30 V8_Fatal("", 0, "unimplemented code")
30 #define UNREACHABLE() ((void) 0) 31 #define UNREACHABLE() ((void) 0)
31 #endif 32 #endif
32 33
33 34
34 // The CHECK macro checks that the given condition is true; if not, it
35 // prints a message to stderr and aborts.
36 #define CHECK(condition) \
37 do { \
38 if (V8_UNLIKELY(!(condition))) { \
39 V8_Fatal(__FILE__, __LINE__, "CHECK(%s) failed", #condition); \
40 } \
41 } while (0)
42
43
44 // Helper function used by the CHECK_EQ function when given int
45 // arguments. Should not be called directly.
46 inline void CheckEqualsHelper(const char* file, int line,
47 const char* expected_source, int expected,
48 const char* value_source, int value) {
49 if (V8_UNLIKELY(expected != value)) {
50 V8_Fatal(file, line,
51 "CHECK_EQ(%s, %s) failed\n# Expected: %i\n# Found: %i",
52 expected_source, value_source, expected, value);
53 }
54 }
55
56
57 // Helper function used by the CHECK_EQ function when given int64_t
58 // arguments. Should not be called directly.
59 inline void CheckEqualsHelper(const char* file, int line,
60 const char* expected_source,
61 int64_t expected,
62 const char* value_source,
63 int64_t value) {
64 if (V8_UNLIKELY(expected != value)) {
65 // Print int64_t values in hex, as two int32s,
66 // to avoid platform-dependencies.
67 V8_Fatal(file, line,
68 "CHECK_EQ(%s, %s) failed\n#"
69 " Expected: 0x%08x%08x\n# Found: 0x%08x%08x",
70 expected_source, value_source,
71 static_cast<uint32_t>(expected >> 32),
72 static_cast<uint32_t>(expected),
73 static_cast<uint32_t>(value >> 32),
74 static_cast<uint32_t>(value));
75 }
76 }
77
78
79 // 32-bit AIX defines intptr_t as long int.
80 #if V8_OS_AIX && V8_HOST_ARCH_32_BIT
81 // Helper function used by the CHECK_EQ function when given intptr_t
82 // arguments. Should not be called directly.
83 inline void CheckEqualsHelper(const char* file, int line,
84 const char* expected_source, intptr_t expected,
85 const char* value_source, intptr_t value) {
86 if (expected != value) {
87 V8_Fatal(file, line,
88 "CHECK_EQ(%s, %s) failed\n#"
89 " Expected: 0x%lx\n# Found: 0x%lx",
90 expected_source, value_source, expected, value);
91 }
92 }
93 #endif
94
95
96 // Helper function used by the CHECK_NE function when given int
97 // arguments. Should not be called directly.
98 inline void CheckNonEqualsHelper(const char* file,
99 int line,
100 const char* unexpected_source,
101 int unexpected,
102 const char* value_source,
103 int value) {
104 if (V8_UNLIKELY(unexpected == value)) {
105 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i",
106 unexpected_source, value_source, value);
107 }
108 }
109
110
111 // Helper function used by the CHECK function when given string
112 // arguments. Should not be called directly.
113 inline void CheckEqualsHelper(const char* file,
114 int line,
115 const char* expected_source,
116 const char* expected,
117 const char* value_source,
118 const char* value) {
119 if (V8_UNLIKELY((expected == NULL && value != NULL) ||
120 (expected != NULL && value == NULL) ||
121 (expected != NULL && value != NULL &&
122 strcmp(expected, value) != 0))) {
123 V8_Fatal(file, line,
124 "CHECK_EQ(%s, %s) failed\n# Expected: %s\n# Found: %s",
125 expected_source, value_source, expected, value);
126 }
127 }
128
129
130 inline void CheckNonEqualsHelper(const char* file,
131 int line,
132 const char* expected_source,
133 const char* expected,
134 const char* value_source,
135 const char* value) {
136 if (V8_UNLIKELY(expected == value || (expected != NULL && value != NULL &&
137 strcmp(expected, value) == 0))) {
138 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %s",
139 expected_source, value_source, value);
140 }
141 }
142
143
144 // Helper function used by the CHECK function when given pointer
145 // arguments. Should not be called directly.
146 inline void CheckEqualsHelper(const char* file,
147 int line,
148 const char* expected_source,
149 const void* expected,
150 const char* value_source,
151 const void* value) {
152 if (V8_UNLIKELY(expected != value)) {
153 V8_Fatal(file, line,
154 "CHECK_EQ(%s, %s) failed\n# Expected: %p\n# Found: %p",
155 expected_source, value_source,
156 expected, value);
157 }
158 }
159
160
161 inline void CheckNonEqualsHelper(const char* file,
162 int line,
163 const char* expected_source,
164 const void* expected,
165 const char* value_source,
166 const void* value) {
167 if (V8_UNLIKELY(expected == value)) {
168 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p",
169 expected_source, value_source, value);
170 }
171 }
172
173
174 inline void CheckNonEqualsHelper(const char* file,
175 int line,
176 const char* expected_source,
177 int64_t expected,
178 const char* value_source,
179 int64_t value) {
180 if (V8_UNLIKELY(expected == value)) {
181 V8_Fatal(file, line,
182 "CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f",
183 expected_source, value_source, expected, value);
184 }
185 }
186
187
188 #define CHECK_EQ(expected, value) CheckEqualsHelper(__FILE__, __LINE__, \
189 #expected, expected, #value, value)
190
191
192 #define CHECK_NE(unexpected, value) CheckNonEqualsHelper(__FILE__, __LINE__, \
193 #unexpected, unexpected, #value, value)
194
195
196 #define CHECK_GT(a, b) CHECK((a) > (b))
197 #define CHECK_GE(a, b) CHECK((a) >= (b))
198 #define CHECK_LT(a, b) CHECK((a) < (b))
199 #define CHECK_LE(a, b) CHECK((a) <= (b))
200
201
202 namespace v8 { 35 namespace v8 {
203 namespace base { 36 namespace base {
204 37
38 // CHECK dies with a fatal error if condition is not true. It is *not*
39 // controlled by DEBUG, so the check will be executed regardless of
40 // compilation mode.
41 //
42 // We make sure CHECK et al. always evaluates their arguments, as
43 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
44 #define CHECK(condition) \
45 do { \
46 if (V8_UNLIKELY(!(condition))) { \
47 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", #condition); \
48 } \
49 } while (0)
50
51
52 #ifdef DEBUG
53
54 // Helper macro for binary operators.
55 // Don't use this macro directly in your code, use CHECK_EQ et al below.
56 #define CHECK_OP(name, op, lhs, rhs) \
57 do { \
58 if (std::string* _msg = ::v8::base::Check##name##Impl( \
59 (lhs), (rhs), #lhs " " #op " " #rhs)) { \
60 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", _msg->c_str()); \
61 delete _msg; \
62 } \
63 } while (0)
64
65 #else
66
67 // Make all CHECK functions discard their log strings to reduce code
68 // bloat for official release builds.
69
70 #define CHECK_OP(name, op, lhs, rhs) CHECK((lhs)op(rhs))
71
72 #endif
73
74
75 // Build the error message string. This is separate from the "Impl"
76 // function template because it is not performance critical and so can
77 // be out of line, while the "Impl" code should be inline. Caller
78 // takes ownership of the returned string.
79 template <typename Lhs, typename Rhs>
80 std::string* MakeCheckOpString(Lhs const& lhs, Rhs const& rhs,
81 char const* msg) {
82 std::ostringstream ss;
83 ss << msg << " (" << lhs << " vs. " << rhs << ")";
84 return new std::string(ss.str());
85 }
86
87 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
88 // in logging.cc.
89 #define DEFINE_MAKE_CHECK_OP_STRING(type) \
90 extern template std::string* MakeCheckOpString<type, type>( \
91 type const&, type const&, char const*);
92 DEFINE_MAKE_CHECK_OP_STRING(int)
93 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int)
94 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int)
95 DEFINE_MAKE_CHECK_OP_STRING(unsigned int)
96 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int)
97 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int)
98 DEFINE_MAKE_CHECK_OP_STRING(char const*)
99 DEFINE_MAKE_CHECK_OP_STRING(void const*)
100 #undef DEFINE_MAKE_CHECK_OP_STRING
101
102
103 // Helper functions for CHECK_OP macro.
104 // The (int, int) specialization works around the issue that the compiler
105 // will not instantiate the template version of the function on values of
106 // unnamed enum type - see comment below.
107 // The (float, float) and (double, double) instantiations are explicitly
108 // externialized to ensure proper 32/64-bit comparisons on x86.
109 #define DEFINE_CHECK_OP_IMPL(NAME, op) \
110 template <typename Lhs, typename Rhs> \
111 V8_INLINE std::string* Check##NAME##Impl(Lhs const& lhs, Rhs const& rhs, \
112 char const* msg) { \
113 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \
114 } \
115 V8_INLINE std::string* Check##NAME##Impl(int lhs, int rhs, \
116 char const* msg) { \
117 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \
118 } \
119 extern template std::string* Check##NAME##Impl<float, float>( \
120 float const& lhs, float const& rhs, char const* msg); \
121 extern template std::string* Check##NAME##Impl<double, double>( \
122 double const& lhs, double const& rhs, char const* msg);
123 DEFINE_CHECK_OP_IMPL(EQ, ==)
124 DEFINE_CHECK_OP_IMPL(NE, !=)
125 DEFINE_CHECK_OP_IMPL(LE, <=)
126 DEFINE_CHECK_OP_IMPL(LT, < )
127 DEFINE_CHECK_OP_IMPL(GE, >=)
128 DEFINE_CHECK_OP_IMPL(GT, > )
129 #undef DEFINE_CHECK_OP_IMPL
130
131 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs)
132 #define CHECK_NE(lhs, rhs) CHECK_OP(NE, !=, lhs, rhs)
133 #define CHECK_LE(lhs, rhs) CHECK_OP(LE, <=, lhs, rhs)
134 #define CHECK_LT(lhs, rhs) CHECK_OP(LT, <, lhs, rhs)
135 #define CHECK_GE(lhs, rhs) CHECK_OP(GE, >=, lhs, rhs)
136 #define CHECK_GT(lhs, rhs) CHECK_OP(GT, >, lhs, rhs)
137 #define CHECK_NULL(val) CHECK((val) == nullptr)
138 #define CHECK_NOT_NULL(val) CHECK((val) != nullptr)
139 #define CHECK_IMPLIES(lhs, rhs) CHECK(!(lhs) || (rhs))
140
141
205 // Exposed for making debugging easier (to see where your function is being 142 // Exposed for making debugging easier (to see where your function is being
206 // called, just add a call to DumpBacktrace). 143 // called, just add a call to DumpBacktrace).
207 void DumpBacktrace(); 144 void DumpBacktrace();
208 145
209 } } // namespace v8::base 146 } // namespace base
147 } // namespace v8
210 148
211 149
212 // The DCHECK macro is equivalent to CHECK except that it only 150 // The DCHECK macro is equivalent to CHECK except that it only
213 // generates code in debug builds. 151 // generates code in debug builds.
152 // TODO(bmeurer): DCHECK_RESULT(expr) must die!
214 #ifdef DEBUG 153 #ifdef DEBUG
215 #define DCHECK_RESULT(expr) CHECK(expr) 154 #define DCHECK_RESULT(expr) CHECK(expr)
216 #define DCHECK(condition) CHECK(condition) 155 #define DCHECK(condition) CHECK(condition)
217 #define DCHECK_EQ(v1, v2) CHECK_EQ(v1, v2) 156 #define DCHECK_EQ(v1, v2) CHECK_EQ(v1, v2)
218 #define DCHECK_NE(v1, v2) CHECK_NE(v1, v2) 157 #define DCHECK_NE(v1, v2) CHECK_NE(v1, v2)
219 #define DCHECK_GE(v1, v2) CHECK_GE(v1, v2) 158 #define DCHECK_GE(v1, v2) CHECK_GE(v1, v2)
220 #define DCHECK_LT(v1, v2) CHECK_LT(v1, v2) 159 #define DCHECK_LT(v1, v2) CHECK_LT(v1, v2)
221 #define DCHECK_LE(v1, v2) CHECK_LE(v1, v2) 160 #define DCHECK_LE(v1, v2) CHECK_LE(v1, v2)
161 #define DCHECK_NULL(val) CHECK_NULL(val)
162 #define DCHECK_NOT_NULL(val) CHECK_NOT_NULL(val)
163 #define DCHECK_IMPLIES(v1, v2) CHECK_IMPLIES(v1, v2)
222 #else 164 #else
223 #define DCHECK_RESULT(expr) (expr) 165 #define DCHECK_RESULT(expr) (expr)
224 #define DCHECK(condition) ((void) 0) 166 #define DCHECK(condition) ((void) 0)
225 #define DCHECK_EQ(v1, v2) ((void) 0) 167 #define DCHECK_EQ(v1, v2) ((void) 0)
226 #define DCHECK_NE(v1, v2) ((void) 0) 168 #define DCHECK_NE(v1, v2) ((void) 0)
227 #define DCHECK_GE(v1, v2) ((void) 0) 169 #define DCHECK_GE(v1, v2) ((void) 0)
228 #define DCHECK_LT(v1, v2) ((void) 0) 170 #define DCHECK_LT(v1, v2) ((void) 0)
229 #define DCHECK_LE(v1, v2) ((void) 0) 171 #define DCHECK_LE(v1, v2) ((void) 0)
172 #define DCHECK_NULL(val) ((void) 0)
173 #define DCHECK_NOT_NULL(val) ((void) 0)
174 #define DCHECK_IMPLIES(v1, v2) ((void) 0)
230 #endif 175 #endif
231 176
232 #define DCHECK_NOT_NULL(p) DCHECK_NE(NULL, p)
233
234 #endif // V8_BASE_LOGGING_H_ 177 #endif // V8_BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « src/base/compiler-specific.h ('k') | src/base/logging.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698