Make write_architecture and write_corpus_path flags in the write_context
authorMatthias Maennich <maennich@google.com>
Tue, 21 May 2019 04:39:20 +0000 (05:39 +0100)
committerDodji Seketeli <dodji@redhat.com>
Wed, 22 May 2019 12:33:45 +0000 (14:33 +0200)
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 <maennich@google.com>
Signed-off-by: Dodji Seketeli <dodji@redhat.com>
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 <dodji@redhat.com>
include/abg-writer.h
src/abg-writer.cc
tools/abidw.cc

index 200b5f7..729b455 100644 (file)
@@ -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);
index d67e64f..3792bd2 100644 (file)
@@ -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 << "<abi-corpus";
-  if (!corpus->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 << "<abi-corpus-group";
 
-  if (!group->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 << "</abi-corpus-group>\n";
index 33b7c76..31ca093 100644 (file)
@@ -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