[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v1 03/11] abg-writer: Refactor write_translation_unit API



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: Introduce write_translation_unit(ctxt,
	tu, indent) and deprecate all other overloads
	* src/abg-writer.cc: Likewise. Also adjust call sites to use new
	overload.
	* tools/abilint.cc (main): use new write_translation_unit() API

Signed-off-by: Matthias Maennich <maennich@google.com>
---
 include/abg-writer.h | 27 ++++++++++++++--------
 src/abg-writer.cc    | 54 +++++++++++++++++++++++++-------------------
 tools/abilint.cc     | 21 +++++++++++++----
 3 files changed, 65 insertions(+), 37 deletions(-)

diff --git a/include/abg-writer.h b/include/abg-writer.h
index 467f8d0a4b2b..ee245cf75db6 100644
--- a/include/abg-writer.h
+++ b/include/abg-writer.h
@@ -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,
diff --git a/src/abg-writer.cc b/src/abg-writer.cc
index 83ea3b30d9fa..627a44715807 100644
--- a/src/abg-writer.cc
+++ b/src/abg-writer.cc
@@ -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";
 }
 
diff --git a/tools/abilint.cc b/tools/abilint.cc
index 8798a24ec256..fab2e6331c8b 100644
--- a/tools/abilint.cc
+++ b/tools/abilint.cc
@@ -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
 	{
-- 
2.21.0.1020.gf2820cf01a-goog