Commit ab350c16 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

Add stream helper to output any iterable collection

{PrintCollection} can print any collection which is iterable via a
standard for-each loop in C++. The output format of {4, 7, 11} is:
[4, 7, 11]

This helper avoids a few repetitions of manually outputting such
collections.

R=titzer@chromium.org

Bug: v8:7754
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: Iaa91e5465968a029815b3aa2b35948f711956cdb
Reviewed-on: https://chromium-review.googlesource.com/1112005
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54048}
parent 265d5c24
......@@ -150,12 +150,7 @@ V8_NOINLINE static void PrintJSONArray(size_t* array, const int len) {
V8_NOINLINE static void DumpJSONArray(std::stringstream& stream, size_t* array,
const int len) {
stream << "[";
for (int i = 0; i < len; i++) {
stream << array[i];
if (i != (len - 1)) stream << ",";
}
stream << "]";
stream << PrintCollection(Vector<size_t>(array, len));
}
void ObjectStats::PrintKeyAndId(const char* key, int gc_count) {
......
......@@ -115,6 +115,23 @@ struct AsHexBytes {
ByteOrder byte_order;
};
template <typename T>
struct PrintIteratorRange {
T start;
T end;
PrintIteratorRange(T start, T end) : start(start), end(end) {}
};
// Print any collection which can be iterated via std::begin and std::end.
// {Iterator} is the common type of {std::begin} and {std::end} called on a
// {const T&}. This function is only instantiable if that type exists.
template <typename T, typename Iterator = typename std::common_type<
decltype(std::begin(std::declval<const T&>())),
decltype(std::end(std::declval<const T&>()))>::type>
PrintIteratorRange<Iterator> PrintCollection(const T& collection) {
return {std::begin(collection), std::end(collection)};
}
// Writes the given character to the output escaping everything outside of
// printable/space ASCII range. Additionally escapes '\' making escaping
// reversible.
......@@ -136,6 +153,17 @@ V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsHex& v);
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
const AsHexBytes& v);
template <typename T>
std::ostream& operator<<(std::ostream& os, const PrintIteratorRange<T>& range) {
const char* comma = "";
os << "[";
for (T it = range.start; it != range.end; ++it, comma = ", ") {
os << comma << *it;
}
os << "]";
return os;
}
} // namespace internal
} // namespace v8
......
......@@ -614,17 +614,11 @@ bool LiftoffAssembler::ValidateCacheState() const {
if (valid) return true;
std::ostringstream os;
os << "Error in LiftoffAssembler::ValidateCacheState().\n";
os << "expected: used_regs " << used_regs << ", counts [";
for (int reg = 0; reg < kAfterMaxLiftoffRegCode; ++reg) {
os << (reg == 0 ? "" : ", ") << register_use_count[reg];
}
os << "]\n";
os << "found: used_regs " << cache_state_.used_registers << ", counts [";
for (int reg = 0; reg < kAfterMaxLiftoffRegCode; ++reg) {
os << (reg == 0 ? "" : ", ") << cache_state_.register_use_count[reg];
}
os << "]\n";
os << "Use --trace-liftoff to debug.\n";
os << "expected: used_regs " << used_regs << ", counts "
<< PrintCollection(register_use_count) << "\n";
os << "found: used_regs " << cache_state_.used_registers << ", counts "
<< PrintCollection(cache_state_.register_use_count) << "\n";
os << "Use --trace-liftoff to debug.";
FATAL("%s", os.str().c_str());
}
......
......@@ -1807,11 +1807,7 @@ class LiftoffCompiler {
control_depth == -1
? asm_->cache_state()
: &decoder->control_at(control_depth)->label_state;
bool first = true;
for (LiftoffAssembler::VarState& slot : cache_state->stack_state) {
os << (first ? "" : "-") << slot;
first = false;
}
os << PrintCollection(cache_state->stack_state);
if (control_depth != -1) PrintF("; ");
}
os << "\n";
......
......@@ -18,17 +18,6 @@ namespace internal {
bool operator==(const NumberFormatSpan& lhs, const NumberFormatSpan& rhs) {
return memcmp(&lhs, &rhs, sizeof(lhs)) == 0;
}
template <typename _CharT, typename _Traits, typename T>
std::basic_ostream<_CharT, _Traits>& operator<<(
std::basic_ostream<_CharT, _Traits>& self, const std::vector<T>& v) {
self << "[";
for (auto it = v.begin(); it < v.end();) {
self << *it;
it++;
if (it < v.end()) self << ", ";
}
return self << "]";
}
template <typename _CharT, typename _Traits>
std::basic_ostream<_CharT, _Traits>& operator<<(
std::basic_ostream<_CharT, _Traits>& self, const NumberFormatSpan& part) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment