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

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

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

Powered by Google App Engine
This is Rietveld 408576698