From 9c7f025ae99414b3c5fa632a0279c12c6324a850 Mon Sep 17 00:00:00 2001 From: Benjamin Kosnik Date: Tue, 6 Aug 2013 16:39:25 -0700 Subject: [PATCH] Simplify interface for serializing/deserializing translation_units. 2013-08-06 Benjamin Kosnik * include/abg-ir.h (translation_unit::read): New member function. (translation_unit::write): Same. * src/abg-reader.cc (translation_unit::read): Define. * src/abg-writer.cc (translation_unit::write): Define. * include/abg-reader.h: Remove. * include/abg-writer.h: Remove. * include/Makefile.am (headers): Same. * include/Makefile.in: Regenerate. --- include/Makefile.am | 2 -- include/Makefile.in | 2 -- include/abg-ir.h | 14 +++++++++++++- include/abg-reader.h | 38 -------------------------------------- include/abg-writer.h | 39 --------------------------------------- src/abg-reader.cc | 37 +++++++++++++++++++------------------ src/abg-writer.cc | 37 +++++++++++++++---------------------- tests/test-read-write.cc | 10 ++++------ tests/test-walker.cc | 4 ++-- 9 files changed, 53 insertions(+), 130 deletions(-) delete mode 100644 include/abg-reader.h delete mode 100644 include/abg-writer.h diff --git a/include/Makefile.am b/include/Makefile.am index 4b6b4085..0e5abc60 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,11 +1,9 @@ headers = \ abg-ir.h \ abg-irfwd.h \ -abg-reader.h \ abg-corpus.h \ abg-libxml-utils.h \ abg-hash.h \ -abg-writer.h \ abg-config.h \ abg-version.h \ abg-viz-common.h \ diff --git a/include/Makefile.in b/include/Makefile.in index 255d6f03..6e253836 100644 --- a/include/Makefile.in +++ b/include/Makefile.in @@ -209,11 +209,9 @@ top_srcdir = @top_srcdir@ headers = \ abg-ir.h \ abg-irfwd.h \ -abg-reader.h \ abg-corpus.h \ abg-libxml-utils.h \ abg-hash.h \ -abg-writer.h \ abg-config.h \ abg-version.h \ abg-viz-common.h \ diff --git a/include/abg-ir.h b/include/abg-ir.h index fdbcc7fe..53be7bf2 100644 --- a/include/abg-ir.h +++ b/include/abg-ir.h @@ -142,7 +142,7 @@ public: /// Constructor of translation_unit. /// - /// @param path the path of the translation unit. + /// @param path the location of the translation unit. translation_unit(const std::string& path); /// @return the path of the compilation unit that gave birth to this @@ -179,6 +179,18 @@ public: bool is_empty() const; + /// Deserialize the contents of the external file into this + /// translation_unit object. + bool + read(); + + /// Serialize the contents of this translation unit object into an + /// external file. + /// + /// @param out the ostream output object. + bool + write(std::ostream& out); + /// This implements the traversable_base::traverse pure virtual /// function. /// diff --git a/include/abg-reader.h b/include/abg-reader.h deleted file mode 100644 index c17e8b9b..00000000 --- a/include/abg-reader.h +++ /dev/null @@ -1,38 +0,0 @@ -// -*- mode: C++ -*- -// -// Copyright (C) 2013 Red Hat, Inc. -// -// This file is part of the GNU Application Binary Interface Generic -// Analysis and Instrumentation Library (libabigail). This library is -// free software; you can redistribute it and/or modify it under the -// terms of the GNU Lesser General Public License as published by the -// Free Software Foundation; either version 3, or (at your option) any -// later version. - -// This library is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Lesser Public License for more details. - -// You should have received a copy of the GNU Lesser General Public -// License along with this program; see the file COPYING-LGPLV3. If -// not, see . - -/// @file - -#ifndef __ABG_READER_H__ -#define __ABG_READER_H__ - -#include "abg-corpus.h" - -namespace abigail -{ -/// Namespace for reader. -namespace reader -{ - bool - read_file(translation_unit& tu); -}// end namespace reader -}// end namespace abigail - -#endif //__ABG_READER_H__ diff --git a/include/abg-writer.h b/include/abg-writer.h deleted file mode 100644 index 14c0fb15..00000000 --- a/include/abg-writer.h +++ /dev/null @@ -1,39 +0,0 @@ -// -*- mode: C++ -*- -// -// Copyright (C) 2013 Red Hat, Inc. -// -// This file is part of the GNU Application Binary Interface Generic -// Analysis and Instrumentation Library (libabigail). This library is -// free software; you can redistribute it and/or modify it under the -// terms of the GNU Lesser General Public License as published by the -// Free Software Foundation; either version 3, or (at your option) any -// later version. - -// This library is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Lesser Public License for more details. - -// You should have received a copy of the GNU Lesser General Public -// License along with this program; see the file COPYING-LGPLV3. If -// not, see . - -/// @file - -#ifndef __ABG_WRITER_H__ -#define __ABG_WRITER_H__ - -#include -#include "abg-corpus.h" - -namespace abigail -{ -/// Namespace for writer. -namespace writer -{ -bool -write_to_ostream(const translation_unit& tu, std::ostream& out); -}//end namespace writer - -}// end namespace abigail -#endif //__ABG_WRITER_H__ diff --git a/src/abg-reader.cc b/src/abg-reader.cc index 037d1e30..4b8014ce 100644 --- a/src/abg-reader.cc +++ b/src/abg-reader.cc @@ -27,28 +27,28 @@ #include #include #include -#include "abg-reader.h" #include "abg-libxml-utils.h" - -using std::string; -using std::stack; -using std::tr1::unordered_map; -using std::tr1::dynamic_pointer_cast; -using std::vector; +#include "abg-ir.h" namespace abigail { using xml::xml_char_sptr; +/// Internal namespace for reader. namespace reader { +using std::string; +using std::stack; +using std::tr1::unordered_map; +using std::tr1::dynamic_pointer_cast; +using std::vector; class read_context; -static void update_read_context(read_context&); -static void update_read_context(read_context&, xmlNodePtr); -static void update_depth_info_of_read_context(read_context&, int); +static void update_read_context(read_context&); +static void update_read_context(read_context&, xmlNodePtr); +static void update_depth_info_of_read_context(read_context&, int); /// This abstracts the context in which the current ABI /// instrumentation dump is being de-serialized. It carries useful @@ -485,14 +485,6 @@ static bool handle_class_decl(read_context&); static bool handle_function_template_decl(read_context&); static bool handle_class_template_decl(read_context&); -bool -read_file(translation_unit& tu) -{ - read_context read_ctxt(xml::new_reader_from_file(tu.get_path())); - - return read_input(read_ctxt, tu); -} - /// Updates the instance of read_context. Basically update thee path /// of elements from the root to the current element, that we maintain /// to know the current scope. This function needs to be called after @@ -2622,4 +2614,13 @@ handle_class_template_decl(read_context& ctxt) } }//end namespace reader + + +// Define reader. +bool +translation_unit::read() +{ + reader::read_context read_ctxt(xml::new_reader_from_file(this->get_path())); + return reader::read_input(read_ctxt, *this); +} }//end namespace abigail diff --git a/src/abg-writer.cc b/src/abg-writer.cc index 14ab6de0..fd7e4970 100644 --- a/src/abg-writer.cc +++ b/src/abg-writer.cc @@ -24,12 +24,12 @@ #include #include #include -#include "abg-writer.h" #include "abg-config.h" - +#include "abg-ir.h" namespace abigail { +/// Internal namespace for writer. namespace writer { using std::tr1::shared_ptr; @@ -67,7 +67,7 @@ public: string get_id_with_prefix(const string& prefix) { - ostringstream o; + ostringstream o; o << prefix << get_new_id(); return o.str(); } @@ -173,8 +173,7 @@ private: }; //end write_context static bool write_translation_unit(const translation_unit&, - write_context&, - unsigned); + write_context&, unsigned); static void write_location(location, translation_unit&, ostream&); static void write_location(const shared_ptr&, ostream&); static bool write_visibility(const shared_ptr&, ostream&); @@ -272,20 +271,6 @@ get_indent_to_level(write_context& ctxt, unsigned initial_indent, return nb_ws; } -/// Serialize a translation_unit into an output stream. -/// -/// @param tu the translation unit to serialize. -/// -/// @param out the output stream. -/// -/// @return true upon successful completion, false otherwise. -bool -write_to_ostream(const translation_unit& tu, ostream &out) -{ - write_context ctxt(out); - return write_translation_unit(tu, ctxt, /*indent=*/0); -} - /// Write a location to the output stream. /// /// If the location is empty, nothing is written. @@ -606,7 +591,7 @@ write_decl(const shared_ptr decl, write_context& ctxt, /// serialization. /// /// @return true upon successful completion, false otherwise. -static bool +bool write_translation_unit(const translation_unit& tu, write_context& ctxt, unsigned indent) { @@ -1505,5 +1490,13 @@ write_class_template_decl (const shared_ptr decl, return true; } -}//end namespace writer -}//end namespace abigail +} //end namespace writer + +bool +translation_unit::write(std::ostream &out) +{ + writer::write_context ctxt(out); + return writer::write_translation_unit(*this, ctxt, /*indent=*/0); +} + +} //end namespace abigail diff --git a/tests/test-read-write.cc b/tests/test-read-write.cc index 96f8e2f5..5dff53d9 100644 --- a/tests/test-read-write.cc +++ b/tests/test-read-write.cc @@ -22,13 +22,11 @@ #include #include #include +#include "abg-ir.h" #include "test-utils.h" -#include "abg-reader.h" -#include "abg-writer.h" using std::string; using std::ofstream; - using std::cerr; /// This is an aggregate that specifies where a test shall get its @@ -143,8 +141,8 @@ main() { string input_suffix(s->in_path); in_path = abigail::tests::get_src_dir() + "/tests/" + input_suffix; - abigail::translation_unit tu(input_suffix); - if (!abigail::reader::read_file(tu)) + abigail::translation_unit tu(in_path); + if (!tu.read()) { cerr << "failed to read " << in_path << "\n"; is_ok = false; @@ -168,7 +166,7 @@ main() continue; } - bool r = abigail::writer::write_to_ostream(tu, of); + bool r = tu.write(of); is_ok = (is_ok && r); of.close(); string cmd = "diff -u " + in_path + " " + out_path; diff --git a/tests/test-walker.cc b/tests/test-walker.cc index 7345d605..d6007e32 100644 --- a/tests/test-walker.cc +++ b/tests/test-walker.cc @@ -22,8 +22,8 @@ #include #include #include +#include "abg-ir.h" #include "test-utils.h" -#include "abg-reader.h" using std::string; using std::ofstream; @@ -50,7 +50,7 @@ main(int argc, char **argv) string file_name = argv[1]; abigail::translation_unit tu(file_name); - if (!abigail::reader::read_file(tu)) + if (!tu.read()) { cerr << "failed to read " << file_name << "\n"; return 1;