From: Petr Machata Date: Wed, 4 Nov 2009 13:42:29 +0000 (+0100) Subject: dwarflint: More consolidation of message formatting code X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9a1662e857607a33d3b5b456a238b00de8ed2544;p=thirdparty%2Felfutils.git dwarflint: More consolidation of message formatting code --- diff --git a/src/Makefile.am b/src/Makefile.am index 1a7d10df2..0de9cef56 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -85,6 +85,7 @@ dwarflint_SOURCES = dwarfstrings.c \ dwarflint/expected-at.cc dwarflint/expected.hh \ dwarflint/coverage.cc dwarflint/coverage.h \ dwarflint/readctx.c dwarflint/readctx.h \ + dwarflint/pri.cc dwarflint/pri.h \ dwarflint/messages.cc dwarflint/messages.h \ dwarflint/where.c dwarflint/where.h \ dwarflint/config.cc dwarflint/config.h \ diff --git a/src/dwarflint/check_debug_line.cc b/src/dwarflint/check_debug_line.cc index 1932919fe..838bf11cb 100644 --- a/src/dwarflint/check_debug_line.cc +++ b/src/dwarflint/check_debug_line.cc @@ -3,6 +3,7 @@ #include #include "../libdw/known-dwarf.h" #include "dwarfstrings.h" +#include "pri.hh" #include diff --git a/src/dwarflint/check_dups_abstract_origin.cc b/src/dwarflint/check_dups_abstract_origin.cc index 867779ee2..5c3b3218e 100644 --- a/src/dwarflint/check_dups_abstract_origin.cc +++ b/src/dwarflint/check_dups_abstract_origin.cc @@ -37,6 +37,7 @@ #include "checks-high.hh" #include "dwarfstrings.h" #include "all-dies-it.hh" +#include "pri.hh" #include using elfutils::dwarf; diff --git a/src/dwarflint/check_range_out_of_scope.cc b/src/dwarflint/check_range_out_of_scope.cc index 9dcdb37a3..a1d7d577e 100644 --- a/src/dwarflint/check_range_out_of_scope.cc +++ b/src/dwarflint/check_range_out_of_scope.cc @@ -4,7 +4,7 @@ #include "checks-high.hh" #include "coverage.hh" -#include "dwarfstrings.h" +#include "pri.hh" using elfutils::dwarf; @@ -176,9 +176,8 @@ check_range_out_of_scope::check_range_out_of_scope (dwarflint &lint) { runoff = true; wr_error (wh) - << ": attribute `" - << dwarf_attr_string ((*at).first) - << "': PC range " << range_fmt (start, end) + << ": attribute `" << pri::attr ((*at).first) + << "': PC range " << pri::range (start, end) << " outside containing scope." << std::endl; } } diff --git a/src/dwarflint/checks-low.cc b/src/dwarflint/checks-low.cc index 411fc487b..778a0e787 100644 --- a/src/dwarflint/checks-low.cc +++ b/src/dwarflint/checks-low.cc @@ -30,6 +30,7 @@ #include "checks-low.hh" #include "low.h" #include "config.h" +#include "pri.hh" #include #include #include @@ -113,7 +114,7 @@ elf_file_init (struct elf_file *file, Elf *elf) if (shdr == NULL) { invalid_elf: - wr_error (NULL, "Broken ELF.\n"); + wr_error () << "Broken ELF." << std::endl; return false; } @@ -123,14 +124,10 @@ elf_file_init (struct elf_file *file, Elf *elf) goto invalid_elf; if (!address_aligned (shdr->sh_addr, shdr->sh_addralign)) - { - std::ostringstream s; - s << "Base address of section " << scnname << ", " - << "0x" << std::hex << shdr->sh_addr - << ", should have an alignment of " - << std::dec << shdr->sh_addralign; - wr_error (NULL, "%s\n", s.str ().c_str ()); - } + wr_error () + << "Base address of section " << scnname << ", " + << pri::addr (shdr->sh_addr) << ", should have an alignment of " + << shdr->sh_addralign << std::endl; secentry *entry = secinfo.get (scnname); cursec->scn = scn; @@ -142,7 +139,8 @@ elf_file_init (struct elf_file *file, Elf *elf) if (entry != NULL) { if (unlikely (entry->secndx != 0)) - wr_error (NULL, "Multiple occurrences of section %s.\n", scnname); + wr_error () + << "Multiple occurrences of section " << scnname << std::endl; else { /* Haven't seen a section of that name yet. */ @@ -178,18 +176,19 @@ elf_file_init (struct elf_file *file, Elf *elf) if (relocated != NULL) { if (relocated->reldata != NULL) - wr_error (NULL, - "Several relocation sections for debug section %s." - " Ignoring %s.\n", - relocated_scnname, scnname); + wr_error () + << "Several relocation sections for debug section " + << relocated_scnname << ". Ignoring " << scnname + << "." << std::endl; else { relocated->reldata = elf_getdata (scn, NULL); if (unlikely (relocated->reldata == NULL || relocated->reldata->d_buf == NULL)) { - wr_error (NULL, - "Data-less relocation section %s.\n", scnname); + wr_error () + << "Data-less relocation section " << scnname + << "." << std::endl; relocated->reldata = NULL; } else @@ -199,8 +198,9 @@ elf_file_init (struct elf_file *file, Elf *elf) if (reloc_symtab == NULL) reloc_symtab = symtab_scn; else if (reloc_symtab != symtab_scn) - wr_error (NULL, - "Relocation sections use multiple symbol tables.\n"); + wr_error () + << "Relocation sections use multiple symbol tables." + << std::endl; } } } @@ -218,8 +218,7 @@ elf_file_init (struct elf_file *file, Elf *elf) if (reloc_symdata == NULL) /* Not a show stopper, we can check a lot of stuff even without a symbol table. */ - wr_error (NULL, - "Couldn't obtain symtab data.\n"); + wr_error () << "Couldn't obtain symtab data." << std::endl; } /* Check relocation sections that we've got. */ @@ -231,11 +230,9 @@ elf_file_init (struct elf_file *file, Elf *elf) struct sec *sec = file->sec + cur->secndx; sec->rel.type = cur->reltype; if (sec->data == NULL) - { - where wh = WHERE (sec->id, NULL); - wr_error (&wh, - ": this data-less section has a relocation section.\n"); - } + wr_error (WHERE (sec->id, NULL)) + << "this data-less section has a relocation section." + << std::endl; else if (read_rel (file, sec, cur->reldata, file->addr_64)) sec->rel.symdata = reloc_symdata; } @@ -243,11 +240,9 @@ elf_file_init (struct elf_file *file, Elf *elf) if (secentry *str = secinfo.get (".debug_str")) if (str->reldata != NULL) - { - where wh = WHERE (sec_str, NULL); - wr_message (mc_impact_2 | mc_elf, &wh, - ": there's a relocation section associated with this section.\n"); - } + wr_message (WHERE (sec_str, NULL), cat (mc_impact_2, mc_elf)) + << "there's a relocation section associated with this section." + << std::endl; } return true; @@ -312,7 +307,7 @@ section_base::get_sec_or_throw (section_id secid) wr_message (WHERE (secid, NULL), cat (mc_impact_4, mc_acc_suboptimal, mc_elf, secid_to_cat (secid))) - << "data not found." << std::endl; + << "data not found." << std::endl; throw check_base::failed (); } diff --git a/src/dwarflint/coverage.cc b/src/dwarflint/coverage.cc index c0fa66222..2cbf3b301 100644 --- a/src/dwarflint/coverage.cc +++ b/src/dwarflint/coverage.cc @@ -29,6 +29,7 @@ #endif #include "coverage.hh" +#include "pri.hh" #include #include @@ -244,18 +245,12 @@ namespace } } -std::string -range_fmt (uint64_t start, uint64_t end) -{ - std::ostringstream os; - os << std::hex << "[0x" << start << ", 0x" << end << ")"; - return os.str (); -} - char * range_fmt (char *buf, size_t buf_size, uint64_t start, uint64_t end) { - std::string s = range_fmt (start, end); + std::stringstream ss; + ss << pri::range (start, end); + std::string s = ss.str (); strncpy (buf, s.c_str (), buf_size); return buf; } @@ -357,3 +352,40 @@ coverage_remove_all (struct coverage *__restrict__ cov, ret = true; return ret; } + +bool +cov::_format_base::fmt (uint64_t start, uint64_t length) +{ + if (_m_seen) + _m_os << _m_delim; + _m_os << pri::range (start, start + length); + _m_seen = true; + return true; +} + +bool +cov::_format_base::wrap_fmt (uint64_t start, uint64_t length, void *data) +{ + _format_base *self = static_cast <_format_base *> (data); + return self->fmt (start, length); +} + +cov::_format_base::_format_base (std::string const &delim) + : _m_delim (delim), + _m_seen (false) +{} + +cov::format_ranges::format_ranges (coverage const &cov, + std::string const &delim) + : _format_base (delim) +{ + coverage_find_ranges (&cov, &wrap_fmt, this); +} + +cov::format_holes::format_holes (coverage const &cov, + uint64_t start, uint64_t length, + std::string const &delim) + : _format_base (delim) +{ + coverage_find_holes (&cov, start, length, &wrap_fmt, this); +} diff --git a/src/dwarflint/coverage.h b/src/dwarflint/coverage.h index c1434855f..d6418c2af 100644 --- a/src/dwarflint/coverage.h +++ b/src/dwarflint/coverage.h @@ -107,9 +107,6 @@ bool coverage_find_ranges (struct coverage const *cov, #ifdef __cplusplus } - -#include -std::string range_fmt (uint64_t start, uint64_t end); #endif #endif//DWARFLINT_COVERAGE_H diff --git a/src/dwarflint/coverage.hh b/src/dwarflint/coverage.hh index 67c6aa62b..d2b418a20 100644 --- a/src/dwarflint/coverage.hh +++ b/src/dwarflint/coverage.hh @@ -31,8 +31,6 @@ #include #include "coverage.h" -std::string range_fmt (uint64_t start, uint64_t end); - namespace cov { class _format_base @@ -42,31 +40,12 @@ namespace cov std::ostringstream _m_os; bool _m_seen; - inline bool fmt (uint64_t start, uint64_t length) - { - if (_m_seen) - _m_os << _m_delim; - _m_os << range_fmt (start, start + length); - _m_seen = true; - return true; - } - - static bool - wrap_fmt (uint64_t start, uint64_t length, void *data) - { - _format_base *self = static_cast <_format_base *> (data); - return self->fmt (start, length); - } - - _format_base (std::string const &delim) - : _m_delim (delim), - _m_seen (false) - { - _m_os << std::hex; - } + inline bool fmt (uint64_t start, uint64_t length); + static bool wrap_fmt (uint64_t start, uint64_t length, void *data); + _format_base (std::string const &delim); public: - operator std::string () const + inline operator std::string () const { return _m_os.str (); } @@ -75,22 +54,14 @@ namespace cov struct format_ranges : public _format_base { - format_ranges (coverage const &cov, std::string const &delim = ", ") - : _format_base (delim) - { - coverage_find_ranges (&cov, &wrap_fmt, this); - } + format_ranges (coverage const &cov, std::string const &delim = ", "); }; struct format_holes : public _format_base { format_holes (coverage const &cov, uint64_t start, uint64_t length, - std::string const &delim = ", ") - : _format_base (delim) - { - coverage_find_holes (&cov, start, length, &wrap_fmt, this); - } + std::string const &delim = ", "); }; } diff --git a/src/dwarflint/messages.cc b/src/dwarflint/messages.cc index afed6b764..c6d647e80 100644 --- a/src/dwarflint/messages.cc +++ b/src/dwarflint/messages.cc @@ -236,28 +236,46 @@ namespace } std::ostream & -wr_warning (where const &wh) +wr_warning () { ++error_count; - return get_stream () << gettext ("warning: ") << wh << ": "; + return get_stream () << gettext ("warning: "); } std::ostream & -wr_error (where const &wh) +wr_error () { ++error_count; - return get_stream () << gettext ("error: ") << wh << ": "; + return get_stream () << gettext ("error: "); } std::ostream & -wr_message (where const &wh, message_category category) +wr_message (message_category category) { if (!message_accept (&warning_criteria, category)) return nostr; else if (message_accept (&error_criteria, category)) - return wr_error (wh); + return wr_error (); else - return wr_warning (wh); + return wr_warning (); +} + +std::ostream & +wr_warning (where const &wh) +{ + return wr_warning () << wh << ": "; +} + +std::ostream & +wr_error (where const &wh) +{ + return wr_error () << wh << ": "; +} + +std::ostream & +wr_message (where const &wh, message_category category) +{ + return wr_message (category) << wh << ": "; } void @@ -304,17 +322,3 @@ wr_message_padding_n0 (unsigned long category, wh, start, end, "unreferenced non-zero bytes"); } - -std::ostream & -pri::operator << (std::ostream &os, pri::pribase const &obj) -{ - return os << obj.m_a << obj.m_b << obj.m_c; -} - -std::ostream & -pri::operator << (std::ostream &os, pri::ref const &obj) -{ - std::stringstream ss; - ss << std::hex << "DIE " << obj.off; - return os << ss.str (); -} diff --git a/src/dwarflint/messages.h b/src/dwarflint/messages.h index 8b595f284..5f88722d4 100644 --- a/src/dwarflint/messages.h +++ b/src/dwarflint/messages.h @@ -145,55 +145,11 @@ cat (message_category c1, } std::ostream &wr_warning (where const &wh); +std::ostream &wr_warning (); std::ostream &wr_error (where const &wh); +std::ostream &wr_error (); std::ostream &wr_message (where const &wh, message_category cat); - -namespace pri -{ - class pribase - { - std::string const &m_a; - std::string const &m_b; - std::string const &m_c; - - protected: - pribase (std::string const &a, - std::string const &b = "", - std::string const &c = "") - : m_a (a), m_b (b), m_c (c) - {} - friend std::ostream &operator << (std::ostream &os, pribase const &obj); - }; - std::ostream &operator << (std::ostream &os, pribase const &obj); - - struct not_enough - : public pribase - { - not_enough (std::string const &what) - : pribase ("not enough data for ", what) - {} - }; - - struct lacks_relocation - : public pribase - { - lacks_relocation (std::string const &what) - : pribase (what, " seems to lack a relocation") - {} - }; - - class ref - { - Dwarf_Off off; - public: - template - ref (T const &die) - : off (die.offset ()) - {} - friend std::ostream &operator << (std::ostream &os, ref const &obj); - }; - std::ostream &operator << (std::ostream &os, ref const &obj); -} +std::ostream &wr_message (message_category cat); #endif #endif//DWARFLINT_MESSAGES_H diff --git a/src/dwarflint/pri.cc b/src/dwarflint/pri.cc new file mode 100644 index 000000000..cd3982911 --- /dev/null +++ b/src/dwarflint/pri.cc @@ -0,0 +1,36 @@ +#include "dwarfstrings.h" +#include "pri.hh" +#include + +pri::attr::attr (int attr_name) + : pribase (dwarf_attr_string (attr_name)) +{} + +std::ostream & +pri::operator << (std::ostream &os, pri::pribase const &obj) +{ + return os << obj.m_a << obj.m_b << obj.m_c; +} + +std::ostream & +pri::operator << (std::ostream &os, pri::ref const &obj) +{ + std::stringstream ss; + ss << std::hex << "DIE " << obj.off; + return os << ss.str (); +} + +std::ostream & +pri::operator << (std::ostream &os, pri::addr const &obj) +{ + std::stringstream ss; + ss << std::hex << "0x" << obj.off; + return os << ss.str (); +} + +std::ostream & +pri::operator << (std::ostream &os, pri::range const &obj) +{ + return os << "[" << pri::addr (obj.start) + << ", " << pri::addr (obj.end) << ")"; +} diff --git a/src/dwarflint/pri.hh b/src/dwarflint/pri.hh new file mode 100644 index 000000000..5914db4d1 --- /dev/null +++ b/src/dwarflint/pri.hh @@ -0,0 +1,83 @@ +#ifndef DWARFLINT_PRI_H +#define DWARFLINT_PRI_H + +#include +#include + +namespace pri +{ + class pribase + { + std::string const &m_a; + std::string const &m_b; + std::string const &m_c; + + protected: + pribase (std::string const &a, + std::string const &b = "", + std::string const &c = "") + : m_a (a), m_b (b), m_c (c) + {} + friend std::ostream &operator << (std::ostream &os, pribase const &obj); + }; + std::ostream &operator << (std::ostream &os, pribase const &obj); + + struct not_enough + : public pribase + { + not_enough (std::string const &what) + : pribase ("not enough data for ", what) + {} + }; + + struct lacks_relocation + : public pribase + { + lacks_relocation (std::string const &what) + : pribase (what, " seems to lack a relocation") + {} + }; + + struct attr + : public pribase + { + attr (int attr_name); + }; + + class ref + { + Dwarf_Off off; + public: + template + ref (T const &die) + : off (die.offset ()) + {} + friend std::ostream &operator << (std::ostream &os, ref const &obj); + }; + std::ostream &operator << (std::ostream &os, ref const &obj); + + class addr + { + Dwarf_Off off; + public: + addr (Dwarf_Off a_off) + : off (a_off) + {} + friend std::ostream &operator << (std::ostream &os, addr const &obj); + }; + std::ostream &operator << (std::ostream &os, addr const &obj); + + class range + { + Dwarf_Off start; + Dwarf_Off end; + public: + range (Dwarf_Off a_start, Dwarf_Off a_end) + : start (a_start), end (a_end) + {} + friend std::ostream &operator << (std::ostream &os, range const &obj); + }; + std::ostream &operator << (std::ostream &os, range const &obj); +} + +#endif//DWARFLINT_PRI_H