From 948f27480fce83ae8de3ca77ca1179646aa07728 Mon Sep 17 00:00:00 2001 From: Matthias Maennich Date: Tue, 21 May 2019 05:39:16 +0100 Subject: [PATCH] abg-writer: Refactor write_corpus API Introduce a new overload for write_corpus that follows the parameter order context, object (i.e. corpus), indent. Deprecate all other overloads that were part of the API and mostly forward them to the new overload. 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_corpus): Introduce new overload write_corpus(ctxt, corpus, indent) and deprecate all others. * src/abg-writer.cc (write_corpus): Likewise for the definitions and adjust. * tests/test-read-dwarf.cc (test_task::perform): Use the new write_corpus which requires a write_context. * tools/abidw.cc (load_corpus_and_write_abixml, ): Likewise. * tools/abilint.cc (main): Likewise. Also simplify logic around the locations as they now can be expressed with less code. Signed-off-by: Matthias Maennich --- include/abg-writer.h | 27 +++++++++--------- src/abg-writer.cc | 61 +++++++++++++++++++++++++++------------- tests/test-read-dwarf.cc | 8 ++++-- tools/abidw.cc | 29 ++++++++++--------- tools/abilint.cc | 48 ++++++++++++------------------- 5 files changed, 96 insertions(+), 77 deletions(-) diff --git a/include/abg-writer.h b/include/abg-writer.h index ee245cf7..d93cc69a 100644 --- a/include/abg-writer.h +++ b/include/abg-writer.h @@ -84,21 +84,22 @@ write_corpus_to_archive(const corpus_sptr corp, const bool annotate = false); bool -write_corpus(const corpus_sptr corpus, - unsigned indent, - write_context& ctxt); +write_corpus(write_context& ctxt, const corpus_sptr& corpus, unsigned indent); -bool -write_corpus(const corpus_sptr corpus, - unsigned indent, - std::ostream& out, - const bool annotate = false); +bool ABG_DEPRECATED +write_corpus(const corpus_sptr& corpus, unsigned indent, write_context& ctxt); -bool -write_corpus(const corpus_sptr corpus, - unsigned indent, - const string& path, - const bool annotate = false); +bool ABG_DEPRECATED +write_corpus(const corpus_sptr corpus, + unsigned indent, + std::ostream& out, + const bool annotate = false); + +bool ABG_DEPRECATED +write_corpus(const corpus_sptr corpus, + unsigned indent, + const string& path, + const bool annotate = false); bool write_corpus_group(const corpus_group_sptr& group, diff --git a/src/abg-writer.cc b/src/abg-writer.cc index 627a4471..85f3aebc 100644 --- a/src/abg-writer.cc +++ b/src/abg-writer.cc @@ -4063,17 +4063,15 @@ write_corpus_to_archive(const corpus_sptr corp, const bool annotate) /// Serialize an ABI corpus to a single native xml document. The root /// note of the resulting XML document is 'abi-corpus'. /// +/// @param ctxt the write context to use. +/// /// @param corpus the corpus to serialize. /// /// @param indent the number of white space indentation to use. /// -/// @param ctxt the write context to use. -/// /// @return true upon successful completion, false otherwise. bool -write_corpus(const corpus_sptr corpus, - unsigned indent, - write_context& ctxt) +write_corpus(write_context& ctxt, const corpus_sptr& corpus, unsigned indent) { if (!corpus) return false; @@ -4157,7 +4155,25 @@ write_corpus(const corpus_sptr corpus, } /// Serialize an ABI corpus to a single native xml document. The root -/// note of the resulting XML document is 'abi-corpus'. +/// node of the resulting XML document is 'abi-corpus'. +/// +/// @param corpus the corpus to serialize. +/// +/// @param indent the number of white space indentation to use. +/// +/// @param ctxt the write context to use. +/// +/// @deprecated: use write_corpus(ctxt, corpus, indent) +/// +/// @return true upon successful completion, false otherwise. +bool ABG_DEPRECATED +write_corpus(const corpus_sptr& corpus, unsigned indent, write_context& ctxt) +{ + return write_corpus(ctxt, corpus, indent); +} + +/// Serialize an ABI corpus to a single native xml document. The root +/// node of the resulting XML document is 'abi-corpus'. /// /// @param corpus the corpus to serialize. /// @@ -4167,12 +4183,14 @@ write_corpus(const corpus_sptr corpus, /// /// @param annotate whether ABIXML output should be annotated. /// +/// @deprecated: use write_corpus(ctxt, corpus, indent) +/// /// @return true upon successful completion, false otherwise. -bool -write_corpus(const corpus_sptr corpus, - unsigned indent, - std::ostream& out, - const bool annotate) +bool ABG_DEPRECATED +write_corpus(const corpus_sptr corpus, + unsigned indent, + std::ostream& out, + const bool annotate) { if (!corpus) return false; @@ -4180,7 +4198,7 @@ write_corpus(const corpus_sptr corpus, write_context ctxt(corpus->get_environment(), out); set_annotate(ctxt, annotate); - return write_corpus(corpus, indent, ctxt); + return write_corpus(ctxt, corpus, indent); } /// Serialize an ABI corpus group to a single native xml document. @@ -4227,7 +4245,7 @@ std::ostream& out = ctxt.get_ostream(); group->get_corpora().begin(); c != group->get_corpora().end(); ++c) - write_corpus(*c, get_indent_to_level(ctxt, indent, 1), ctxt); + write_corpus(ctxt, *c, get_indent_to_level(ctxt, indent, 1)); do_indent_to_level(ctxt, indent, 0); out << "\n"; @@ -4274,12 +4292,14 @@ write_corpus_group(const corpus_group_sptr& group, /// /// @param annotate whether ABIXML output should be annotated. /// +/// @deprecated: use write_corpus(ctxt, corpus, indent) +/// /// @return true upon successful completion, false otherwise. -bool -write_corpus(const corpus_sptr corpus, - unsigned indent, - const string& path, - const bool annotate) +bool ABG_DEPRECATED +write_corpus(const corpus_sptr corpus, + unsigned indent, + const string& path, + const bool annotate) { bool result = true; @@ -4292,7 +4312,10 @@ write_corpus(const corpus_sptr corpus, return false; } - if (!write_corpus(corpus, indent, of, annotate)) + const write_context_sptr& ctxt + = create_write_context(corpus->get_environment(), of); + ctxt->set_annotate(annotate); + if (!write_corpus(*ctxt, corpus, indent)) { cerr << "failed to access " << path << "\n"; result = false; diff --git a/tests/test-read-dwarf.cc b/tests/test-read-dwarf.cc index e743e5b6..b3be07fe 100644 --- a/tests/test-read-dwarf.cc +++ b/tests/test-read-dwarf.cc @@ -46,6 +46,9 @@ using abigail::dwarf_reader::read_corpus_from_elf; using abigail::dwarf_reader::read_context; using abigail::dwarf_reader::read_context_sptr; using abigail::dwarf_reader::create_read_context; +using abigail::xml_writer::create_write_context; +using abigail::xml_writer::write_context_sptr; +using abigail::xml_writer::write_corpus; /// This is an aggregate that specifies where a test shall get its /// input from, and where it shall write its ouput to. @@ -333,8 +336,9 @@ struct test_task : public abigail::workers::task is_ok = false; return; } - is_ok = - abigail::xml_writer::write_corpus(corp, /*indent=*/0, of); + const write_context_sptr write_ctxt + = create_write_context(corp->get_environment(), of); + is_ok = write_corpus(*write_ctxt, corp, /*indent=*/0); of.close(); string abidw = string(get_build_dir()) + "/tools/abidw"; diff --git a/tools/abidw.cc b/tools/abidw.cc index d86c3607..45c0b862 100644 --- a/tools/abidw.cc +++ b/tools/abidw.cc @@ -69,6 +69,8 @@ using abigail::comparison::corpus_diff_sptr; using abigail::comparison::compute_diff; using abigail::comparison::diff_context_sptr; using abigail::comparison::diff_context; +using abigail::xml_writer::create_write_context; +using abigail::xml_writer::write_context_sptr; using abigail::xml_writer::write_corpus; using abigail::xml_reader::read_corpus_from_native_xml_file; using abigail::dwarf_reader::read_context; @@ -446,7 +448,10 @@ load_corpus_and_write_abixml(char* argv[], // it back, and compare the ABI of what we've read back // against the ABI of the input ELF file. temp_file_sptr tmp_file = temp_file::create(); - write_corpus(corp, 0, tmp_file->get_stream(), opts.annotate); + const write_context_sptr& write_ctxt = create_write_context( + corp->get_environment(), tmp_file->get_stream()); + set_annotate(*write_ctxt, opts.annotate); + write_corpus(*write_ctxt, corp, 0); tmp_file->get_stream().flush(); corpus_sptr corp2 = read_corpus_from_native_xml_file(tmp_file->get_path(), @@ -489,23 +494,21 @@ load_corpus_and_write_abixml(char* argv[], << opts.out_file_path << "'\n"; return 1; } - abigail::xml_writer::write_context_sptr write_ctxt = - abigail::xml_writer::create_write_context(corp->get_environment(), - of); - abigail::xml_writer::set_show_locs(*write_ctxt, opts.show_locs); - abigail::xml_writer::set_annotate(*write_ctxt, opts.annotate); - abigail::xml_writer::write_corpus(corp, 0, *write_ctxt); + const write_context_sptr& write_ctxt + = create_write_context(corp->get_environment(), of); + set_show_locs(*write_ctxt, opts.show_locs); + set_annotate(*write_ctxt, opts.annotate); + write_corpus(*write_ctxt, corp, 0); of.close(); return 0; } else { - abigail::xml_writer::write_context_sptr write_ctxt = - abigail::xml_writer::create_write_context(corp->get_environment(), - cout); - abigail::xml_writer::set_show_locs(*write_ctxt, opts.show_locs); - abigail::xml_writer::set_annotate(*write_ctxt, opts.annotate); - exit_code = !abigail::xml_writer::write_corpus(corp, 0, *write_ctxt); + write_context_sptr write_ctxt + = create_write_context(corp->get_environment(), cout); + set_show_locs(*write_ctxt, opts.show_locs); + set_annotate(*write_ctxt, opts.annotate); + exit_code = !write_corpus(*write_ctxt, corp, 0); } } diff --git a/tools/abilint.cc b/tools/abilint.cc index fab2e633..0bdc4962 100644 --- a/tools/abilint.cc +++ b/tools/abilint.cc @@ -305,7 +305,11 @@ main(int argc, char* argv[]) set_suppressions(*ctxt, opts); corpus_sptr corp = abigail::xml_reader::read_corpus_from_input(*ctxt); if (!opts.noout) - write_corpus(corp, /*indent=*/0, cout); + { + const write_context_sptr& ctxt + = create_write_context(corp->get_environment(), cout); + write_corpus(*ctxt, corp, /*indent=*/0); + } return 0; } } @@ -411,51 +415,35 @@ main(int argc, char* argv[]) return 1; } - std::iostream& of = tmp_file->get_stream(); - bool r = true; + std::ostream& of = opts.diff ? tmp_file->get_stream() : cout; + const abigail::ir::environment* env + = tu ? tu->get_environment() : corp->get_environment(); + const write_context_sptr ctxt = create_write_context(env, of); + + bool is_ok = true; if (tu) { - if (opts.diff) - { - 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) - { - const write_context_sptr& ctxt - = create_write_context(tu->get_environment(), cout); - r &= write_translation_unit(*ctxt, *tu, 0); - } + if (!opts.noout) + is_ok = write_translation_unit(*ctxt, *tu, 0); } else { - r = true; - if (type == abigail::tools_utils::FILE_TYPE_XML_CORPUS) + if (type == abigail::tools_utils::FILE_TYPE_XML_CORPUS + || type == abigail::tools_utils::FILE_TYPE_ELF) { - if (opts.diff) - r = write_corpus(corp, /*indent=*/0, of); - - if (!opts.noout && !opts.diff) - r &= write_corpus(corp, /*indent=*/0, cout); + if (!opts.noout) + is_ok = write_corpus(*ctxt, corp, 0); } else if (type == abigail::tools_utils::FILE_TYPE_ZIP_CORPUS) { #ifdef WITH_ZIP_ARCHIVE if (!opts.noout) - r = write_corpus_to_archive(*corp, tmp_file->get_path()); + is_ok = write_corpus_to_archive(*corp, tmp_file->get_path()); #endif //WITH_ZIP_ARCHIVE } - else if (type == abigail::tools_utils::FILE_TYPE_ELF) - { - if (!opts.noout) - r = write_corpus(corp, /*indent=*/0, cout); - } } - bool is_ok = r; - if (!is_ok) { string output = -- 2.43.5