From fca2661581af6412dde955a04c8291f13230fd5b Mon Sep 17 00:00:00 2001 From: Matthias Maennich Date: Tue, 21 May 2019 05:39:20 +0100 Subject: [PATCH] Make write_architecture and write_corpus_path flags in the write_context Having write_context carry corresponding flags for output sensitive command line options, is useful to ensure these options are not lost in chains for write_* calls. In particular, these options can have various meanings depending on the context (corpus, corpus_group, etc.) Hence add them to the write_context along with getters and setters and make the writers aware of their existence. We do not need to modify the corpus or corpus group's path or architecture any longer as they get ignored for a different reason now. Finally, drop the flag handling in abidw as it is already done via set_opts, which learned about these new flags. * include/abg-writer.h (set_write_architecture) (set_write_corpus_path): Declare new getter functions. (write_corpus): Take a new "member_of_group" argument. (set_common_options): Use set_write_{architecture, corpus_path} here. * src/abg-writer.cc (write_context::m_write_{architecture, corpus_path}}): Add new data members. (write_context::write_context): Initialize the new data members. (write_context::{s,g}et_write_{architecture, corpus}): Define new accessors. (set_write_{architecture, corpus}): Define new free-form getter functions. (write_corpus): Add flag to make aware if written as part of a group. * tools/abidw.cc (load_corpus_and_write_abixml) (load_kernel_corpus_group_and_write_abixml): Drop obsolete option handling as xml_writer::set_common_options now takes care of it. Signed-off-by: Matthias Maennich Signed-off-by: Dodji Seketeli ldiff --git a/include/abg-writer.h b/include/abg-writer.h index 200b5f7..729b455 100644 --- a/include/abg-writer.h +++ b/include/abg-writer.h @@ -53,6 +53,11 @@ set_show_locs(write_context& ctxt, bool flag); void set_annotate(write_context& ctxt, bool flag); +void +set_write_architecture(write_context& ctxt, bool flag); + +void +set_write_corpus_path(write_context& ctxt, bool flag); /// A convenience generic function to set common options (usually used /// by Libabigail tools) from a generic options carrying-object, into @@ -69,6 +74,8 @@ set_common_options(write_context& ctxt, const OPTS& opts) { set_annotate(ctxt, opts.annotate); set_show_locs(ctxt, opts.show_locs); + set_write_architecture(ctxt, opts.write_architecture); + set_write_corpus_path(ctxt, opts.write_corpus_path); } void @@ -105,7 +112,10 @@ write_corpus_to_archive(const corpus_sptr corp, const bool annotate = false); bool -write_corpus(write_context& ctxt, const corpus_sptr& corpus, unsigned indent); +write_corpus(write_context& ctxt, + const corpus_sptr& corpus, + unsigned indent, + bool member_of_group = false); bool ABG_DEPRECATED write_corpus(const corpus_sptr& corpus, unsigned indent, write_context& ctxt); Signed-off-by: Dodji Seketeli --- include/abg-writer.h | 12 +++++- src/abg-writer.cc | 91 ++++++++++++++++++++++++++++++++++++++++---- tools/abidw.cc | 10 ----- 3 files changed, 94 insertions(+), 19 deletions(-) diff --git a/include/abg-writer.h b/include/abg-writer.h index 200b5f7e..729b455d 100644 --- a/include/abg-writer.h +++ b/include/abg-writer.h @@ -53,6 +53,11 @@ set_show_locs(write_context& ctxt, bool flag); void set_annotate(write_context& ctxt, bool flag); +void +set_write_architecture(write_context& ctxt, bool flag); + +void +set_write_corpus_path(write_context& ctxt, bool flag); /// A convenience generic function to set common options (usually used /// by Libabigail tools) from a generic options carrying-object, into @@ -69,6 +74,8 @@ set_common_options(write_context& ctxt, const OPTS& opts) { set_annotate(ctxt, opts.annotate); set_show_locs(ctxt, opts.show_locs); + set_write_architecture(ctxt, opts.write_architecture); + set_write_corpus_path(ctxt, opts.write_corpus_path); } void @@ -105,7 +112,10 @@ write_corpus_to_archive(const corpus_sptr corp, const bool annotate = false); bool -write_corpus(write_context& ctxt, const corpus_sptr& corpus, unsigned indent); +write_corpus(write_context& ctxt, + const corpus_sptr& corpus, + unsigned indent, + bool member_of_group = false); bool ABG_DEPRECATED write_corpus(const corpus_sptr& corpus, unsigned indent, write_context& ctxt); diff --git a/src/abg-writer.cc b/src/abg-writer.cc index d67e64ff..3792bd22 100644 --- a/src/abg-writer.cc +++ b/src/abg-writer.cc @@ -165,6 +165,8 @@ class write_context ostream* m_ostream; bool m_annotate; bool m_show_locs; + bool m_write_architecture; + bool m_write_corpus_path; mutable type_ptr_map m_type_id_map; mutable type_ptr_set_type m_emitted_type_set; type_ptr_set_type m_emitted_decl_only_set; @@ -193,7 +195,9 @@ public: m_id_manager(env), m_ostream(&os), m_annotate(false), - m_show_locs(true) + m_show_locs(true), + m_write_architecture(true), + m_write_corpus_path(true) {} /// Getter of the environment we are operating from. @@ -235,6 +239,34 @@ public: set_annotate(bool f) {m_annotate = f;} + /// Getter of the write-architecture option. + /// + /// @return true iff architecture information shall be emitted + bool + get_write_architecture() + {return m_write_architecture;} + + /// Setter of the write-architecture option + /// + /// @param f the new value of the flag. + void + set_write_architecture(bool f) + {m_write_architecture = f;} + + /// Getter of the write-corpus-path option. + /// + /// @return true iff corpus-path information shall be emitted + bool + get_write_corpus_path() + {return m_write_corpus_path;} + + /// Setter of the write-corpus-path option + /// + /// @param f the new value of the flag. + void + set_write_corpus_path(bool f) + {m_write_corpus_path = f;} + /// Getter of the "show-locs" option. /// /// When this option is true then the XML writer emits location @@ -1763,6 +1795,30 @@ void set_ostream(write_context& ctxt, ostream& os) {ctxt.set_ostream(os);} +/// Set the 'write-architecture' flag. +/// +/// When this flag is set then the XML writer will emit architecture +/// information +/// +/// @param ctxt the context to set this flag on to. +/// +/// @param flag the new value of the 'write-architecture' flag. +void +set_write_architecture(write_context& ctxt, bool flag) +{ctxt.set_write_architecture(flag);} + +/// Set the 'write-corpus-path' flag. +/// +/// When this flag is set then the XML writer will emit corpus-path +/// information +/// +/// @param ctxt the context to set this flag on to. +/// +/// @param flag the new value of the 'write-corpus-path' flag. +void +set_write_corpus_path(write_context& ctxt, bool flag) +{ctxt.set_write_corpus_path(flag);} + /// Serialize a translation unit to an output stream. /// /// @param ctxt the context of the serialization. It contains e.g, @@ -4092,7 +4148,10 @@ write_corpus_to_archive(const corpus_sptr corp, const bool annotate) /// /// @return true upon successful completion, false otherwise. bool -write_corpus(write_context& ctxt, const corpus_sptr& corpus, unsigned indent) +write_corpus(write_context& ctxt, + const corpus_sptr& corpus, + unsigned indent, + bool member_of_group) { if (!corpus) return false; @@ -4102,10 +4161,26 @@ write_corpus(write_context& ctxt, const corpus_sptr& corpus, unsigned indent) std::ostream& out = ctxt.get_ostream(); out << "get_path().empty()) - out << " path='" << xml::escape_xml_string(corpus->get_path()) << "'"; - if (!corpus->get_architecture_name().empty()) + // For an abi-corpus as part of an abi-corpus group, only omit the path, but + // keep the filename. + std::string corpus_path = corpus->get_path(); + if (!ctxt.get_write_corpus_path()) + { + if (member_of_group) + { + size_t pos = corpus_path.rfind('/'); + corpus_path + = corpus_path.substr(pos != std::string::npos ? pos + 1 : 0); + } + else + corpus_path.clear(); + } + if (!corpus_path.empty()) + out << " path='" << xml::escape_xml_string(corpus_path) << "'"; + + if (!corpus->get_architecture_name().empty() + && ctxt.get_write_architecture()) out << " architecture='" << corpus->get_architecture_name()<< "'"; if (!corpus->get_soname().empty()) @@ -4247,10 +4322,10 @@ std::ostream& out = ctxt.get_ostream(); out << "get_path().empty()) + if (!group->get_path().empty() && ctxt.get_write_corpus_path()) out << " path='" << xml::escape_xml_string(group->get_path()) << "'"; - if (!group->get_architecture_name().empty()) + if (!group->get_architecture_name().empty() && ctxt.get_write_architecture()) out << " architecture='" << group->get_architecture_name()<< "'"; if (group->is_empty()) @@ -4266,7 +4341,7 @@ std::ostream& out = ctxt.get_ostream(); group->get_corpora().begin(); c != group->get_corpora().end(); ++c) - write_corpus(ctxt, *c, get_indent_to_level(ctxt, indent, 1)); + write_corpus(ctxt, *c, get_indent_to_level(ctxt, indent, 1), true); do_indent_to_level(ctxt, indent, 0); out << "\n"; diff --git a/tools/abidw.cc b/tools/abidw.cc index 33b7c76b..31ca093c 100644 --- a/tools/abidw.cc +++ b/tools/abidw.cc @@ -481,11 +481,6 @@ load_corpus_and_write_abixml(char* argv[], if (opts.noout) return 0; - if (!opts.write_architecture) - corp->set_architecture_name(""); - if (!opts.write_corpus_path) - corp->set_path(""); - if (!opts.out_file_path.empty()) { ofstream of(opts.out_file_path.c_str(), std::ios_base::trunc); @@ -548,11 +543,6 @@ load_kernel_corpus_group_and_write_abixml(char* argv[], if (!group) return 1; - if (!opts.write_architecture) - group->set_architecture_name(""); - if (!opts.write_corpus_path) - group->set_path(""); - if (!opts.noout) { const xml_writer::write_context_sptr& ctxt -- 2.43.5