OLD | NEW |
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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 DEFINE_MAKE_CHECK_OP_STRING(int) | 108 DEFINE_MAKE_CHECK_OP_STRING(int) |
109 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int) | 109 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int) |
110 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int) | 110 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int) |
111 DEFINE_MAKE_CHECK_OP_STRING(unsigned int) | 111 DEFINE_MAKE_CHECK_OP_STRING(unsigned int) |
112 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int) | 112 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int) |
113 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int) | 113 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int) |
114 DEFINE_MAKE_CHECK_OP_STRING(char const*) | 114 DEFINE_MAKE_CHECK_OP_STRING(char const*) |
115 DEFINE_MAKE_CHECK_OP_STRING(void const*) | 115 DEFINE_MAKE_CHECK_OP_STRING(void const*) |
116 #undef DEFINE_MAKE_CHECK_OP_STRING | 116 #undef DEFINE_MAKE_CHECK_OP_STRING |
117 | 117 |
118 // is_signed_vs_unsigned::value is true if both types are integral, Lhs is | |
119 // signed, and Rhs is unsigned. False in all other cases. | |
120 template <typename Lhs, typename Rhs> | |
121 struct is_signed_vs_unsigned { | |
122 enum : bool { | |
123 value = std::is_integral<Lhs>::value && std::is_integral<Rhs>::value && | |
124 std::is_signed<Lhs>::value && std::is_unsigned<Rhs>::value | |
125 }; | |
126 }; | |
127 // Same thing, other way around: Lhs is unsigned, Rhs signed. | |
128 template <typename Lhs, typename Rhs> | |
129 struct is_unsigned_vs_signed : public is_signed_vs_unsigned<Rhs, Lhs> {}; | |
130 | |
131 // Specialize the compare functions for signed vs. unsigned comparisons. | |
132 // std::enable_if ensures that this template is only instantiable if both Lhs | |
133 // and Rhs are integral types, and their signedness does not match. | |
134 #define MAKE_UNSIGNED(Type, value) \ | |
135 static_cast<typename std::make_unsigned<Type>::type>(value) | |
136 #define DEFINE_SIGNED_MISMATCH_COMP(CHECK, NAME, IMPL) \ | |
137 template <typename Lhs, typename Rhs> \ | |
138 V8_INLINE typename std::enable_if<CHECK<Lhs, Rhs>::value, bool>::type \ | |
139 Cmp##NAME##Impl(Lhs const& lhs, Rhs const& rhs) { \ | |
140 return IMPL; \ | |
141 } | |
142 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, EQ, | |
143 lhs >= 0 && MAKE_UNSIGNED(Lhs, lhs) == rhs) | |
144 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, LT, | |
145 lhs < 0 || MAKE_UNSIGNED(Lhs, lhs) < rhs) | |
146 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, LE, | |
147 lhs <= 0 || MAKE_UNSIGNED(Lhs, lhs) <= rhs) | |
148 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, NE, !CmpEQImpl(lhs, rhs)) | |
149 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GT, !CmpLEImpl(lhs, rhs)) | |
150 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GE, !CmpLTImpl(lhs, rhs)) | |
151 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, EQ, CmpEQImpl(rhs, lhs)) | |
152 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, NE, CmpNEImpl(rhs, lhs)) | |
153 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LT, CmpGTImpl(rhs, lhs)) | |
154 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LE, CmpGEImpl(rhs, lhs)) | |
155 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GT, CmpLTImpl(rhs, lhs)) | |
156 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GE, CmpLEImpl(rhs, lhs)) | |
157 #undef MAKE_UNSIGNED | |
158 #undef DEFINE_SIGNED_MISMATCH_COMP | |
159 | |
160 // Helper functions for CHECK_OP macro. | 118 // Helper functions for CHECK_OP macro. |
161 // The (float, float) and (double, double) instantiations are explicitly | 119 // The (float, float) and (double, double) instantiations are explicitly |
162 // externalized to ensure proper 32/64-bit comparisons on x86. | 120 // externialized to ensure proper 32/64-bit comparisons on x86. |
163 // The Cmp##NAME##Impl function is only instantiable if one of the two types is | |
164 // not integral or their signedness matches (i.e. whenever no specialization is | |
165 // required, see above). Otherwise it is disabled by the enable_if construct, | |
166 // and the compiler will pick a specialization from above. | |
167 #define DEFINE_CHECK_OP_IMPL(NAME, op) \ | 121 #define DEFINE_CHECK_OP_IMPL(NAME, op) \ |
168 template <typename Lhs, typename Rhs> \ | 122 template <typename Lhs, typename Rhs> \ |
169 V8_INLINE \ | |
170 typename std::enable_if<!is_signed_vs_unsigned<Lhs, Rhs>::value && \ | |
171 !is_unsigned_vs_signed<Lhs, Rhs>::value, \ | |
172 bool>::type \ | |
173 Cmp##NAME##Impl(typename PassType<Lhs>::type lhs, \ | |
174 typename PassType<Rhs>::type rhs) { \ | |
175 return lhs op rhs; \ | |
176 } \ | |
177 template <typename Lhs, typename Rhs> \ | |
178 V8_INLINE std::string* Check##NAME##Impl(typename PassType<Lhs>::type lhs, \ | 123 V8_INLINE std::string* Check##NAME##Impl(typename PassType<Lhs>::type lhs, \ |
179 typename PassType<Rhs>::type rhs, \ | 124 typename PassType<Rhs>::type rhs, \ |
180 char const* msg) { \ | 125 char const* msg) { \ |
181 bool cmp = Cmp##NAME##Impl<Lhs, Rhs>(lhs, rhs); \ | 126 return V8_LIKELY(lhs op rhs) ? nullptr \ |
182 return V8_LIKELY(cmp) ? nullptr \ | 127 : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \ |
183 : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \ | |
184 } \ | 128 } \ |
185 extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \ | 129 extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \ |
186 float lhs, float rhs, char const* msg); \ | 130 const float lhs, const float rhs, char const* msg); \ |
187 extern template V8_BASE_EXPORT std::string* \ | 131 extern template V8_BASE_EXPORT std::string* \ |
188 Check##NAME##Impl<double, double>(double lhs, double rhs, \ | 132 Check##NAME##Impl<double, double>(const double lhs, const double rhs, \ |
189 char const* msg); | 133 char const* msg); |
190 DEFINE_CHECK_OP_IMPL(EQ, ==) | 134 DEFINE_CHECK_OP_IMPL(EQ, ==) |
191 DEFINE_CHECK_OP_IMPL(NE, !=) | 135 DEFINE_CHECK_OP_IMPL(NE, !=) |
192 DEFINE_CHECK_OP_IMPL(LE, <=) | 136 DEFINE_CHECK_OP_IMPL(LE, <=) |
193 DEFINE_CHECK_OP_IMPL(LT, < ) | 137 DEFINE_CHECK_OP_IMPL(LT, < ) |
194 DEFINE_CHECK_OP_IMPL(GE, >=) | 138 DEFINE_CHECK_OP_IMPL(GE, >=) |
195 DEFINE_CHECK_OP_IMPL(GT, > ) | 139 DEFINE_CHECK_OP_IMPL(GT, > ) |
196 #undef DEFINE_CHECK_OP_IMPL | 140 #undef DEFINE_CHECK_OP_IMPL |
197 | 141 |
198 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs) | 142 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs) |
(...skipping 30 matching lines...) Expand all Loading... |
229 #define DCHECK_GT(v1, v2) ((void) 0) | 173 #define DCHECK_GT(v1, v2) ((void) 0) |
230 #define DCHECK_GE(v1, v2) ((void) 0) | 174 #define DCHECK_GE(v1, v2) ((void) 0) |
231 #define DCHECK_LT(v1, v2) ((void) 0) | 175 #define DCHECK_LT(v1, v2) ((void) 0) |
232 #define DCHECK_LE(v1, v2) ((void) 0) | 176 #define DCHECK_LE(v1, v2) ((void) 0) |
233 #define DCHECK_NULL(val) ((void) 0) | 177 #define DCHECK_NULL(val) ((void) 0) |
234 #define DCHECK_NOT_NULL(val) ((void) 0) | 178 #define DCHECK_NOT_NULL(val) ((void) 0) |
235 #define DCHECK_IMPLIES(v1, v2) ((void) 0) | 179 #define DCHECK_IMPLIES(v1, v2) ((void) 0) |
236 #endif | 180 #endif |
237 | 181 |
238 #endif // V8_BASE_LOGGING_H_ | 182 #endif // V8_BASE_LOGGING_H_ |
OLD | NEW |