XML writer: make sort_types return values
There are currently 3 overloads of sort_values, all of which modify a
given, presumed empty, vector but which take different argument types:
- type_ptr_set_type
- istring_type_base_wptr_map_type - not used
- vector<function_type_sptr>
We can relying on the return value optimisation which makes for more
readable code. The vectors no longer need to be assumed empty and
space can reserved to match the input.
* src/abg-writer.cc (sort_types): Drop the unused
istring_type_base_wptr_map_type overload. Change the remaining
overloads to reserve the exact number of elements and return
the sorted vector by value. Use std:: prefix for vector and
sort.
(write_referenced_types): Update call to sort_types. Use std::
prefix for vector.
(write_translation_unit): Likewise. Also iterate over types
using a range-based for loop.
Change-Id: Ie7c19205ee43da897cb0bd26054709decc29c38d
Signed-off-by: Giuliano Procida <gprocida@google.com>
diff --git a/src/abg-writer.cc b/src/abg-writer.cc
index 857d833..1d32233 100644
--- a/src/abg-writer.cc
+++ b/src/abg-writer.cc
@@ -633,40 +633,17 @@
///
/// @param types the map to sort.
///
- /// @param sorted the resulted sorted vector. It's set by this
- /// function with the result of the sorting.
- void
- sort_types(type_ptr_set_type& types,
- vector<type_base*>& sorted)
+ /// @return the resulting sorted vector.
+ std::vector<type_base*>
+ sort_types(type_ptr_set_type& types)
{
- string id;
- for (type_ptr_set_type::const_iterator i = types.begin();
- i != types.end();
- ++i)
- sorted.push_back(const_cast<type_base*>(*i));
+ std::vector<type_base*> sorted;
+ sorted.reserve(types.size());
+ for (const type_base* t : types)
+ sorted.push_back(const_cast<type_base*>(t));
type_ptr_cmp comp(&m_type_id_map);
- sort(sorted.begin(), sorted.end(), comp);
- }
-
- /// Sort the content of a map of type pointers into a vector.
- ///
- /// The pointers are sorted by using their string representation as
- /// the key to sort, lexicographically.
- ///
- /// @param types the map to sort.
- ///
- /// @param sorted the resulted sorted vector. It's set by this
- /// function with the result of the sorting.
- void
- sort_types(const istring_type_base_wptr_map_type& types,
- vector<type_base_sptr> &sorted)
- {
- for (istring_type_base_wptr_map_type::const_iterator i = types.begin();
- i != types.end();
- ++i)
- sorted.push_back(type_base_sptr(i->second));
- type_ptr_cmp comp(&m_type_id_map);
- sort(sorted.begin(), sorted.end(), comp);
+ std::sort(sorted.begin(), sorted.end(), comp);
+ return sorted;
}
/// Sort the content of a vector of function types into a vector of
@@ -675,20 +652,19 @@
/// The pointers are sorted by using their string representation as
/// the key to sort, lexicographically.
///
- /// @param types the vector of function types to store.
+ /// @param types the vector of function types to sort.
///
- /// @param sorted the resulted sorted vector. It's set by this
- /// function with the result of the sorting.
- void
- sort_types(const vector<function_type_sptr>& types,
- vector<type_base_sptr> &sorted)
+ /// @return the resulting sorted vector.
+ std::vector<type_base_sptr>
+ sort_types(const vector<function_type_sptr>& types)
{
- for (vector<function_type_sptr>::const_iterator i = types.begin();
- i != types.end();
- ++i)
- sorted.push_back(*i);
+ std::vector<type_base_sptr> sorted;
+ sorted.reserve(types.size());
+ for (const auto& t : types)
+ sorted.push_back(t);
type_ptr_cmp comp(&m_type_id_map);
- sort(sorted.begin(), sorted.end(), comp);
+ std::sort(sorted.begin(), sorted.end(), comp);
+ return sorted;
}
/// Flag a type as having been written out to the XML output.
@@ -2256,8 +2232,8 @@
// But first, we need to sort them, otherwise, emitting the ABI
// (in xml) of the same binary twice will yield different
// results, because we'd be walking an *unordered* hash table.
- vector<type_base*> sorted_types;
- ctxt.sort_types(referenced_types_to_emit, sorted_types);
+ std::vector<type_base*> sorted_types =
+ ctxt.sort_types(referenced_types_to_emit);
// Now, emit the referenced decls in a sorted order.
for (type_base* t : sorted_types)
@@ -2379,15 +2355,12 @@
// Now handle all function types that were not only referenced by
// emitted types.
- const vector<function_type_sptr>& t = tu.get_live_fn_types();
- vector<type_base_sptr> sorted_types;
- ctxt.sort_types(t, sorted_types);
+ std::vector<type_base_sptr> sorted_types =
+ ctxt.sort_types(tu.get_live_fn_types());
- for (vector<type_base_sptr>::const_iterator i = sorted_types.begin();
- i != sorted_types.end();
- ++i)
+ for (const type_base_sptr& t : sorted_types)
{
- function_type_sptr fn_type = is_function_type(*i);
+ function_type_sptr fn_type = is_function_type(t);
if (fn_type->get_is_artificial() || ctxt.type_is_emitted(fn_type))
// This function type is either already emitted or it's