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)) | |
Igor Sheludko
2016/11/24 15:37:12
#undef DEFINE_SIGNED_MISMATCH_COMP
Clemens Hammacher
2016/11/24 17:06:30
Oh, sure. And #undef MAKE_UNSIGNED. Done.
| |
157 | |
118 // Helper functions for CHECK_OP macro. | 158 // Helper functions for CHECK_OP macro. |
119 // The (float, float) and (double, double) instantiations are explicitly | 159 // The (float, float) and (double, double) instantiations are explicitly |
120 // externialized to ensure proper 32/64-bit comparisons on x86. | 160 // externalized to ensure proper 32/64-bit comparisons on x86. |
161 // The Cmp##NAME##Impl function is only instantiable if one of the two types is | |
162 // not integral or their signedness matches (i.e. whenever no specialization is | |
163 // required, see above). Otherwise it is disabled by the enable_if construct, | |
164 // and the compiler will pick a specialization from above. | |
121 #define DEFINE_CHECK_OP_IMPL(NAME, op) \ | 165 #define DEFINE_CHECK_OP_IMPL(NAME, op) \ |
122 template <typename Lhs, typename Rhs> \ | 166 template <typename Lhs, typename Rhs> \ |
167 V8_INLINE \ | |
168 typename std::enable_if<!is_signed_vs_unsigned<Lhs, Rhs>::value && \ | |
169 !is_unsigned_vs_signed<Lhs, Rhs>::value, \ | |
170 bool>::type \ | |
171 Cmp##NAME##Impl(typename PassType<Lhs>::type lhs, \ | |
172 typename PassType<Rhs>::type rhs) { \ | |
173 return lhs op rhs; \ | |
174 } \ | |
175 template <typename Lhs, typename Rhs> \ | |
123 V8_INLINE std::string* Check##NAME##Impl(typename PassType<Lhs>::type lhs, \ | 176 V8_INLINE std::string* Check##NAME##Impl(typename PassType<Lhs>::type lhs, \ |
124 typename PassType<Rhs>::type rhs, \ | 177 typename PassType<Rhs>::type rhs, \ |
125 char const* msg) { \ | 178 char const* msg) { \ |
126 return V8_LIKELY(lhs op rhs) ? nullptr \ | 179 bool cmp = Cmp##NAME##Impl<Lhs, Rhs>(lhs, rhs); \ |
127 : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \ | 180 return V8_LIKELY(cmp) ? nullptr \ |
181 : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \ | |
128 } \ | 182 } \ |
129 extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \ | 183 extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \ |
130 const float lhs, const float rhs, char const* msg); \ | 184 float lhs, float rhs, char const* msg); \ |
131 extern template V8_BASE_EXPORT std::string* \ | 185 extern template V8_BASE_EXPORT std::string* \ |
132 Check##NAME##Impl<double, double>(const double lhs, const double rhs, \ | 186 Check##NAME##Impl<double, double>(double lhs, double rhs, \ |
133 char const* msg); | 187 char const* msg); |
134 DEFINE_CHECK_OP_IMPL(EQ, ==) | 188 DEFINE_CHECK_OP_IMPL(EQ, ==) |
135 DEFINE_CHECK_OP_IMPL(NE, !=) | 189 DEFINE_CHECK_OP_IMPL(NE, !=) |
136 DEFINE_CHECK_OP_IMPL(LE, <=) | 190 DEFINE_CHECK_OP_IMPL(LE, <=) |
137 DEFINE_CHECK_OP_IMPL(LT, < ) | 191 DEFINE_CHECK_OP_IMPL(LT, < ) |
138 DEFINE_CHECK_OP_IMPL(GE, >=) | 192 DEFINE_CHECK_OP_IMPL(GE, >=) |
139 DEFINE_CHECK_OP_IMPL(GT, > ) | 193 DEFINE_CHECK_OP_IMPL(GT, > ) |
140 #undef DEFINE_CHECK_OP_IMPL | 194 #undef DEFINE_CHECK_OP_IMPL |
141 | 195 |
142 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs) | 196 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs) |
(...skipping 30 matching lines...) Expand all Loading... | |
173 #define DCHECK_GT(v1, v2) ((void) 0) | 227 #define DCHECK_GT(v1, v2) ((void) 0) |
174 #define DCHECK_GE(v1, v2) ((void) 0) | 228 #define DCHECK_GE(v1, v2) ((void) 0) |
175 #define DCHECK_LT(v1, v2) ((void) 0) | 229 #define DCHECK_LT(v1, v2) ((void) 0) |
176 #define DCHECK_LE(v1, v2) ((void) 0) | 230 #define DCHECK_LE(v1, v2) ((void) 0) |
177 #define DCHECK_NULL(val) ((void) 0) | 231 #define DCHECK_NULL(val) ((void) 0) |
178 #define DCHECK_NOT_NULL(val) ((void) 0) | 232 #define DCHECK_NOT_NULL(val) ((void) 0) |
179 #define DCHECK_IMPLIES(v1, v2) ((void) 0) | 233 #define DCHECK_IMPLIES(v1, v2) ((void) 0) |
180 #endif | 234 #endif |
181 | 235 |
182 #endif // V8_BASE_LOGGING_H_ | 236 #endif // V8_BASE_LOGGING_H_ |
OLD | NEW |