Skip to content

Commit

Permalink
Replace assert_internal::GetStreamable() with public `riegeli::Debu…
Browse files Browse the repository at this point in the history
…g()`,

extensible using `RiegeliDebug()` FTADLE.

Use it for printing of values under an assertion and to replace quoted
`absl::CHexEscape()`.

Extract some operations on `std::ostream` to `stream_utils.h`.

PiperOrigin-RevId: 693716729
  • Loading branch information
QrczakMK committed Nov 6, 2024
1 parent 23438c3 commit c8bac41
Show file tree
Hide file tree
Showing 17 changed files with 913 additions and 97 deletions.
27 changes: 27 additions & 0 deletions riegeli/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ cc_library(
],
)

cc_library(
name = "stream_utils",
srcs = ["stream_utils.cc"],
hdrs = ["stream_utils.h"],
deps = [
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings",
],
)

cc_library(
name = "debug",
srcs = ["debug.cc"],
hdrs = ["debug.h"],
deps = [
":stream_utils",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/meta:type_traits",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
],
)

cc_library(
name = "assert",
srcs = [
Expand All @@ -49,6 +74,7 @@ cc_library(
],
hdrs = ["assert.h"],
deps = [
":debug",
"@com_google_absl//absl/base:core_headers",
],
)
Expand Down Expand Up @@ -466,6 +492,7 @@ cc_library(
":memory_estimator",
":new_aligned",
":shared_ptr",
":stream_utils",
":string_utils",
":to_string_view",
"@com_google_absl//absl/base:core_headers",
Expand Down
23 changes: 2 additions & 21 deletions riegeli/base/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#define RIEGELI_BASE_ASSERT_H_

#include <stddef.h>
#include <stdint.h>

#include <ostream> // IWYU pragma: export
#include <sstream>
Expand All @@ -25,6 +24,7 @@

#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "riegeli/base/debug.h"
#include "riegeli/base/port.h"

namespace riegeli {
Expand Down Expand Up @@ -95,30 +95,11 @@ class CheckResult {
const char* message_ = nullptr;
};

// For showing a value in a failure message involving a comparison, if needed
// then converts the value to a different type with the appropriate behavior of
// `operator<<`: characters are shown as integers.

inline int GetStreamable(char value) { return value; }
inline int GetStreamable(signed char value) { return value; }
inline unsigned GetStreamable(unsigned char value) { return value; }
inline int GetStreamable(wchar_t value) { return value; }
#if __cpp_char8_t
inline unsigned GetStreamable(char8_t value) { return value; }
#endif
inline uint16_t GetStreamable(char16_t value) { return value; }
inline uint32_t GetStreamable(char32_t value) { return value; }

template <typename T>
inline const T& GetStreamable(const T& value) {
return value;
}

template <typename A, typename B>
ABSL_ATTRIBUTE_COLD const char* FormatCheckOpMessage(const char* message,
const A& a, const B& b) {
std::ostringstream stream;
stream << message << " (" << GetStreamable(a) << " vs. " << GetStreamable(b)
stream << message << " (" << riegeli::Debug(a) << " vs. " << riegeli::Debug(b)
<< ")";
// Do not bother with freeing this string: the program will soon terminate.
return (new std::string(stream.str()))->c_str();
Expand Down
29 changes: 3 additions & 26 deletions riegeli/base/chain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "riegeli/base/memory_estimator.h"
#include "riegeli/base/new_aligned.h"
#include "riegeli/base/ownership.h"
#include "riegeli/base/stream_utils.h"
#include "riegeli/base/string_utils.h"

namespace riegeli {
Expand All @@ -62,16 +63,6 @@ constexpr Chain::BlockPtrPtr Chain::BlockIterator::kEndShortData;

namespace {

void WritePadding(std::ostream& dest, size_t length) {
char buffer[64];
std::memset(buffer, dest.fill(), sizeof(buffer));
while (length > sizeof(buffer)) {
dest.write(buffer, std::streamsize{sizeof(buffer)});
length -= sizeof(buffer);
}
dest.write(buffer, IntCast<std::streamsize>(length));
}

// Stores an `absl::Cord` which must be flat, i.e.
// `src.TryFlat() != absl::nullopt`.
//
Expand Down Expand Up @@ -2112,25 +2103,11 @@ StrongOrdering Chain::Compare(const Chain& a, absl::string_view b) {
}

void Chain::Output(std::ostream& dest) const {
std::ostream::sentry sentry(dest);
if (sentry) {
size_t left_pad = 0;
size_t right_pad = 0;
if (IntCast<size_t>(dest.width()) > size()) {
const size_t pad = IntCast<size_t>(dest.width()) - size();
if ((dest.flags() & dest.adjustfield) == dest.left) {
right_pad = pad;
} else {
left_pad = pad;
}
}
if (left_pad > 0) WritePadding(dest, left_pad);
WriteWithPadding(dest, size(), [&] {
for (const absl::string_view fragment : blocks()) {
dest.write(fragment.data(), IntCast<std::streamsize>(fragment.size()));
}
if (right_pad > 0) WritePadding(dest, right_pad);
dest.width(0);
}
});
}

void Chain::VerifyInvariants() const {
Expand Down
8 changes: 8 additions & 0 deletions riegeli/base/chain_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,12 @@ class Chain : public WithCompare<Chain> {
return dest;
}

// Support `riegeli::Debug()`.
template <typename DebugStream>
friend void RiegeliDebug(const Chain& src, DebugStream& dest) {
src.Debug(dest);
}

// Support `absl::Format(&chain, format, args...)`.
friend void AbslFormatFlush(Chain* dest, absl::string_view src) {
dest->Append(src);
Expand Down Expand Up @@ -594,6 +600,8 @@ class Chain : public WithCompare<Chain> {
template <typename Sink>
void Stringify(Sink& dest) const;
void Output(std::ostream& dest) const;
template <typename DebugStream>
void Debug(DebugStream& dest) const;

BlockPtrs block_ptrs_;

Expand Down
10 changes: 10 additions & 0 deletions riegeli/base/chain_details.h
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,16 @@ void Chain::Stringify(Sink& dest) const {
for (const absl::string_view block : blocks()) dest.Append(block);
}

template <typename DebugStream>
void Chain::Debug(DebugStream& dest) const {
dest.DebugStringQuote();
typename DebugStream::EscapeState escape_state;
for (const absl::string_view fragment : blocks()) {
dest.DebugStringFragment(fragment, escape_state);
}
dest.DebugStringQuote();
}

} // namespace riegeli

#endif // RIEGELI_BASE_CHAIN_DETAILS_H_
Loading

0 comments on commit c8bac41

Please sign in to comment.