[17/21] Refactor suppression property string parsing.

Message ID 20200423154441.170531-18-gprocida@google.com
State Superseded
Headers
Series Simplify regex and suppression parsing. |

Commit Message

Giuliano Procida April 23, 2020, 3:44 p.m. UTC
  This patch introduces a helper function to look up a key in an ini
configuration section and get a string value. All string look-ups now
use this.

There are no behavioural changes.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 src/abg-suppression.cc | 198 ++++++++++++++++-------------------------
 1 file changed, 78 insertions(+), 120 deletions(-)
  

Patch

diff --git a/src/abg-suppression.cc b/src/abg-suppression.cc
index fa382e02..13431eef 100644
--- a/src/abg-suppression.cc
+++ b/src/abg-suppression.cc
@@ -1598,6 +1598,31 @@  read_suppression_reach_kind(const string& input)
     return type_suppression::DIRECT_REACH_KIND;
 }
 
+/// Maybe fetch a property to a string.
+///
+/// Attempt to find a simple property in an ini section and copy it
+/// to a string
+///
+/// @param section the ini section to look in
+///
+/// @param prop the property name
+///
+/// @param str the string to compile into
+///
+/// @return whether the property was found
+static bool
+maybe_get_string_prop(const ini::config::section& section,
+                      const std::string& name,
+                      std::string& str)
+{
+  ini::simple_property_sptr prop =
+    is_simple_property(section.find_property(name));
+  if (!prop)
+    return false;
+  str = prop->get_value()->as_string();
+  return true;
+}
+
 /// Read a type suppression from an instance of ini::config::section
 /// and build a @ref type_suppression as a result.
 ///
@@ -1613,18 +1638,12 @@  read_type_suppression(const ini::config::section& section)
   if (section.get_name() != "suppress_type")
     return result;
 
-  ini::simple_property_sptr drop_artifact =
-    is_simple_property(section.find_property("drop_artifact"));
-  if (!drop_artifact)
-    drop_artifact = is_simple_property(section.find_property("drop"));
-
-  string drop_artifact_str = drop_artifact
-    ? drop_artifact->get_value()->as_string()
-    : "";
+  string drop_artifact_str;
+  maybe_get_string_prop(section, "drop_artifact", drop_artifact_str)
+    || maybe_get_string_prop(section, "drop", drop_artifact_str);
 
-  ini::simple_property_sptr label =
-    is_simple_property(section.find_property("label"));
-  string label_str = label ? label->get_value()->as_string() : "";
+  string label_str;
+  maybe_get_string_prop(section, "label", label_str);
 
   ini::simple_property_sptr file_name_regex_prop =
     is_simple_property(section.find_property("file_name_regexp"));
@@ -1666,11 +1685,8 @@  read_type_suppression(const ini::config::section& section)
     name_not_regex =
       regex::compile(name_not_regex_prop->get_value()->as_string());
 
-  ini::simple_property_sptr name_prop =
-    is_simple_property(section.find_property("name"));
-  string name_str = name_prop
-    ? name_prop->get_value()->as_string()
-    : "";
+  string name_str;
+  maybe_get_string_prop(section, "name", name_str);
 
   ini::property_sptr srcloc_not_in_prop =
     section.find_property("source_location_not_in");
@@ -1701,25 +1717,19 @@  read_type_suppression(const ini::config::section& section)
     srcloc_not_regex =
       regex::compile(srcloc_not_regexp_prop->get_value()->as_string());
 
-  bool consider_type_kind = false;
-  type_suppression::type_kind type_kind = type_suppression::UNKNOWN_TYPE_KIND;
-  if (ini::simple_property_sptr type_kind_prop =
-      is_simple_property(section.find_property("type_kind")))
-    {
-      consider_type_kind = true;
-      type_kind =
-	read_type_kind_string(type_kind_prop->get_value()->as_string());
-    }
+  std::string type_kind_str;
+  bool consider_type_kind =
+    maybe_get_string_prop(section, "type_kind", type_kind_str);
+  type_suppression::type_kind type_kind = consider_type_kind
+    ? read_type_kind_string(type_kind_str)
+    : type_suppression::UNKNOWN_TYPE_KIND;
 
-  bool consider_reach_kind = false;
-  type_suppression::reach_kind reach_kind = type_suppression::DIRECT_REACH_KIND;
-  if (ini::simple_property_sptr reach_kind_prop =
-      is_simple_property(section.find_property("accessed_through")))
-    {
-      consider_reach_kind = true;
-      reach_kind =
-	read_suppression_reach_kind(reach_kind_prop->get_value()->as_string());
-    }
+  std::string reach_kind_str;
+  bool consider_reach_kind =
+      maybe_get_string_prop(section, "accessed_through", reach_kind_str);
+  type_suppression::reach_kind reach_kind = consider_reach_kind
+    ? read_suppression_reach_kind(reach_kind_str)
+    : type_suppression::DIRECT_REACH_KIND;
 
   // Support has_data_member_inserted_at
   vector<type_suppression::insertion_range_sptr> insert_ranges;
