]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
dwarflint: More consolidation of message formatting code
authorPetr Machata <pmachata@redhat.com>
Wed, 4 Nov 2009 13:42:29 +0000 (14:42 +0100)
committerPetr Machata <pmachata@redhat.com>
Wed, 18 Aug 2010 12:55:12 +0000 (14:55 +0200)
12 files changed:
src/Makefile.am
src/dwarflint/check_debug_line.cc
src/dwarflint/check_dups_abstract_origin.cc
src/dwarflint/check_range_out_of_scope.cc
src/dwarflint/checks-low.cc
src/dwarflint/coverage.cc
src/dwarflint/coverage.h
src/dwarflint/coverage.hh
src/dwarflint/messages.cc
src/dwarflint/messages.h
src/dwarflint/pri.cc [new file with mode: 0644]
src/dwarflint/pri.hh [new file with mode: 0644]

index 1a7d10df2e512dea6c463b77459a6b7b48a7f488..0de9cef563ea815e1759f2798a32b66867715152 100644 (file)
@@ -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 \
index 1932919feb084f318ef154d31522a17d3e7559e8..838bf11cb9f3933596f58482f8359ec858e203fa 100644 (file)
@@ -3,6 +3,7 @@
 #include <dwarf.h>
 #include "../libdw/known-dwarf.h"
 #include "dwarfstrings.h"
+#include "pri.hh"
 
 #include <sstream>
 
index 867779ee20f0ff042bc4523d399eb0dc0020c02d..5c3b3218e20a2cd68352212b47a1673c21393431 100644 (file)
@@ -37,6 +37,7 @@
 #include "checks-high.hh"
 #include "dwarfstrings.h"
 #include "all-dies-it.hh"
+#include "pri.hh"
 #include <map>
 
 using elfutils::dwarf;
index 9dcdb37a36922d08ae1489864fa98459a0c7ae66..a1d7d577e1dc1e8bad251cd20ca8aa857bcb1443 100644 (file)
@@ -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;
                            }
                        }
index 411fc487bff638cfba20e875c457eadee3e93a6c..778a0e7877bb16065115044d8ec875be2e37d5c1 100644 (file)
@@ -30,6 +30,7 @@
 #include "checks-low.hh"
 #include "low.h"
 #include "config.h"
+#include "pri.hh"
 #include <map>
 #include <sstream>
 #include <cstring>
@@ -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 ();
 }
index c0fa662220c4d34d0268980286f5a07e942f36d6..2cbf3b301e89788fa9b04707ff66e67a279d4092 100644 (file)
@@ -29,6 +29,7 @@
 #endif
 
 #include "coverage.hh"
+#include "pri.hh"
 
 #include <stdbool.h>
 #include <assert.h>
@@ -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);
+}
index c1434855f12638e6462e3e308f5bc6d45203e204..d6418c2af8643e10d1590decbefbac7acc3ad6ae 100644 (file)
@@ -107,9 +107,6 @@ bool coverage_find_ranges (struct coverage const *cov,
 
 #ifdef __cplusplus
 }
-
-#include <string>
-std::string range_fmt (uint64_t start, uint64_t end);
 #endif
 
 #endif//DWARFLINT_COVERAGE_H
index 67c6aa62bfbd49216b8a1931efb46873f481202e..d2b418a2042bd98b22919da68a7bd056ea6dc7ee 100644 (file)
@@ -31,8 +31,6 @@
 #include <sstream>
 #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 = ", ");
   };
 }
 
index afed6b7645d5aadf74f33487d6bc93e8d4ab490f..c6d647e80d51d62e3f8a3965a44494ee50d89c09 100644 (file)
@@ -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 ();
-}
index 8b595f28490772ddc923330383efd5e27edfab8e..5f88722d4948f8fe0d4025cf31541dd50fb20f8b 100644 (file)
@@ -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 <class T>
-    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 (file)
index 0000000..cd39829
--- /dev/null
@@ -0,0 +1,36 @@
+#include "dwarfstrings.h"
+#include "pri.hh"
+#include <sstream>
+
+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 (file)
index 0000000..5914db4
--- /dev/null
@@ -0,0 +1,83 @@
+#ifndef DWARFLINT_PRI_H
+#define DWARFLINT_PRI_H
+
+#include <libdw.h>
+#include <string>
+
+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 <class T>
+    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