From e86994ad45924ebcba4cd59abcd2cf0815cd0982 Mon Sep 17 00:00:00 2001 From: Petr Machata Date: Wed, 4 Nov 2009 15:11:18 +0100 Subject: [PATCH] dwarflint: Use pri::hex where possible * Change pri::addr to be trivial subclass of pri::hex --- src/dwarflint/check_debug_abbrev.cc | 91 +++++++++++++++-------------- src/dwarflint/check_debug_line.cc | 4 +- src/dwarflint/pri.cc | 8 ++- src/dwarflint/pri.hh | 22 +++++-- 4 files changed, 71 insertions(+), 54 deletions(-) diff --git a/src/dwarflint/check_debug_abbrev.cc b/src/dwarflint/check_debug_abbrev.cc index 46e8751e9..3aadd1d37 100644 --- a/src/dwarflint/check_debug_abbrev.cc +++ b/src/dwarflint/check_debug_abbrev.cc @@ -28,7 +28,7 @@ #endif #include "checks-low.hh" -#include "dwarfstrings.h" +#include "pri.hh" #include #include @@ -119,6 +119,16 @@ namespace return a.code < b.code; } }; + + void + complain_invalid_form (where const &where, int name, int form, + std::string const &specification = "") + { + wr_error (where) + << specification << (" "[specification == ""]) + << pri::attr (name) << " with invalid form " + << pri::form (form) << '.' << std::endl; + } } check_debug_abbrev::check_debug_abbrev (dwarflint &lint) @@ -243,9 +253,9 @@ check_debug_abbrev::check_debug_abbrev (dwarflint &lint) if (abbr_tag > DW_TAG_hi_user) { - std::stringstream ss; - ss << ": invalid abbrev tag 0x" << std::hex << abbr_tag << '.'; - wr_error (&where, "%s\n", ss.str ().c_str ()); + wr_error (where) + << "invalid abbrev tag " << pri::hex (abbr_tag) + << '.' << std::endl; throw check_base::failed (); } cur->tag = (typeof (cur->tag))abbr_tag; @@ -261,8 +271,9 @@ check_debug_abbrev::check_debug_abbrev (dwarflint &lint) if (has_children != DW_CHILDREN_no && has_children != DW_CHILDREN_yes) { - wr_error (&where, - ": invalid has_children value 0x%x.\n", cur->has_children); + wr_error (where) + << "invalid has_children value " << pri::hex (cur->has_children) + << '.' << std::endl; throw check_base::failed (); } cur->has_children = has_children == DW_CHILDREN_yes; @@ -298,17 +309,17 @@ check_debug_abbrev::check_debug_abbrev (dwarflint &lint) /* Otherwise validate name and form. */ if (attrib_name > DW_AT_hi_user) { - std::stringstream ss; - ss << ": invalid name 0x" << std::hex << attrib_name << '.'; - wr_error (&where, "%s\n", ss.str ().c_str ()); + wr_error (where) + << "invalid name " << pri::hex (attrib_name) + << '.' << std::endl; throw check_base::failed (); } if (!attrib_form_valid (attrib_form)) { - std::stringstream ss; - ss << ": invalid form 0x" << std::hex << attrib_form << '.'; - wr_error (&where, "%s\n", ss.str ().c_str ()); + wr_error (where) + << "invalid form " << pri::hex (attrib_form) + << '.' << std::endl; throw check_base::failed (); } } @@ -326,44 +337,42 @@ check_debug_abbrev::check_debug_abbrev (dwarflint &lint) if (attrib_name == DW_AT_sibling) { if (sibling_attr != 0) - { - std::stringstream ss; - ss << ": Another DW_AT_sibling attribute in one abbreviation. " - << "(First was 0x" << std::hex << sibling_attr << ".)"; - wr_error (&where, "%s\n", ss.str ().c_str ()); - } + wr_error (where) + << "another DW_AT_sibling attribute in one abbreviation " + << "(first was " << pri::hex (sibling_attr) << ")." + << std::endl; else { assert (attr_off > 0); sibling_attr = attr_off; if (!cur->has_children) - wr_message (mc_die_rel | mc_acc_bloat | mc_impact_1, - &where, - ": Excessive DW_AT_sibling attribute at childless abbrev.\n"); + wr_message (where, + cat (mc_die_rel, mc_acc_bloat, mc_impact_1)) + << "excessive DW_AT_sibling attribute at childless abbrev." + << std::endl; } switch (check_sibling_form (attrib_form)) { case -1: - wr_message (mc_die_rel | mc_impact_2, &where, - ": DW_AT_sibling attribute with form DW_FORM_ref_addr.\n"); + wr_message (where, cat (mc_die_rel, mc_impact_2)) + << "DW_AT_sibling attribute with form DW_FORM_ref_addr." + << std::endl; break; case -2: - wr_error (&where, - ": DW_AT_sibling attribute with non-reference form \"%s\".\n", - dwarf_form_string (attrib_form)); + wr_error (where) + << "DW_AT_sibling attribute with non-reference form " + << pri::form (attrib_form) << '.' << std::endl; }; } /* Similar for DW_AT_location and friends. */ else if (is_location_attrib (attrib_name)) { if (!check_abbrev_location_form (attrib_form)) - wr_error (&where, - ": location attribute %s with invalid form \"%s\".\n", - dwarf_attr_string (attrib_name), - dwarf_form_string (attrib_form)); + complain_invalid_form (where, attrib_name, attrib_form, + "location attribute"); } /* Similar for DW_AT_ranges. */ else if (attrib_name == DW_AT_ranges @@ -373,10 +382,7 @@ check_debug_abbrev::check_debug_abbrev (dwarflint &lint) && attrib_form != DW_FORM_data8 && attrib_form != DW_FORM_sec_offset && attrib_form != DW_FORM_indirect) - wr_error (&where, - ": %s with invalid form \"%s\".\n", - dwarf_attr_string (attrib_name), - dwarf_form_string (attrib_form)); + complain_invalid_form (where, attrib_name, attrib_form); if (attrib_name == DW_AT_ranges) ranges = true; } @@ -387,10 +393,7 @@ check_debug_abbrev::check_debug_abbrev (dwarflint &lint) { if (attrib_form != DW_FORM_addr && attrib_form != DW_FORM_ref_addr) - wr_error (&where, - ": %s with invalid form \"%s\".\n", - dwarf_attr_string (attrib_name), - dwarf_form_string (attrib_form)); + complain_invalid_form (where, attrib_name, attrib_form); if (attrib_name == DW_AT_low_pc) low_pc = true; @@ -406,13 +409,13 @@ check_debug_abbrev::check_debug_abbrev (dwarflint &lint) where_reset_2 (&where, where.addr2); // drop addr 3 if (high_pc && !low_pc) - wr_error (&where, - ": the abbrev has DW_AT_high_pc" - " without also having DW_AT_low_pc.\n"); + wr_error (where) + << "the abbrev has DW_AT_high_pc without also having DW_AT_low_pc." + << std::endl; else if (high_pc && ranges) - wr_error (&where, - ": the abbrev has DW_AT_high_pc & DW_AT_low_pc," - " but also has DW_AT_ranges.\n"); + wr_error (where) + << "the abbrev has DW_AT_high_pc & DW_AT_low_pc, " + << "but also has DW_AT_ranges." << std::endl; } abbrev_table *last = NULL; diff --git a/src/dwarflint/check_debug_line.cc b/src/dwarflint/check_debug_line.cc index 838bf11cb..b976a6cea 100644 --- a/src/dwarflint/check_debug_line.cc +++ b/src/dwarflint/check_debug_line.cc @@ -52,8 +52,8 @@ namespace struct ref *ref = it->line_refs.refs + i; if (!addr_record_has_addr (&line_tables, ref->addr)) wr_error (ref->who) - << ": unresolved reference to .debug_line table " - << "0x" << std::hex << ref->addr << "." << std::endl; + << "unresolved reference to .debug_line table " + << pri::hex (ref->addr) << '.' << std::endl; } addr_record_free (&line_tables); } diff --git a/src/dwarflint/pri.cc b/src/dwarflint/pri.cc index cd3982911..f69307b9a 100644 --- a/src/dwarflint/pri.cc +++ b/src/dwarflint/pri.cc @@ -6,6 +6,10 @@ pri::attr::attr (int attr_name) : pribase (dwarf_attr_string (attr_name)) {} +pri::form::form (int attr_form) + : pribase (dwarf_form_string (attr_form)) +{} + std::ostream & pri::operator << (std::ostream &os, pri::pribase const &obj) { @@ -21,10 +25,10 @@ pri::operator << (std::ostream &os, pri::ref const &obj) } std::ostream & -pri::operator << (std::ostream &os, pri::addr const &obj) +pri::operator << (std::ostream &os, pri::hex const &obj) { std::stringstream ss; - ss << std::hex << "0x" << obj.off; + ss << std::hex << "0x" << obj.value; return os << ss.str (); } diff --git a/src/dwarflint/pri.hh b/src/dwarflint/pri.hh index 5914db4d1..b4ef54df1 100644 --- a/src/dwarflint/pri.hh +++ b/src/dwarflint/pri.hh @@ -44,6 +44,12 @@ namespace pri attr (int attr_name); }; + struct form + : public pribase + { + form (int attr_form); + }; + class ref { Dwarf_Off off; @@ -56,16 +62,20 @@ namespace pri }; std::ostream &operator << (std::ostream &os, ref const &obj); - class addr + class hex { - Dwarf_Off off; + Dwarf_Off value; public: - addr (Dwarf_Off a_off) - : off (a_off) + hex (Dwarf_Off a_value) + : value (a_value) {} - friend std::ostream &operator << (std::ostream &os, addr const &obj); + friend std::ostream &operator << (std::ostream &os, hex const &obj); + }; + std::ostream &operator << (std::ostream &os, hex const &obj); + + struct addr : public hex { + addr (Dwarf_Off off) : hex (off) {} }; - std::ostream &operator << (std::ostream &os, addr const &obj); class range { -- 2.47.3