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

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

Issue 2527883004: Revert of [base] Pass scalar arguments by value in CHECK/DCHECK (Closed)
Patch Set: Created 4 years 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 | « no previous file | 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 <cstring> 8 #include <cstring>
9 #include <sstream> 9 #include <sstream>
10 #include <string> 10 #include <string>
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 if (V8_UNLIKELY(!(condition))) { \ 48 if (V8_UNLIKELY(!(condition))) { \
49 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", #condition); \ 49 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", #condition); \
50 } \ 50 } \
51 } while (0) 51 } while (0)
52 52
53 53
54 #ifdef DEBUG 54 #ifdef DEBUG
55 55
56 // Helper macro for binary operators. 56 // Helper macro for binary operators.
57 // Don't use this macro directly in your code, use CHECK_EQ et al below. 57 // Don't use this macro directly in your code, use CHECK_EQ et al below.
58 #define CHECK_OP(name, op, lhs, rhs) \ 58 #define CHECK_OP(name, op, lhs, rhs) \
59 do { \ 59 do { \
60 if (std::string* _msg = \ 60 if (std::string* _msg = ::v8::base::Check##name##Impl( \
61 ::v8::base::Check##name##Impl<decltype(lhs), decltype(rhs)>( \ 61 (lhs), (rhs), #lhs " " #op " " #rhs)) { \
62 (lhs), (rhs), #lhs " " #op " " #rhs)) { \ 62 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", _msg->c_str()); \
63 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", _msg->c_str()); \ 63 delete _msg; \
64 delete _msg; \ 64 } \
65 } \
66 } while (0) 65 } while (0)
67 66
68 #else 67 #else
69 68
70 // Make all CHECK functions discard their log strings to reduce code 69 // Make all CHECK functions discard their log strings to reduce code
71 // bloat for official release builds. 70 // bloat for official release builds.
72 71
73 #define CHECK_OP(name, op, lhs, rhs) CHECK((lhs)op(rhs)) 72 #define CHECK_OP(name, op, lhs, rhs) CHECK((lhs)op(rhs))
74 73
75 #endif 74 #endif
76 75
77 // Helpers to determine how to pass values: Pass scalars by value, others by
78 // const reference.
79 template <typename Lhs, typename Rhs>
80 struct PassByValue {
81 enum : bool {
82 value = (std::is_scalar<Lhs>::value || std::is_array<Lhs>::value) &&
83 (std::is_scalar<Rhs>::value || std::is_array<Rhs>::value)
84 };
85 };
86 template <typename T>
87 struct PassType
88 : public std::conditional<PassByValue<T, T>::value, T, T const&> {};
89 76
90 // Build the error message string. This is separate from the "Impl" 77 // Build the error message string. This is separate from the "Impl"
91 // function template because it is not performance critical and so can 78 // function template because it is not performance critical and so can
92 // be out of line, while the "Impl" code should be inline. Caller 79 // be out of line, while the "Impl" code should be inline. Caller
93 // takes ownership of the returned string. 80 // takes ownership of the returned string.
94 template <typename Lhs, typename Rhs> 81 template <typename Lhs, typename Rhs>
95 std::string* MakeCheckOpString(typename PassType<Lhs>::type lhs, 82 std::string* MakeCheckOpString(Lhs const& lhs, Rhs const& rhs,
96 typename PassType<Rhs>::type rhs,
97 char const* msg) { 83 char const* msg) {
98 std::ostringstream ss; 84 std::ostringstream ss;
99 ss << msg << " (" << lhs << " vs. " << rhs << ")"; 85 ss << msg << " (" << lhs << " vs. " << rhs << ")";
100 return new std::string(ss.str()); 86 return new std::string(ss.str());
101 } 87 }
102 88
103 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated 89 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
104 // in logging.cc. 90 // in logging.cc.
105 #define DEFINE_MAKE_CHECK_OP_STRING(type) \ 91 #define DEFINE_MAKE_CHECK_OP_STRING(type) \
106 extern template V8_BASE_EXPORT std::string* MakeCheckOpString<type, type>( \ 92 extern template V8_BASE_EXPORT std::string* MakeCheckOpString<type, type>( \
107 type, type, char const*); 93 type const&, type const&, char const*);
108 DEFINE_MAKE_CHECK_OP_STRING(int) 94 DEFINE_MAKE_CHECK_OP_STRING(int)
109 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int) 95 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int)
110 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int) 96 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int)
111 DEFINE_MAKE_CHECK_OP_STRING(unsigned int) 97 DEFINE_MAKE_CHECK_OP_STRING(unsigned int)
112 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int) 98 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int)
113 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int) 99 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int)
114 DEFINE_MAKE_CHECK_OP_STRING(char const*) 100 DEFINE_MAKE_CHECK_OP_STRING(char const*)
115 DEFINE_MAKE_CHECK_OP_STRING(void const*) 101 DEFINE_MAKE_CHECK_OP_STRING(void const*)
116 #undef DEFINE_MAKE_CHECK_OP_STRING 102 #undef DEFINE_MAKE_CHECK_OP_STRING
117 103
104
118 // Helper functions for CHECK_OP macro. 105 // Helper functions for CHECK_OP macro.
106 // The (int, int) specialization works around the issue that the compiler
107 // will not instantiate the template version of the function on values of
108 // unnamed enum type - see comment below.
119 // The (float, float) and (double, double) instantiations are explicitly 109 // The (float, float) and (double, double) instantiations are explicitly
120 // externialized to ensure proper 32/64-bit comparisons on x86. 110 // externialized to ensure proper 32/64-bit comparisons on x86.
121 #define DEFINE_CHECK_OP_IMPL(NAME, op) \ 111 #define DEFINE_CHECK_OP_IMPL(NAME, op) \
122 template <typename Lhs, typename Rhs> \ 112 template <typename Lhs, typename Rhs> \
123 V8_INLINE std::string* Check##NAME##Impl(typename PassType<Lhs>::type lhs, \ 113 V8_INLINE std::string* Check##NAME##Impl(Lhs const& lhs, Rhs const& rhs, \
124 typename PassType<Rhs>::type rhs, \
125 char const* msg) { \ 114 char const* msg) { \
126 return V8_LIKELY(lhs op rhs) ? nullptr \ 115 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \
127 : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \ 116 } \
117 V8_INLINE std::string* Check##NAME##Impl(int lhs, int rhs, \
118 char const* msg) { \
119 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \
128 } \ 120 } \
129 extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \ 121 extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \
130 const float lhs, const float rhs, char const* msg); \ 122 float const& lhs, float const& rhs, char const* msg); \
131 extern template V8_BASE_EXPORT std::string* \ 123 extern template V8_BASE_EXPORT std::string* \
132 Check##NAME##Impl<double, double>(const double lhs, const double rhs, \ 124 Check##NAME##Impl<double, double>(double const& lhs, double const& rhs, \
133 char const* msg); 125 char const* msg);
134 DEFINE_CHECK_OP_IMPL(EQ, ==) 126 DEFINE_CHECK_OP_IMPL(EQ, ==)
135 DEFINE_CHECK_OP_IMPL(NE, !=) 127 DEFINE_CHECK_OP_IMPL(NE, !=)
136 DEFINE_CHECK_OP_IMPL(LE, <=) 128 DEFINE_CHECK_OP_IMPL(LE, <=)
137 DEFINE_CHECK_OP_IMPL(LT, < ) 129 DEFINE_CHECK_OP_IMPL(LT, < )
138 DEFINE_CHECK_OP_IMPL(GE, >=) 130 DEFINE_CHECK_OP_IMPL(GE, >=)
139 DEFINE_CHECK_OP_IMPL(GT, > ) 131 DEFINE_CHECK_OP_IMPL(GT, > )
140 #undef DEFINE_CHECK_OP_IMPL 132 #undef DEFINE_CHECK_OP_IMPL
141 133
142 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs) 134 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs)
(...skipping 30 matching lines...) Expand all
173 #define DCHECK_GT(v1, v2) ((void) 0) 165 #define DCHECK_GT(v1, v2) ((void) 0)
174 #define DCHECK_GE(v1, v2) ((void) 0) 166 #define DCHECK_GE(v1, v2) ((void) 0)
175 #define DCHECK_LT(v1, v2) ((void) 0) 167 #define DCHECK_LT(v1, v2) ((void) 0)
176 #define DCHECK_LE(v1, v2) ((void) 0) 168 #define DCHECK_LE(v1, v2) ((void) 0)
177 #define DCHECK_NULL(val) ((void) 0) 169 #define DCHECK_NULL(val) ((void) 0)
178 #define DCHECK_NOT_NULL(val) ((void) 0) 170 #define DCHECK_NOT_NULL(val) ((void) 0)
179 #define DCHECK_IMPLIES(v1, v2) ((void) 0) 171 #define DCHECK_IMPLIES(v1, v2) ((void) 0)
180 #endif 172 #endif
181 173
182 #endif // V8_BASE_LOGGING_H_ 174 #endif // V8_BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « no previous file | src/base/logging.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698