diff options
author | Giuliano Procida <gprocida@google.com> | 2023-06-15 16:09:23 +0100 |
---|---|---|
committer | Matthias Maennich <maennich@google.com> | 2023-06-22 22:15:48 +0100 |
commit | cdb2ff6ca3b3cf9dc60618dbc58f965be0bd071a (patch) | |
tree | a80a520f42b768ac36c825ae6782edb5f0c8d26b | |
parent | b3b692e233fbe3ad2ada8936865c95d0a20b0745 (diff) | |
download | stg-cdb2ff6ca3b3cf9dc60618dbc58f965be0bd071a.tar.gz |
error: use a wrapper object for hexadecimal output
PiperOrigin-RevId: 540584755
Change-Id: I9d08b47f61c87768f3b0dabbb3c643d791ca8a81
-rw-r--r-- | dwarf_processor.cc | 7 | ||||
-rw-r--r-- | dwarf_wrappers.cc | 2 | ||||
-rw-r--r-- | elf_reader.cc | 4 | ||||
-rw-r--r-- | error.h | 18 | ||||
-rw-r--r-- | graph.cc | 2 |
5 files changed, 25 insertions, 8 deletions
diff --git a/dwarf_processor.cc b/dwarf_processor.cc index 10642ff..0924b4f 100644 --- a/dwarf_processor.cc +++ b/dwarf_processor.cc @@ -42,7 +42,7 @@ namespace { std::string EntryToString(Entry& entry) { std::ostringstream os; - os << "DWARF entry <0x" << std::hex << entry.GetOffset() << ">"; + os << "DWARF entry <" << Hex(entry.GetOffset()) << ">"; return os.str(); } @@ -108,7 +108,7 @@ Primitive::Encoding GetEncoding(Entry& entry) { case DW_ATE_UTF: return Primitive::Encoding::UTF; default: - Die() << "Unknown encoding 0x" << std::hex << *dwarf_encoding << " for " + Die() << "Unknown encoding " << Hex(*dwarf_encoding) << " for " << EntryToString(entry); } } @@ -344,8 +344,7 @@ class Processor { void CheckUnresolvedIds() const { for (const auto& [offset, id] : id_map_) { if (!graph_.Is(id)) { - Die() << "unresolved id " << id << ", DWARF offset 0x" << std::hex - << offset; + Die() << "unresolved id " << id << ", DWARF offset " << Hex(offset); } } } diff --git a/dwarf_wrappers.cc b/dwarf_wrappers.cc index 3d66145..b9f89d2 100644 --- a/dwarf_wrappers.cc +++ b/dwarf_wrappers.cc @@ -82,7 +82,7 @@ void CheckOrDwflError(bool condition, const char* caller) { const char* errmsg = dwfl_errmsg(dwfl_error); if (errmsg == nullptr) { // There are some cases when DWFL fails to produce an error message. - Die() << caller << " returned error code 0x" << std::hex << dwfl_error; + Die() << caller << " returned error code " << Hex(dwfl_error); } Die() << caller << " returned error: " << errmsg; } diff --git a/elf_reader.cc b/elf_reader.cc index 148599d..0498acd 100644 --- a/elf_reader.cc +++ b/elf_reader.cc @@ -253,8 +253,8 @@ class Typing { // TODO: allow "compatible" duplicates, for example // "void foo(int bar)" vs "void foo(const int bar)" if (!IsEqual(symbol, other)) { - Die() << "Duplicate DWARF symbol: address=0x" << std::hex - << symbol.address << std::dec << ", name=" << symbol.name; + Die() << "Duplicate DWARF symbol: address=" << Hex(symbol.address) + << ", name=" << symbol.name; } } } @@ -21,6 +21,7 @@ #define STG_ERROR_H_ #include <exception> +#include <ios> #include <iostream> #include <optional> #include <ostream> @@ -105,6 +106,23 @@ inline std::ostream& operator<<(std::ostream& os, Error error) { return os << std::system_error(error.number, std::generic_category()).what(); } +template <typename T> +struct Hex { + explicit Hex(const T& value) : value(value) {} + const T& value; +}; + +template <typename T> Hex(const T&) -> Hex<T>; + +template <typename T> +std::ostream& operator<<(std::ostream& os, const Hex<T>& hex_value) { + // not quite right if an exception is thrown + const auto flags = os.flags(); + os << std::hex << std::showbase << hex_value.value; + os.flags(flags); + return os; +} + } // namespace stg #endif // STG_ERROR_H_ @@ -140,7 +140,7 @@ std::string VersionedSymbolName(const ElfSymbol& symbol) { } std::ostream& operator<<(std::ostream& os, ElfSymbol::CRC crc) { - return os << "0x" << std::hex << crc.number; + return os << Hex(crc.number); } std::ostream& operator<<(std::ostream& os, Primitive::Encoding encoding) { |