@@ -3179,26 +3189,15 @@  read_function_suppression(const ini::config::section& section)
   if (section.get_name() != "suppress_function")
     return result;
 
-  ini::simple_property_sptr drop_artifact =
-    is_simple_property(section.find_property("drop_artifact"));
-  if (!drop_artifact)
-    drop_artifact = is_simple_property(section.find_property("drop"));
-
-  string drop_artifact_str = drop_artifact
-    ? drop_artifact->get_value()->as_string()
-    : "";
+  string drop_artifact_str;
+  maybe_get_string_prop(section, "drop_artifact", drop_artifact_str)
+    || maybe_get_string_prop(section, "drop", drop_artifact_str);
 
-  ini::simple_property_sptr change_kind_prop =
-    is_simple_property(section.find_property("change_kind"));
-  string change_kind_str = change_kind_prop
-    ? change_kind_prop->get_value()->as_string()
-    : "";
+  string change_kind_str;
+  maybe_get_string_prop(section, "change_kind", change_kind_str);
 
-  ini::simple_property_sptr label_prop =
-    is_simple_property(section.find_property("label"));
-  string label_str = label_prop
-    ? label_prop->get_value()->as_string()
-    : "";
+  string label_str;
+  maybe_get_string_prop(section, "label", label_str);
 
   ini::simple_property_sptr file_name_regex_prop =
     is_simple_property(section.find_property("file_name_regexp"));
@@ -3227,11 +3226,8 @@  read_function_suppression(const ini::config::section& section)
     soname_not_regex =
       regex::compile(soname_not_regex_prop->get_value()->as_string());
 
-  ini::simple_property_sptr name_prop =
-    is_simple_property(section.find_property("name"));
-  string name = name_prop
-    ? name_prop->get_value()->as_string()
-    : "";
+  string name;
+  maybe_get_string_prop(section, "name", name);
 
   ini::simple_property_sptr name_regex_prop =
     is_simple_property(section.find_property("name_regexp"));
@@ -3246,11 +3242,8 @@  read_function_suppression(const ini::config::section& section)
     name_not_regex =
       regex::compile(name_not_regex_prop->get_value()->as_string());
 
-  ini::simple_property_sptr return_type_name_prop =
-    is_simple_property(section.find_property("return_type_name"));
-  string return_type_name = return_type_name_prop
-    ? return_type_name_prop->get_value()->as_string()
-    : "";
+  string return_type_name;
+  maybe_get_string_prop(section, "return_type_name", return_type_name);
 
   ini::simple_property_sptr return_type_regex_prop =
     is_simple_property(section.find_property("return_type_regexp"));
@@ -3259,11 +3252,8 @@  read_function_suppression(const ini::config::section& section)
     return_type_regex =
       regex::compile(return_type_regex_prop->get_value()->as_string());
 
-  ini::simple_property_sptr sym_name_prop =
-    is_simple_property(section.find_property("symbol_name"));
-  string sym_name = sym_name_prop
-    ? sym_name_prop->get_value()->as_string()
-    : "";
+  string sym_name;
+  maybe_get_string_prop(section, "symbol_name", sym_name);
 
   ini::simple_property_sptr sym_name_regex_prop =
     is_simple_property(section.find_property("symbol_name_regexp"));
@@ -3279,11 +3269,8 @@  read_function_suppression(const ini::config::section& section)
     sym_name_not_regex =
       regex::compile(sym_name_not_regex_prop->get_value()->as_string());
 
-  ini::simple_property_sptr sym_ver_prop =
-    is_simple_property(section.find_property("symbol_version"));
-  string sym_version = sym_ver_prop
-    ? sym_ver_prop->get_value()->as_string()
-    : "";
+  string sym_version;
+  maybe_get_string_prop(section, "symbol_version", sym_version);
 
   ini::simple_property_sptr sym_ver_regex_prop =
     is_simple_property(section.find_property("symbol_version_regexp"));
@@ -3292,11 +3279,8 @@  read_function_suppression(const ini::config::section& section)
     sym_ver_regex =
       regex::compile(sym_ver_regex_prop->get_value()->as_string());
 
-  ini::simple_property_sptr allow_other_aliases_prop =
-    is_simple_property(section.find_property("allow_other_aliases"));
-  string allow_other_aliases = allow_other_aliases_prop
-    ? allow_other_aliases_prop->get_value()->as_string()
-    : "";
+  string allow_other_aliases;
+  maybe_get_string_prop(section, "allow_other_aliases", allow_other_aliases);
 
   function_suppression::parameter_spec_sptr parm;
   function_suppression::parameter_specs_type parms;
