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 3 // found in the LICENSE file. 4 5 #ifndef V8_BASE_LOGGING_H_ 6 #define V8_BASE_LOGGING_H_ 7 8 #include <cstring> 9 #include <sstream> 10 #include <string> 11 12 #include "src/base/base-export.h" 13 #include "src/base/build_config.h" 14 #include "src/base/compiler-specific.h" 15 16 extern "C" PRINTF_FORMAT(3, 4) V8_NORETURN V8_BASE_EXPORT 17 void V8_Fatal(const char* file, int line, const char* format, ...); 18 19 // The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during 20 // development, but they should not be relied on in the final product. 21 #ifdef DEBUG 22 #define FATAL(msg) \ 23 V8_Fatal(__FILE__, __LINE__, "%s", (msg)) 24 #define UNIMPLEMENTED() \ 25 V8_Fatal(__FILE__, __LINE__, "unimplemented code") 26 #define UNREACHABLE() \ 27 V8_Fatal(__FILE__, __LINE__, "unreachable code") 28 #else 29 #define FATAL(msg) \ 30 V8_Fatal("", 0, "%s", (msg)) 31 #define UNIMPLEMENTED() \ 32 V8_Fatal("", 0, "unimplemented code") 33 #define UNREACHABLE() V8_Fatal("", 0, "unreachable code") 34 #endif 35 36 37 namespace v8 { 38 namespace base { 39 40 // CHECK dies with a fatal error if condition is not true. It is *not* 41 // controlled by DEBUG, so the check will be executed regardless of 42 // compilation mode. 43 // 44 // We make sure CHECK et al. always evaluates their arguments, as 45 // doing CHECK(FunctionWithSideEffect()) is a common idiom. 46 #define CHECK_WITH_MSG(condition, message) \ 47 do { \ 48 if (V8_UNLIKELY(!(condition))) { \ 49 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", message); \ 50 } \ 51 } while (0) 52 #define CHECK(condition) CHECK_WITH_MSG(condition, #condition) 53 54 #ifdef DEBUG 55 56 // Helper macro for binary operators. 57 // Don't use this macro directly in your code, use CHECK_EQ et al below. 58 #define CHECK_OP(name, op, lhs, rhs) \ 59 do { \ 60 if (std::string* _msg = \ 61 ::v8::base::Check##name##Impl<decltype(lhs), decltype(rhs)>( \ 62 (lhs), (rhs), #lhs " " #op " " #rhs)) { \ 63 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", _msg->c_str()); \ 64 delete _msg; \ 65 } \ 66 } while (0) 67 68 #else 69 70 // Make all CHECK functions discard their log strings to reduce code 71 // bloat for official release builds. 72 73 #define CHECK_OP(name, op, lhs, rhs) \ 74 do { \ 75 bool _cmp = \ 76 ::v8::base::Cmp##name##Impl<decltype(lhs), decltype(rhs)>(lhs, rhs); \ 77 CHECK_WITH_MSG(_cmp, #lhs " " #op " " #rhs); \ 78 } while (0) 79 80 #endif 81 82 // Helper to determine how to pass values: Pass scalars and arrays by value, 83 // others by const reference. std::decay<T> provides the type which should be 84 // used to pass T by value, e.g. converts array to pointer and removes const, 85 // volatile and reference. 86 template <typename T> 87 struct PassType : public std::conditional< 88 std::is_scalar<typename std::decay<T>::type>::value, 89 typename std::decay<T>::type, T const&> {}; 90 91 // Build the error message string. This is separate from the "Impl" 92 // function template because it is not performance critical and so can 93 // be out of line, while the "Impl" code should be inline. Caller 94 // takes ownership of the returned string. 95 template <typename Lhs, typename Rhs> 96 std::string* MakeCheckOpString(typename PassType<Lhs>::type lhs, 97 typename PassType<Rhs>::type rhs, 98 char const* msg) { 99 std::ostringstream ss; 100 ss << msg << " (" << lhs << " vs. " << rhs << ")"; 101 return new std::string(ss.str()); 102 } 103 104 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated 105 // in logging.cc. 106 #define DEFINE_MAKE_CHECK_OP_STRING(type) \ 107 extern template V8_BASE_EXPORT std::string* MakeCheckOpString<type, type>( \ 108 type, type, char const*); 109 DEFINE_MAKE_CHECK_OP_STRING(int) 110 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int) 111 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int) 112 DEFINE_MAKE_CHECK_OP_STRING(unsigned int) 113 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int) 114 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int) 115 DEFINE_MAKE_CHECK_OP_STRING(char const*) 116 DEFINE_MAKE_CHECK_OP_STRING(void const*) 117 #undef DEFINE_MAKE_CHECK_OP_STRING 118 119 // is_signed_vs_unsigned::value is true if both types are integral, Lhs is 120 // signed, and Rhs is unsigned. False in all other cases. 121 template <typename Lhs, typename Rhs> 122 struct is_signed_vs_unsigned { 123 enum : bool { 124 value = std::is_integral<Lhs>::value && std::is_integral<Rhs>::value && 125 std::is_signed<Lhs>::value && std::is_unsigned<Rhs>::value 126 }; 127 }; 128 // Same thing, other way around: Lhs is unsigned, Rhs signed. 129 template <typename Lhs, typename Rhs> 130 struct is_unsigned_vs_signed : public is_signed_vs_unsigned<Rhs, Lhs> {}; 131 132 // Specialize the compare functions for signed vs. unsigned comparisons. 133 // std::enable_if ensures that this template is only instantiable if both Lhs 134 // and Rhs are integral types, and their signedness does not match. 135 #define MAKE_UNSIGNED(Type, value) \ 136 static_cast<typename std::make_unsigned<Type>::type>(value) 137 #define DEFINE_SIGNED_MISMATCH_COMP(CHECK, NAME, IMPL) \ 138 template <typename Lhs, typename Rhs> \ 139 V8_INLINE typename std::enable_if<CHECK<Lhs, Rhs>::value, bool>::type \ 140 Cmp##NAME##Impl(Lhs const& lhs, Rhs const& rhs) { \ 141 return IMPL; \ 142 } 143 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, EQ, 144 lhs >= 0 && MAKE_UNSIGNED(Lhs, lhs) == rhs) 145 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, LT, 146 lhs < 0 || MAKE_UNSIGNED(Lhs, lhs) < rhs) 147 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, LE, 148 lhs <= 0 || MAKE_UNSIGNED(Lhs, lhs) <= rhs) 149 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, NE, !CmpEQImpl(lhs, rhs)) 150 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GT, !CmpLEImpl(lhs, rhs)) 151 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GE, !CmpLTImpl(lhs, rhs)) 152 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, EQ, CmpEQImpl(rhs, lhs)) 153 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, NE, CmpNEImpl(rhs, lhs)) 154 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LT, CmpGTImpl(rhs, lhs)) 155 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LE, CmpGEImpl(rhs, lhs)) 156 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GT, CmpLTImpl(rhs, lhs)) 157 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GE, CmpLEImpl(rhs, lhs)) 158 #undef MAKE_UNSIGNED 159 #undef DEFINE_SIGNED_MISMATCH_COMP 160 161 // Helper functions for CHECK_OP macro. 162 // The (float, float) and (double, double) instantiations are explicitly 163 // externalized to ensure proper 32/64-bit comparisons on x86. 164 // The Cmp##NAME##Impl function is only instantiable if one of the two types is 165 // not integral or their signedness matches (i.e. whenever no specialization is 166 // required, see above). Otherwise it is disabled by the enable_if construct, 167 // and the compiler will pick a specialization from above. 168 #define DEFINE_CHECK_OP_IMPL(NAME, op) \ 169 template <typename Lhs, typename Rhs> \ 170 V8_INLINE \ 171 typename std::enable_if<!is_signed_vs_unsigned<Lhs, Rhs>::value && \ 172 !is_unsigned_vs_signed<Lhs, Rhs>::value, \ 173 bool>::type \ 174 Cmp##NAME##Impl(typename PassType<Lhs>::type lhs, \ 175 typename PassType<Rhs>::type rhs) { \ 176 return lhs op rhs; \ 177 } \ 178 template <typename Lhs, typename Rhs> \ 179 V8_INLINE std::string* Check##NAME##Impl(typename PassType<Lhs>::type lhs, \ 180 typename PassType<Rhs>::type rhs, \ 181 char const* msg) { \ 182 bool cmp = Cmp##NAME##Impl<Lhs, Rhs>(lhs, rhs); \ 183 return V8_LIKELY(cmp) ? nullptr \ 184 : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \ 185 } \ 186 extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \ 187 float lhs, float rhs, char const* msg); \ 188 extern template V8_BASE_EXPORT std::string* \ 189 Check##NAME##Impl<double, double>(double lhs, double rhs, \ 190 char const* msg); 191 DEFINE_CHECK_OP_IMPL(EQ, ==) 192 DEFINE_CHECK_OP_IMPL(NE, !=) 193 DEFINE_CHECK_OP_IMPL(LE, <=) 194 DEFINE_CHECK_OP_IMPL(LT, < ) 195 DEFINE_CHECK_OP_IMPL(GE, >=) 196 DEFINE_CHECK_OP_IMPL(GT, > ) 197 #undef DEFINE_CHECK_OP_IMPL 198 199 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs) 200 #define CHECK_NE(lhs, rhs) CHECK_OP(NE, !=, lhs, rhs) 201 #define CHECK_LE(lhs, rhs) CHECK_OP(LE, <=, lhs, rhs) 202 #define CHECK_LT(lhs, rhs) CHECK_OP(LT, <, lhs, rhs) 203 #define CHECK_GE(lhs, rhs) CHECK_OP(GE, >=, lhs, rhs) 204 #define CHECK_GT(lhs, rhs) CHECK_OP(GT, >, lhs, rhs) 205 #define CHECK_NULL(val) CHECK((val) == nullptr) 206 #define CHECK_NOT_NULL(val) CHECK((val) != nullptr) 207 #define CHECK_IMPLIES(lhs, rhs) \ 208 CHECK_WITH_MSG(!(lhs) || (rhs), #lhs " implies " #rhs) 209 210 } // namespace base 211 } // namespace v8 212 213 214 // The DCHECK macro is equivalent to CHECK except that it only 215 // generates code in debug builds. 216 #ifdef DEBUG 217 #define DCHECK(condition) CHECK(condition) 218 #define DCHECK_EQ(v1, v2) CHECK_EQ(v1, v2) 219 #define DCHECK_NE(v1, v2) CHECK_NE(v1, v2) 220 #define DCHECK_GT(v1, v2) CHECK_GT(v1, v2) 221 #define DCHECK_GE(v1, v2) CHECK_GE(v1, v2) 222 #define DCHECK_LT(v1, v2) CHECK_LT(v1, v2) 223 #define DCHECK_LE(v1, v2) CHECK_LE(v1, v2) 224 #define DCHECK_NULL(val) CHECK_NULL(val) 225 #define DCHECK_NOT_NULL(val) CHECK_NOT_NULL(val) 226 #define DCHECK_IMPLIES(v1, v2) CHECK_IMPLIES(v1, v2) 227 #else 228 #define DCHECK(condition) ((void) 0) 229 #define DCHECK_EQ(v1, v2) ((void) 0) 230 #define DCHECK_NE(v1, v2) ((void) 0) 231 #define DCHECK_GT(v1, v2) ((void) 0) 232 #define DCHECK_GE(v1, v2) ((void) 0) 233 #define DCHECK_LT(v1, v2) ((void) 0) 234 #define DCHECK_LE(v1, v2) ((void) 0) 235 #define DCHECK_NULL(val) ((void) 0) 236 #define DCHECK_NOT_NULL(val) ((void) 0) 237 #define DCHECK_IMPLIES(v1, v2) ((void) 0) 238 #endif 239 240 #endif // V8_BASE_LOGGING_H_ 241