diff mbox series

[3/3] Fix performance-unnecessary-copy-initialization warnings

Message ID 20200903132205.589136-4-gprocida@google.com
State New
Headers show
Series clang-tidy error and warning fixes | expand

Commit Message

Giuliano Procida Sept. 3, 2020, 1:22 p.m. UTC
clang-tidy's performance-unnecessary-copy-initialization warning is
triggered when it detects unncessary copying of objects. In the case
of the shared_ptr types used by libabigail, the extra cost is
primarily that of manipulating reference counts.

This commit addresses these warnings. Note that while they are
unlikely to be genuine performance issues, eliminating them means
we'll be able to spot new issues more easily.

	* include/abg-diff-utils.h (compute_diff): Take const
	reference to point instead of copying.
	* src/abg-comparison-priv.h (elf_symbol_comp::operator()):
	Take const references to strings instead of copying.
	* src/abg-default-reporter.cc (default_reporter::report): Take
	const references to diff_sptrs instead of copying.
	* src/abg-dwarf-reader.cc (die_location): Take a const
	reference to translation_unit_sptr instead of copying.
	* src/abg-ir.cc (function_decls_alias): Take const references
	to elf_symbol_sptrs instead of copying.
	(virtual_member_function_less_than::operator()): Take const
	reference to location instead of copying.
	(methods_equal_modulo_elf_symbol): Modify objects through
	given shared pointers rather than copies thereof.
	* src/abg-suppression.cc
	(function_suppression::suppresses_function): Take const
	references to elf_symbol_sptrs instead of copying them.
	* tools/abipkgdiff.cc (create_private_types_suppressions):
	Take const reference to package_sptr instead of copying.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 include/abg-diff-utils.h    |  2 +-
 src/abg-comparison-priv.h   |  3 ++-
 src/abg-default-reporter.cc |  4 ++--
 src/abg-dwarf-reader.cc     |  2 +-
 src/abg-ir.cc               | 11 ++++++-----
 src/abg-suppression.cc      |  8 ++++----
 tools/abipkgdiff.cc         |  2 +-
 7 files changed, 17 insertions(+), 15 deletions(-)
diff mbox series

Patch

