abg-writer: Refactor write_translation_unit API
authorMatthias Maennich <maennich@google.com>
Tue, 21 May 2019 04:39:15 +0000 (05:39 +0100)
committerDodji Seketeli <dodji@redhat.com>
Wed, 22 May 2019 12:33:45 +0000 (14:33 +0200)
Introduce a new overload for write_translation_unit that follows the
parameter order context, object (i.e. translation unit), indent.

Deprecate all other overloads that were part of the API and mostly
forward them to the new one. That effort is made to ensure write_context
is always provided. write_context allows access to all options that
influence the output format.

* include/abg-writer.h (write_translation_unit): Declare a new
overload write_translation_unit(ctxt, tu, indent) and deprecate
all others.
* src/abg-writer.cc (write_translation_unit): Likewise in the
definitions.
(write_corpus, dump, write_translation_unit): Adjust.
* tools/abilint.cc (main): use new write_translation_unit() API

Signed-off-by: Matthias Maennich <maennich@google.com>
include/abg-writer.h
src/abg-writer.cc
tools/abilint.cc

index 467f8d0..ee245cf 100644 (file)
@@ -29,6 +29,8 @@
 #ifndef __ABG_WRITER_H__
 #define __ABG_WRITER_H__
 
+#include "abg-fwd.h"
+
 namespace abigail
 {
 namespace xml_writer
@@ -52,16 +54,21 @@ void
 set_annotate(write_context& ctxt, bool flag);
 
 bool
-write_translation_unit(const translation_unit& tu,
-                      unsigned         indent,
-                      std::ostream&            out,
-                      const bool               annotate = false);
-
-bool
-write_translation_unit(const translation_unit& tu,
-                      unsigned         indent,
-                      const string&            path,
-                      const bool               annotate = false);
+write_translation_unit(write_context&         ctxt,
+                      const translation_unit& tu,
+                      const unsigned          indent);
+
+bool ABG_DEPRECATED
+write_translation_unit(const translation_unit& tu,
+                      unsigned                indent,
+                      std::ostream&           out,
+                      const bool              annotate = false);
+
+bool ABG_DEPRECATED
+write_translation_unit(const translation_unit& tu,
+                      unsigned                indent,
+                      const string&           path,
+                      const bool              annotate = false);
 
 bool
 write_corpus_to_archive(const corpus& corp,
index 83ea3b3..627a447 100644 (file)
@@ -649,8 +649,6 @@ public:
 
 };//end write_context
 
-static bool write_translation_unit(const translation_unit&,
-                                  write_context&, unsigned);
 static void write_location(const location&, write_context&);
 static void write_location(const decl_base_sptr&, write_context&);
 static bool write_visibility(const decl_base_sptr&, ostream&);
@@ -1746,19 +1744,19 @@ set_annotate(write_context& ctxt, bool flag)
 
 /// Serialize a translation unit to an output stream.
 ///
-/// @param tu the translation unit to serialize.
-///
 /// @param ctxt the context of the serialization.  It contains e.g,
 /// the output stream to serialize to.
 ///
+/// @param tu the translation unit to serialize.
+///
 /// @param indent how many indentation spaces to use during the
 /// serialization.
 ///
 /// @return true upon successful completion, false otherwise.
-static bool
-write_translation_unit(const translation_unit& tu,
-                      write_context&           ctxt,
-                      unsigned         indent)
+bool
+write_translation_unit(write_context&         ctxt,
+                      const translation_unit& tu,
+                      const unsigned          indent)
 {
   ostream& o = ctxt.get_ostream();
   const config& c = ctxt.get_config();
@@ -1930,16 +1928,20 @@ write_translation_unit(const translation_unit&  tu,
 ///
 /// @param out the output stream to serialize the translation unit to.
 ///
+/// @param annotate whether to annotate the output with debug information
+///
+/// @deprecated use write_translation_unit(ctct, tu, indent)
+///
 /// @return true upon successful completion, false otherwise.
-bool
-write_translation_unit(const translation_unit& tu,
-                      unsigned         indent,
-                      std::ostream&            out,
-                      const bool               annotate)
+bool ABG_DEPRECATED
+write_translation_unit(const translation_unit& tu,
+                      unsigned                indent,
+                      std::ostream&           out,
+                      const bool              annotate)
 {
   write_context ctxt(tu.get_environment(), out);
   set_annotate(ctxt, annotate);
-  return write_translation_unit(tu, ctxt, indent);
+  return write_translation_unit(ctxt, tu, indent);
 }
 
 /// Serialize a translation unit to a file.
@@ -1949,14 +1951,18 @@ write_translation_unit(const translation_unit&  tu,
 /// @param indent how many indentation spaces to use during the
 /// serialization.
 ///
-/// @param out the file to serialize the translation unit to.
+/// @param path the file to serialize the translation unit to.
+///
+/// @param annotate whether to annotate the output with debug information
+///
+/// @deprecated use write_translation_unit(ctct, tu, indent)
 ///
 /// @return true upon successful completion, false otherwise.
-bool
-write_translation_unit(const translation_unit& tu,
-                      unsigned         indent,
-                      const string&            path,
-                      const bool annotate)
+bool ABG_DEPRECATED
+write_translation_unit(const translation_unit& tu,
+                      unsigned                indent,
+                      const string&           path,
+                      const bool              annotate)
 {
   bool result = true;
 
@@ -1969,7 +1975,9 @@ write_translation_unit(const translation_unit&    tu,
          return false;
        }
 
-      if (!write_translation_unit(tu, indent, of, annotate))
+      write_context ctxt(tu.get_environment(), of);
+      set_annotate(ctxt, annotate);
+      if (!write_translation_unit(ctxt, tu, indent))
        {
          cerr << "failed to access " << path << "\n";
          result = false;
@@ -4139,7 +4147,7 @@ write_corpus(const corpus_sptr    corpus,
     {
       translation_unit& tu = **i;
       if (!tu.is_empty())
-       write_translation_unit(tu, ctxt, get_indent_to_level(ctxt, indent, 1));
+       write_translation_unit(ctxt, tu, get_indent_to_level(ctxt, indent, 1));
     }
 
   do_indent_to_level(ctxt, indent, 0);
@@ -4389,7 +4397,7 @@ dump(const translation_unit& t, std::ostream& o, const bool annotate)
 {
   xml_writer::write_context ctxt(t.get_environment(), o);
   xml_writer::set_annotate(ctxt, annotate);
-  write_translation_unit(t, ctxt, /*indent=*/0);
+  write_translation_unit(ctxt, t, /*indent=*/0);
   o << "\n";
 }
 
index 8798a24..fab2e63 100644 (file)
@@ -67,6 +67,8 @@ using abigail::xml_reader::read_corpus_from_native_xml;
 using abigail::xml_reader::read_corpus_from_native_xml_file;
 using abigail::dwarf_reader::read_corpus_from_elf;
 using abigail::xml_writer::write_translation_unit;
+using abigail::xml_writer::write_context_sptr;
+using abigail::xml_writer::create_write_context;
 using abigail::xml_writer::write_corpus;
 using abigail::xml_writer::write_corpus_to_archive;
 
@@ -287,7 +289,11 @@ main(int argc, char* argv[])
            }
 
          if (!opts.noout)
-           write_translation_unit(*tu, 0, cout);
+           {
+             const write_context_sptr& ctxt
+                 = create_write_context(tu->get_environment(), cout);
+             write_translation_unit(*ctxt, *tu, 0);
+           }
          return 0;
        }
       else
@@ -411,10 +417,17 @@ main(int argc, char* argv[])
       if (tu)
        {
          if (opts.diff)
-           r = write_translation_unit(*tu, /*indent=*/0,
-                                      tmp_file->get_stream());
+           {
+             const write_context_sptr& ctxt = create_write_context(
+                 tu->get_environment(), tmp_file->get_stream());
+             r = write_translation_unit(*ctxt, *tu, 0);
+           }
          if (!opts.noout && !opts.diff)
-           r &= write_translation_unit(*tu, /*indent=*/0, cout);
+           {
+             const write_context_sptr& ctxt
+                 = create_write_context(tu->get_environment(), cout);
+             r &= write_translation_unit(*ctxt, *tu, 0);
+           }
        }
       else
        {