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

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

Issue 2524093002: [base] Pass scalar arguments by value in CHECK/DCHECK (Closed)
Patch Set: Remove unneeded const 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 = ::v8::base::Check##name##Impl( \ 60 if (std::string* _msg = \
61 (lhs), (rhs), #lhs " " #op " " #rhs)) { \ 61 ::v8::base::Check##name##Impl<decltype(lhs), decltype(rhs)>( \
62 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", _msg->c_str()); \ 62 (lhs), (rhs), #lhs " " #op " " #rhs)) { \
63 delete _msg; \ 63 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", _msg->c_str()); \
64 } \ 64 delete _msg; \
65 } \
65 } while (0) 66 } while (0)
66 67
67 #else 68 #else
68 69
69 // Make all CHECK functions discard their log strings to reduce code 70 // Make all CHECK functions discard their log strings to reduce code
70 // bloat for official release builds. 71 // bloat for official release builds.
71 72
72 #define CHECK_OP(name, op, lhs, rhs) CHECK((lhs)op(rhs)) 73 #define CHECK_OP(name, op, lhs, rhs) CHECK((lhs)op(rhs))
73 74
74 #endif 75 #endif
75 76
77 // Helper to determine how to pass values: Pass scalars and arrays by value,
78 // others by const reference. std::decay<T> provides the type which should be
79 // used to pass T by value, e.g. converts array to pointer and removes const,
80 // volatile and reference.
81 template <typename T>
82 struct PassType : public std::conditional<
83 std::is_scalar<typename std::decay<T>::type>::value,
84 typename std::decay<T>::type, T const&> {};
Igor Sheludko 2016/11/29 14:06:05 Awesome! Does it mean that we can now move definit
76 85
77 // Build the error message string. This is separate from the "Impl" 86 // Build the error message string. This is separate from the "Impl"
78 // function template because it is not performance critical and so can 87 // function template because it is not performance critical and so can
79 // be out of line, while the "Impl" code should be inline. Caller 88 // be out of line, while the "Impl" code should be inline. Caller
80 // takes ownership of the returned string. 89 // takes ownership of the returned string.
81 template <typename Lhs, typename Rhs> 90 template <typename Lhs, typename Rhs>
82 std::string* MakeCheckOpString(Lhs const& lhs, Rhs const& rhs, 91 std::string* MakeCheckOpString(typename PassType<Lhs>::type lhs,
92 typename PassType<Rhs>::type rhs,
83 char const* msg) { 93 char const* msg) {
84 std::ostringstream ss; 94 std::ostringstream ss;
85 ss << msg << " (" << lhs << " vs. " << rhs << ")"; 95 ss << msg << " (" << lhs << " vs. " << rhs << ")";
86 return new std::string(ss.str()); 96 return new std::string(ss.str());
87 } 97 }
88 98
89 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated 99 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
90 // in logging.cc. 100 // in logging.cc.
91 #define DEFINE_MAKE_CHECK_OP_STRING(type) \ 101 #define DEFINE_MAKE_CHECK_OP_STRING(type) \
92 extern template V8_BASE_EXPORT std::string* MakeCheckOpString<type, type>( \ 102 extern template V8_BASE_EXPORT std::string* MakeCheckOpString<type, type>( \
93 type const&, type const&, char const*); 103 type, type, char const*);
94 DEFINE_MAKE_CHECK_OP_STRING(int) 104 DEFINE_MAKE_CHECK_OP_STRING(int)
95 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int) 105 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int)
96 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int) 106 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int)
97 DEFINE_MAKE_CHECK_OP_STRING(unsigned int) 107 DEFINE_MAKE_CHECK_OP_STRING(unsigned int)
98 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int) 108 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int)
99 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int) 109 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int)
100 DEFINE_MAKE_CHECK_OP_STRING(char const*) 110 DEFINE_MAKE_CHECK_OP_STRING(char const*)
101 DEFINE_MAKE_CHECK_OP_STRING(void const*) 111 DEFINE_MAKE_CHECK_OP_STRING(void const*)
102 #undef DEFINE_MAKE_CHECK_OP_STRING 112 #undef DEFINE_MAKE_CHECK_OP_STRING
103 113
104
105 // Helper functions for CHECK_OP macro. 114 // 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.
109 // The (float, float) and (double, double) instantiations are explicitly 115 // The (float, float) and (double, double) instantiations are explicitly
110 // externialized to ensure proper 32/64-bit comparisons on x86. 116 // externialized to ensure proper 32/64-bit comparisons on x86.
111 #define DEFINE_CHECK_OP_IMPL(NAME, op) \ 117 #define DEFINE_CHECK_OP_IMPL(NAME, op) \
112 template <typename Lhs, typename Rhs> \ 118 template <typename Lhs, typename Rhs> \
113 V8_INLINE std::string* Check##NAME##Impl(Lhs const& lhs, Rhs const& rhs, \ 119 V8_INLINE std::string* Check##NAME##Impl(typename PassType<Lhs>::type lhs, \
120 typename PassType<Rhs>::type rhs, \
114 char const* msg) { \ 121 char const* msg) { \
115 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \ 122 return V8_LIKELY(lhs op rhs) ? nullptr \
116 } \ 123 : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \
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); \
120 } \ 124 } \
121 extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \ 125 extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \
122 float const& lhs, float const& rhs, char const* msg); \ 126 float lhs, float rhs, char const* msg); \
123 extern template V8_BASE_EXPORT std::string* \ 127 extern template V8_BASE_EXPORT std::string* \
124 Check##NAME##Impl<double, double>(double const& lhs, double const& rhs, \ 128 Check##NAME##Impl<double, double>(double lhs, double rhs, \
125 char const* msg); 129 char const* msg);
126 DEFINE_CHECK_OP_IMPL(EQ, ==) 130 DEFINE_CHECK_OP_IMPL(EQ, ==)
127 DEFINE_CHECK_OP_IMPL(NE, !=) 131 DEFINE_CHECK_OP_IMPL(NE, !=)
128 DEFINE_CHECK_OP_IMPL(LE, <=) 132 DEFINE_CHECK_OP_IMPL(LE, <=)
129 DEFINE_CHECK_OP_IMPL(LT, < ) 133 DEFINE_CHECK_OP_IMPL(LT, < )
130 DEFINE_CHECK_OP_IMPL(GE, >=) 134 DEFINE_CHECK_OP_IMPL(GE, >=)
131 DEFINE_CHECK_OP_IMPL(GT, > ) 135 DEFINE_CHECK_OP_IMPL(GT, > )
132 #undef DEFINE_CHECK_OP_IMPL 136 #undef DEFINE_CHECK_OP_IMPL
133 137
134 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs) 138 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs)
(...skipping 30 matching lines...) Expand all
165 #define DCHECK_GT(v1, v2) ((void) 0) 169 #define DCHECK_GT(v1, v2) ((void) 0)
166 #define DCHECK_GE(v1, v2) ((void) 0) 170 #define DCHECK_GE(v1, v2) ((void) 0)
167 #define DCHECK_LT(v1, v2) ((void) 0) 171 #define DCHECK_LT(v1, v2) ((void) 0)
168 #define DCHECK_LE(v1, v2) ((void) 0) 172 #define DCHECK_LE(v1, v2) ((void) 0)
169 #define DCHECK_NULL(val) ((void) 0) 173 #define DCHECK_NULL(val) ((void) 0)
170 #define DCHECK_NOT_NULL(val) ((void) 0) 174 #define DCHECK_NOT_NULL(val) ((void) 0)
171 #define DCHECK_IMPLIES(v1, v2) ((void) 0) 175 #define DCHECK_IMPLIES(v1, v2) ((void) 0)
172 #endif 176 #endif
173 177
174 #endif // V8_BASE_LOGGING_H_ 178 #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