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

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

Issue 2526783002: [base] Define CHECK comparison for signed vs. unsigned (Closed)
Patch Set: Rebase & refactor for windows 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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 template <typename Lhs, typename Rhs>
titzer 2016/11/24 12:57:31 This is a nasty little template knot that took me
Clemens Hammacher 2016/11/24 13:46:08 I added several comments, and the MAKE_UNSIGNED ma
119 struct is_signed_vs_unsigned {
120 enum : bool {
121 value = std::is_integral<Lhs>::value && std::is_integral<Rhs>::value &&
122 std::is_signed<Lhs>::value && std::is_unsigned<Rhs>::value
123 };
124 };
125 template <typename Lhs, typename Rhs>
126 struct is_unsigned_vs_signed : public is_signed_vs_unsigned<Rhs, Lhs> {};
127 template <typename Lhs, typename Rhs>
128 struct is_signed_unsigned_mismatch {
129 enum : bool {
130 value = std::is_integral<Lhs>::value && std::is_integral<Rhs>::value &&
131 std::is_signed<Lhs>::value != std::is_signed<Rhs>::value
132 };
133 };
134
135 // Specialize compare operators for signed vs. unsigned comparisons.
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(
143 is_signed_vs_unsigned, EQ,
144 lhs >= 0 && static_cast<typename std::make_unsigned<Lhs>::type>(lhs) == rhs)
145 DEFINE_SIGNED_MISMATCH_COMP(
146 is_signed_vs_unsigned, LT,
147 lhs < 0 || static_cast<typename std::make_unsigned<Lhs>::type>(lhs) < rhs)
148 DEFINE_SIGNED_MISMATCH_COMP(
149 is_signed_vs_unsigned, LE,
150 lhs <= 0 || static_cast<typename std::make_unsigned<Lhs>::type>(lhs) <= rhs)
151 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, NE, !CmpEQImpl(lhs, rhs))
152 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GT, !CmpLEImpl(lhs, rhs))
153 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GE, !CmpLTImpl(lhs, rhs))
154 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, EQ, CmpEQImpl(rhs, lhs))
155 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, NE, CmpNEImpl(rhs, lhs))
156 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LT, CmpGTImpl(rhs, lhs))
157 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LE, CmpGEImpl(rhs, lhs))
158 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GT, CmpLTImpl(rhs, lhs))
159 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GE, CmpLEImpl(rhs, lhs))
160
118 // Helper functions for CHECK_OP macro. 161 // Helper functions for CHECK_OP macro.
119 // The (float, float) and (double, double) instantiations are explicitly 162 // The (float, float) and (double, double) instantiations are explicitly
120 // externialized to ensure proper 32/64-bit comparisons on x86. 163 // externialized to ensure proper 32/64-bit comparisons on x86.
121 #define DEFINE_CHECK_OP_IMPL(NAME, op) \ 164 #define DEFINE_CHECK_OP_IMPL(NAME, op) \
122 template <typename Lhs, typename Rhs> \ 165 template <typename Lhs, typename Rhs> \
166 V8_INLINE \
167 typename std::enable_if<!is_signed_unsigned_mismatch<Lhs, Rhs>::value, \
titzer 2016/11/24 12:57:31 Do you really need the auxilliary is_signed_unsign
Clemens Hammacher 2016/11/24 13:46:07 Good suggestion, thanks. Done.
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> \
123 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, \
124 typename PassType<Rhs>::type rhs, \ 175 typename PassType<Rhs>::type rhs, \
125 char const* msg) { \ 176 char const* msg) { \
126 return V8_LIKELY(lhs op rhs) ? nullptr \ 177 bool cmp = Cmp##NAME##Impl<Lhs, Rhs>(lhs, rhs); \
127 : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \ 178 return V8_LIKELY(cmp) ? nullptr \
179 : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \
128 } \ 180 } \
129 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>( \
130 const float lhs, const float rhs, char const* msg); \ 182 float lhs, float rhs, char const* msg); \
131 extern template V8_BASE_EXPORT std::string* \ 183 extern template V8_BASE_EXPORT std::string* \
132 Check##NAME##Impl<double, double>(const double lhs, const double rhs, \ 184 Check##NAME##Impl<double, double>(double lhs, double rhs, \
133 char const* msg); 185 char const* msg);
134 DEFINE_CHECK_OP_IMPL(EQ, ==) 186 DEFINE_CHECK_OP_IMPL(EQ, ==)
135 DEFINE_CHECK_OP_IMPL(NE, !=) 187 DEFINE_CHECK_OP_IMPL(NE, !=)
136 DEFINE_CHECK_OP_IMPL(LE, <=) 188 DEFINE_CHECK_OP_IMPL(LE, <=)
137 DEFINE_CHECK_OP_IMPL(LT, < ) 189 DEFINE_CHECK_OP_IMPL(LT, < )
138 DEFINE_CHECK_OP_IMPL(GE, >=) 190 DEFINE_CHECK_OP_IMPL(GE, >=)
139 DEFINE_CHECK_OP_IMPL(GT, > ) 191 DEFINE_CHECK_OP_IMPL(GT, > )
140 #undef DEFINE_CHECK_OP_IMPL 192 #undef DEFINE_CHECK_OP_IMPL
141 193
142 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs) 194 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs)
(...skipping 30 matching lines...) Expand all
173 #define DCHECK_GT(v1, v2) ((void) 0) 225 #define DCHECK_GT(v1, v2) ((void) 0)
174 #define DCHECK_GE(v1, v2) ((void) 0) 226 #define DCHECK_GE(v1, v2) ((void) 0)
175 #define DCHECK_LT(v1, v2) ((void) 0) 227 #define DCHECK_LT(v1, v2) ((void) 0)
176 #define DCHECK_LE(v1, v2) ((void) 0) 228 #define DCHECK_LE(v1, v2) ((void) 0)
177 #define DCHECK_NULL(val) ((void) 0) 229 #define DCHECK_NULL(val) ((void) 0)
178 #define DCHECK_NOT_NULL(val) ((void) 0) 230 #define DCHECK_NOT_NULL(val) ((void) 0)
179 #define DCHECK_IMPLIES(v1, v2) ((void) 0) 231 #define DCHECK_IMPLIES(v1, v2) ((void) 0)
180 #endif 232 #endif
181 233
182 #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