diff --git a/include/abg-diff-utils.h b/include/abg-diff-utils.h
index b59ffa04f..d113e8380 100644
--- a/include/abg-diff-utils.h
+++ b/include/abg-diff-utils.h
@@ -1623,7 +1623,7 @@  compute_diff(RandomAccessOutputIterator a_base,
 
       if (snak.has_vertical_edge())
 	{
-	  point p = snak.intermediate();
+	  const point& p = snak.intermediate();
 	  insertion ins(p.x());
 	  ins.inserted_indexes().push_back(p.y());
 	  ses.insertions().push_back(ins);
diff --git a/src/abg-comparison-priv.h b/src/abg-comparison-priv.h
index b82b5d070..8e17155fd 100644
--- a/src/abg-comparison-priv.h
+++ b/src/abg-comparison-priv.h
@@ -981,7 +981,8 @@  struct elf_symbol_comp
   bool
   operator()(const elf_symbol& l, const elf_symbol& r)
   {
-    string name1 = l.get_id_string(), name2 = r.get_id_string();
+    const string& name1 = l.get_id_string();
+    const string& name2 = r.get_id_string();
     return name1 < name2;
   }
 
diff --git a/src/abg-default-reporter.cc b/src/abg-default-reporter.cc
index f8572f25c..03c1137fd 100644
--- a/src/abg-default-reporter.cc
+++ b/src/abg-default-reporter.cc
@@ -489,7 +489,7 @@  default_reporter::report(const reference_diff& d, ostream& out,
     report_local_reference_type_changes(d, out, indent);
 
   if (k & SUBTYPE_CHANGE_KIND)
-    if (diff_sptr dif = d.underlying_type_diff())
+    if (const diff_sptr& dif = d.underlying_type_diff())
       {
 	RETURN_IF_BEING_REPORTED_OR_WAS_REPORTED_EARLIER2(dif,
 							  "referenced type");
@@ -680,7 +680,7 @@  default_reporter::report(const array_diff& d, ostream& out,
 						    d.second_array(),
 						    "array type");
 
-  diff_sptr dif = d.element_type_diff();
+  const diff_sptr& dif = d.element_type_diff();
   if (dif->to_be_reported())
     {
       string fn = ir::get_pretty_representation(is_type(dif->first_subject()));
diff --git a/src/abg-dwarf-reader.cc b/src/abg-dwarf-reader.cc
index 7c56bd8c4..88d775b99 100644
--- a/src/abg-dwarf-reader.cc
+++ b/src/abg-dwarf-reader.cc
@@ -8426,7 +8426,7 @@  die_location(const read_context& ctxt, const Dwarf_Die* die)
 
   if (!file.empty() && line != 0)
     {
-      translation_unit_sptr tu = ctxt.cur_transl_unit();
+      const translation_unit_sptr& tu = ctxt.cur_transl_unit();
       location l = tu->get_loc_mgr().create_new_location(file, line, 1);
       return l;
     }
diff --git a/src/abg-ir.cc b/src/abg-ir.cc
index 358a7a26a..68afd6ea1 100644
--- a/src/abg-ir.cc
+++ b/src/abg-ir.cc
@@ -17746,7 +17746,8 @@  function_decl::get_id() const
 bool
 function_decls_alias(const function_decl& f1, const function_decl& f2)
 {
-  elf_symbol_sptr s1 = f1.get_symbol(), s2 = f2.get_symbol();
+  const elf_symbol_sptr& s1 = f1.get_symbol();
+  const elf_symbol_sptr& s2 = f2.get_symbol();
 
   if (!s1 || !s2)
     return false;
@@ -20490,7 +20491,8 @@  struct virtual_member_function_less_than
 	  {
 	    string fn_filepath, sn_filepath;
 	    unsigned line = 0, column = 0;
-	    location fn_loc = f.get_location(), sn_loc = s.get_location();
+	    const location& fn_loc = f.get_location();
+	    const location& sn_loc = s.get_location();
 	    if (fn_loc)
 	      fn_loc.expand(fn_filepath, line, column);
 	    if (sn_loc)
@@ -20710,10 +20712,9 @@  class_decl::get_hash() const
 /// @return true iff @p f equals @p s without taking their linkage
 /// name or symbol into account.
 static bool
-methods_equal_modulo_elf_symbol(const method_decl_sptr& f,
-				const method_decl_sptr& s)
+methods_equal_modulo_elf_symbol(const method_decl_sptr& first,
+				const method_decl_sptr& second)
 {
-  method_decl_sptr first = f, second = s;
   elf_symbol_sptr saved_first_elf_symbol =
     first->get_symbol();
   elf_symbol_sptr saved_second_elf_symbol =
diff --git a/src/abg-suppression.cc b/src/abg-suppression.cc
index ae7cc95ce..d6ee9dc56 100644
--- a/src/abg-suppression.cc
+++ b/src/abg-suppression.cc
@@ -2500,7 +2500,7 @@  function_suppression::suppresses_function(const function_decl* fn,
 	  // function only if the suppression condition matches the
 	  // names of all aliases.
 	  string symbol_name;
-	  elf_symbol_sptr sym = fn->get_symbol();
+	  const elf_symbol_sptr& sym = fn->get_symbol();
 	  ABG_ASSERT(sym);
 	  symbol_name = sym->get_name();
 	  if (sym->has_aliases() && sym->get_alias_from_name(fname))
@@ -2534,7 +2534,7 @@  function_suppression::suppresses_function(const function_decl* fn,
 	  // function only if the suppression condition matches *all*
 	  // the aliases.
 	  string symbol_name;
-	  elf_symbol_sptr sym = fn->get_symbol();
+	  const elf_symbol_sptr& sym = fn->get_symbol();
 	  ABG_ASSERT(sym);
 	  symbol_name = sym->get_name();
 	  if (sym->has_aliases())
@@ -2565,7 +2565,7 @@  function_suppression::suppresses_function(const function_decl* fn,
 	  // function only if the suppression condition matches *all*
 	  // the aliases.
 	  string symbol_name;
-	  elf_symbol_sptr sym = fn->get_symbol();
+	  const elf_symbol_sptr& sym = fn->get_symbol();
 	  ABG_ASSERT(sym);
 	  symbol_name = sym->get_name();
 	  if (sym->has_aliases())
@@ -2604,7 +2604,7 @@  function_suppression::suppresses_function(const function_decl* fn,
   // Check if the "symbol_name", "symbol_name_regexp", and
   // "symbol_name_not_regexp" properties match.
   string fn_sym_name, fn_sym_version;
-  elf_symbol_sptr sym = fn->get_symbol();
+  const elf_symbol_sptr& sym = fn->get_symbol();
   if (sym)
     {
       fn_sym_name = sym->get_name();
diff --git a/tools/abipkgdiff.cc b/tools/abipkgdiff.cc
index 445e24068..c24868afd 100644
--- a/tools/abipkgdiff.cc
+++ b/tools/abipkgdiff.cc
@@ -1489,7 +1489,7 @@  create_private_types_suppressions(const package& pkg, const options &opts)
 {
   suppressions_type supprs;
 
-  package_sptr devel_pkg = pkg.devel_package();
+  const package_sptr& devel_pkg = pkg.devel_package();
   if (!devel_pkg
       || !file_exists(devel_pkg->extracted_dir_path())
       || !is_dir(devel_pkg->extracted_dir_path()))