@@ -4035,26 +4019,15 @@  read_variable_suppression(const ini::config::section& section)
   if (section.get_name() != "suppress_variable")
     return result;
 
-  ini::simple_property_sptr drop_artifact =
-    is_simple_property(section.find_property("drop_artifact"));
-  if (!drop_artifact)
-    drop_artifact = is_simple_property(section.find_property("drop"));
-
-  string drop_artifact_str = drop_artifact
-    ? drop_artifact->get_value()->as_string()
-    : "";
+  string drop_artifact_str;
+  maybe_get_string_prop(section, "drop_artifact", drop_artifact_str)
+    || maybe_get_string_prop(section, "drop", drop_artifact_str);
 
-  ini::simple_property_sptr change_kind_prop =
-    is_simple_property(section.find_property("change_kind"));
-  string change_kind_str = change_kind_prop
-    ? change_kind_prop->get_value()->as_string()
-    : "";
+  string change_kind_str;
+  maybe_get_string_prop(section, "change_kind", change_kind_str);
 
-  ini::simple_property_sptr label_prop =
-    is_simple_property(section.find_property("label"));
-  string label_str = (label_prop
-		      ? label_prop->get_value()->as_string()
-		      : "");
+  string label_str;
+  maybe_get_string_prop(section, "label", label_str);
 
   ini::simple_property_sptr file_name_regex_prop =
     is_simple_property(section.find_property("file_name_regexp"));
@@ -4083,11 +4056,8 @@  read_variable_suppression(const ini::config::section& section)
     soname_not_regex =
       regex::compile(soname_not_regex_prop->get_value()->as_string());
 
-  ini::simple_property_sptr name_prop =
-    is_simple_property(section.find_property("name"));
-  string name_str = (name_prop
-		     ? name_prop->get_value()->as_string()
-		     : "");
+  string name_str;
+  maybe_get_string_prop(section, "name", name_str);
 
   ini::simple_property_sptr name_regex_prop =
     is_simple_property(section.find_property("name_regexp"));
@@ -4102,11 +4072,8 @@  read_variable_suppression(const ini::config::section& section)
     name_not_regex =
       regex::compile(name_not_regex_prop->get_value()->as_string());
 
-  ini::simple_property_sptr sym_name_prop =
-    is_simple_property(section.find_property("symbol_name"));
-  string symbol_name = (sym_name_prop
-			? sym_name_prop->get_value()->as_string()
-			: "");
+  string symbol_name;
+  maybe_get_string_prop(section, "symbol_name", symbol_name);
 
   ini::simple_property_sptr sym_name_regex_prop =
     is_simple_property(section.find_property("symbol_name_regexp"));
@@ -4122,11 +4089,8 @@  read_variable_suppression(const ini::config::section& section)
     symbol_name_not_regex =
       regex::compile(sym_name_not_regex_prop->get_value()->as_string());
 
-  ini::simple_property_sptr sym_version_prop =
-    is_simple_property(section.find_property("symbol_version"));
-  string symbol_version = sym_version_prop
-    ? sym_version_prop->get_value()->as_string()
-    : "";
+  string symbol_version;
+  maybe_get_string_prop(section, "symbol_version", symbol_version);
 
   ini::simple_property_sptr sym_version_regex_prop =
     is_simple_property(section.find_property("symbol_version_regexp"));
@@ -4135,11 +4099,8 @@  read_variable_suppression(const ini::config::section& section)
     symbol_version_regex =
       regex::compile(sym_version_regex_prop->get_value()->as_string());
 
-  ini::simple_property_sptr type_name_prop =
-    is_simple_property(section.find_property("type_name"));
-  string type_name_str = type_name_prop
-    ? type_name_prop->get_value()->as_string()
-    : "";
+  string type_name_str;
+  maybe_get_string_prop(section, "type_name", type_name_str);
 
   ini::simple_property_sptr type_name_regex_prop =
     is_simple_property(section.find_property("type_name_regexp"));
@@ -4286,11 +4247,8 @@  read_file_suppression(const ini::config::section& section)
   if (section.get_name() != "suppress_file")
     return result;
 
-  ini::simple_property_sptr label_prop =
-    is_simple_property(section.find_property("label"));
-  string label_str = (label_prop
-		      ? label_prop->get_value()->as_string()
-		      : "");
+  string label_str;
+  maybe_get_string_prop(section, "label", label_str);
 
   ini::simple_property_sptr file_name_regex_prop =
     is_simple_property(section.find_property("file_name_regexp"));