From 88ae73fdf9a8eb8c1fdbf2fe9f90bef28d517569 Mon Sep 17 00:00:00 2001 From: Dodji Seketeli Date: Sat, 15 Aug 2015 00:03:07 +0200 Subject: [PATCH] Avoid declaring a type several times in the same TU in the XML format It appears a lot of duplicated type declarations can appear in a given translation unit. This patch avoids that. * src/abg-writer.cc (write_context::{record_type_id_as_emitted, record_type_as_emitted, type_id_is_emitted, type_is_emitted, clear_emitted_types_map}): New member functions. (write_context::m_emitted_type_id_map): New data member. (write_translation_unit): Clear the per-translation unit map of emitted types. Do not emit a type that has already been emitted in this translation unit. (write_namespace_decl): Do not emit a type that has already been emitted in this translation unit. (write_type_decl, write_qualified_type_def) (write_pointer_type_def, write_reference_type_def) (write_array_type_def, write_typedef_decl, write_class_decl) (write_type_tparameter, write_template_tparameter): Record the type we've just written as having been written out. * tests/data/test-read-dwarf/test9-pr18818-clang.so.abi: Adjust as duplicated declarations got removed. Signed-off-by: Dodji Seketeli --- src/abg-writer.cc | 91 +++++++++++++++++++ .../test9-pr18818-clang.so.abi | 8 -- 2 files changed, 91 insertions(+), 8 deletions(-) diff --git a/src/abg-writer.cc b/src/abg-writer.cc index e0dcc969..28ba7f03 100644 --- a/src/abg-writer.cc +++ b/src/abg-writer.cc @@ -200,6 +200,56 @@ public: clear_type_id_map() {m_type_id_map.clear();} + /// Record a given type id as belonging to a type that as been + /// written out to the XML output. + /// + /// @param id the ID of the type. + void + record_type_id_as_emitted(const string& id) + {m_emitted_type_id_map[id] = true;} + + /// Flag a type as having been written out to the XML output. + /// + /// @param t the type to flag. + void + record_type_as_emitted(const type_base_sptr& t) + { + string id = get_id_for_type(t); + record_type_id_as_emitted(id); + } + + /// Test if a given type ID belongs to a type that has been written + /// out to the XML output. + /// + /// @param id the ID of the type to test. + /// + /// @return true if the type has already been emitted, false + /// otherwise. + bool + type_id_is_emitted(const string& id) + {return m_emitted_type_id_map.find(id) != m_emitted_type_id_map.end();} + + /// Test if a given type has been written out to the XML output. + /// + /// @param the type to test for. + /// + /// @return true if the type has already been emitted, false + /// otherwise. + bool + type_is_emitted(const type_base_sptr& t) + { + if (!type_has_existing_id(t)) + return false; + string id = get_id_for_type(t); + return type_id_is_emitted(id); + } + + /// Clear the map that contains the IDs of the types that has been + /// recorded as having been written out to the XML output. + void + clear_emitted_types_map() + {m_emitted_type_id_map.clear();} + const string_elf_symbol_sptr_map_type& get_fun_symbol_map() const {return m_fun_symbol_map;} @@ -213,6 +263,7 @@ private: config m_config; ostream& m_ostream; type_ptr_map m_type_id_map; + unordered_map m_emitted_type_id_map; fn_tmpl_shared_ptr_map m_fn_tmpl_id_map; class_tmpl_shared_ptr_map m_class_tmpl_id_map; string_elf_symbol_sptr_map_type m_fun_symbol_map; @@ -858,6 +909,15 @@ write_translation_unit(const translation_unit& tu, ostream& o = ctxt.get_ostream(); const config& c = ctxt.get_config(); + // In a given translation unit, we'd like to ensure that a given + // type is defined only once. The same type can be present in + // several translation units, though. They'll be canonicalized + // later, by the reader's code. + // + // So lets clear the map that contains the types that are emitted in + // the translation unit tu. + ctxt.clear_emitted_types_map(); + do_indent(o, indent); o << ""; + ctxt.record_type_as_emitted(d); + return true; } @@ -1030,6 +1097,11 @@ write_namespace_decl(const shared_ptr decl, for (const_iterator i = d.begin(); i != d.end(); ++i) { + if (type_base_sptr t = is_type(*i)) + if (ctxt.type_is_emitted(t)) + // This type has already been emitted to the current + // translation unit so do not emit it again. + continue; o << "\n"; write_decl(*i, ctxt, indent + c.get_xml_element_indent()); } @@ -1091,6 +1163,8 @@ write_qualified_type_def(const qualified_type_def_sptr decl, o<< " id='" << i << "'/>"; + ctxt.record_type_as_emitted(decl); + return true; } @@ -1155,6 +1229,8 @@ write_pointer_type_def(const pointer_type_def_sptr decl, write_location(static_pointer_cast(decl), o); o << "/>"; + ctxt.record_type_as_emitted(decl); + return true; } @@ -1222,6 +1298,9 @@ write_reference_type_def(const reference_type_def_sptr decl, write_location(static_pointer_cast(decl), o); o << "/>"; + + ctxt.record_type_as_emitted(decl); + return true; } @@ -1315,6 +1394,8 @@ write_array_type_def(const array_type_def_sptr decl, o << ""; } + ctxt.record_type_as_emitted(decl); + return true; } @@ -1392,6 +1473,8 @@ write_enum_type_decl(const enum_type_decl_sptr decl, do_indent(o, indent); o << ""; + ctxt.record_type_as_emitted(decl); + return true; } @@ -1565,6 +1648,8 @@ write_typedef_decl(const typedef_decl_sptr decl, o << " id='" << i << "'/>"; + ctxt.record_type_as_emitted(decl); + return true; } @@ -1924,6 +2009,8 @@ write_class_decl(const class_decl_sptr decl, o << ""; } + ctxt.record_type_as_emitted(decl); + return true; } @@ -2032,6 +2119,8 @@ write_type_tparameter(const type_tparameter_sptr decl, o << "/>"; + ctxt.record_type_as_emitted(decl); + return true; } @@ -2117,6 +2206,8 @@ write_template_tparameter (const template_tparameter_sptr decl, do_indent_to_level(ctxt, indent, 0); o << ""; + ctxt.record_type_as_emitted(decl); + return true; } diff --git a/tests/data/test-read-dwarf/test9-pr18818-clang.so.abi b/tests/data/test-read-dwarf/test9-pr18818-clang.so.abi index 6b65ffe5..e2d456c0 100644 --- a/tests/data/test-read-dwarf/test9-pr18818-clang.so.abi +++ b/tests/data/test-read-dwarf/test9-pr18818-clang.so.abi @@ -1136,7 +1136,6 @@ - @@ -4498,7 +4497,6 @@ - @@ -7466,7 +7464,6 @@ - @@ -9302,7 +9299,6 @@ - @@ -10527,7 +10523,6 @@ - @@ -13152,7 +13147,6 @@ - @@ -14818,7 +14812,6 @@ - @@ -15310,7 +15303,6 @@ - -- 2.43.5