diff mbox series

[18/21] Refactor suppression property regex parsing.

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

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 compiled regex value. All regex
look-ups now use this.

There are no behavioural changes.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 src/abg-suppression.cc | 215 ++++++++++++-----------------------------
 1 file changed, 61 insertions(+), 154 deletions(-)
diff mbox series

Patch

diff --git a/src/abg-suppression.cc b/src/abg-suppression.cc
index 13431eef..3db2b7b1 100644
--- a/src/abg-suppression.cc
+++ b/src/abg-suppression.cc
@@ -1623,6 +1623,30 @@  maybe_get_string_prop(const ini::config::section& section,
   return true;
 }
 
+/// Maybe compile a property to a regex.
+///
+/// Attempt to find a simple property in an ini section and compile it
+/// to a regex.
+///
+/// @param section the ini section to look in
+///
+/// @param prop the property name
+///
+/// @param r the regex to compile into
+///
+/// @return whether the property was found
+static bool
+maybe_get_regex_prop(const ini::config::section& section,
+                     const std::string& name,
+                     regex_t_sptr& regex)
+{
+  string str;
+  if (!maybe_get_string_prop(section, name, str))
+    return false;
+  regex = regex::compile(str);
+  return true;
+}
+
 /// Read a type suppression from an instance of ini::config::section
 /// and build a @ref type_suppression as a result.
 ///
@@ -1645,45 +1669,23 @@  read_type_suppression(const ini::config::section& section)
   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"));
   regex_t_sptr file_name_regex;
-  if (file_name_regex_prop)
-    file_name_regex =
-      regex::compile(file_name_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "file_name_regexp", file_name_regex);
 
-  ini::simple_property_sptr file_name_not_regex_prop =
-    is_simple_property(section.find_property("file_name_not_regexp"));
   regex_t_sptr file_name_not_regex;
-  if (file_name_not_regex_prop)
-    file_name_not_regex =
-      regex::compile(file_name_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "file_name_not_regexp", file_name_not_regex);
 
-  ini::simple_property_sptr soname_regex_prop =
-    is_simple_property(section.find_property("soname_regexp"));
   regex_t_sptr soname_regex;
-  if (soname_regex_prop)
-    soname_regex = regex::compile(soname_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "soname_regexp", soname_regex);
 
-  ini::simple_property_sptr soname_not_regex_prop =
-    is_simple_property(section.find_property("soname_not_regexp"));
   regex_t_sptr soname_not_regex;
-  if (soname_not_regex_prop)
-    soname_not_regex =
-      regex::compile(soname_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "soname_not_regexp", soname_not_regex);
 
-  ini::simple_property_sptr name_regex_prop =
-    is_simple_property(section.find_property("name_regexp"));
   regex_t_sptr name_regex;
-  if (name_regex_prop)
-    name_regex = regex::compile(name_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "name_regexp", name_regex);
 
-  ini::simple_property_sptr name_not_regex_prop =
-    is_simple_property(section.find_property("name_not_regexp"));
   regex_t_sptr name_not_regex;
-  if (name_not_regex_prop)
-    name_not_regex =
-      regex::compile(name_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "name_not_regexp", name_not_regex);
 
   string name_str;
   maybe_get_string_prop(section, "name", name_str);
@@ -1710,12 +1712,8 @@  read_type_suppression(const ini::config::section& section)
 	}
     }
 
-  ini::simple_property_sptr srcloc_not_regexp_prop =
-    is_simple_property(section.find_property("source_location_not_regexp"));
   regex_t_sptr srcloc_not_regex;
-  if (srcloc_not_regexp_prop)
-    srcloc_not_regex =
-      regex::compile(srcloc_not_regexp_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "source_location_not_regexp", srcloc_not_regex);
 
   std::string type_kind_str;
   bool consider_type_kind =
@@ -3199,85 +3197,47 @@  read_function_suppression(const ini::config::section& section)
   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"));
   regex_t_sptr file_name_regex;
-  if (file_name_regex_prop)
-    file_name_regex =
-      regex::compile(file_name_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "file_name_regexp", file_name_regex);
 
-  ini::simple_property_sptr file_name_not_regex_prop =
-    is_simple_property(section.find_property("file_name_not_regexp"));
   regex_t_sptr file_name_not_regex;
-  if (file_name_not_regex_prop)
-    file_name_not_regex =
-      regex::compile(file_name_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "file_name_not_regexp", file_name_not_regex);
 
-  ini::simple_property_sptr soname_regex_prop =
-    is_simple_property(section.find_property("soname_regexp"));
   regex_t_sptr soname_regex;
-  if (soname_regex_prop)
-    soname_regex = regex::compile(soname_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "soname_regexp", soname_regex);
 
-  ini::simple_property_sptr soname_not_regex_prop =
-    is_simple_property(section.find_property("soname_not_regexp"));
   regex_t_sptr soname_not_regex;
-  if (soname_not_regex_prop)
-    soname_not_regex =
-      regex::compile(soname_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "soname_not_regexp", soname_not_regex);
 
   string name;
   maybe_get_string_prop(section, "name", name);
 
-  ini::simple_property_sptr name_regex_prop =
-    is_simple_property(section.find_property("name_regexp"));
   regex_t_sptr name_regex;
-  if (name_regex_prop)
-    name_regex = regex::compile(name_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "name_regexp", name_regex);
 
-  ini::simple_property_sptr name_not_regex_prop =
-    is_simple_property(section.find_property("name_not_regexp"));
   regex_t_sptr name_not_regex;
-  if (name_not_regex_prop)
-    name_not_regex =
-      regex::compile(name_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "name_not_regexp", name_not_regex);
 
   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"));
   regex_t_sptr return_type_regex;
-  if (return_type_regex_prop)
-    return_type_regex =
-      regex::compile(return_type_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "return_type_regexp", return_type_regex);
 
   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"));
   regex_t_sptr sym_name_regex;
-  if (sym_name_regex_prop)
-    sym_name_regex =
-      regex::compile(sym_name_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "symbol_name_regexp", sym_name_regex);
 
-  ini::simple_property_sptr sym_name_not_regex_prop =
-    is_simple_property(section.find_property("symbol_name_not_regexp"));
   regex_t_sptr sym_name_not_regex;
-  if (sym_name_not_regex_prop)
-    sym_name_not_regex =
-      regex::compile(sym_name_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "symbol_name_not_regexp", sym_name_not_regex);
 
   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"));
   regex_t_sptr sym_ver_regex;
-  if (sym_ver_regex_prop)
-    sym_ver_regex =
-      regex::compile(sym_ver_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "symbol_version_regexp", sym_ver_regex);
 
   string allow_other_aliases;
   maybe_get_string_prop(section, "allow_other_aliases", allow_other_aliases);
@@ -4029,85 +3989,47 @@  read_variable_suppression(const ini::config::section& section)
   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"));
   regex_t_sptr file_name_regex;
-  if (file_name_regex_prop)
-    file_name_regex =
-      regex::compile(file_name_regex_prop->get_value()->as_string());
-
- ini::simple_property_sptr file_name_not_regex_prop =
-  is_simple_property(section.find_property("file_name_not_regexp"));
- regex_t_sptr file_name_not_regex;
- if (file_name_not_regex_prop)
-   file_name_not_regex =
-     regex::compile(file_name_not_regex_prop->get_value()->as_string());
-
-  ini::simple_property_sptr soname_regex_prop =
-    is_simple_property(section.find_property("soname_regexp"));
+  maybe_get_regex_prop(section, "file_name_regexp", file_name_regex);
+
+  regex_t_sptr file_name_not_regex;
+  maybe_get_regex_prop(section, "file_name_not_regexp", file_name_not_regex);
+
   regex_t_sptr soname_regex;
-  if (soname_regex_prop)
-    soname_regex = regex::compile(soname_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "soname_regexp", soname_regex);
 
-  ini::simple_property_sptr soname_not_regex_prop =
-    is_simple_property(section.find_property("soname_not_regexp"));
   regex_t_sptr soname_not_regex;
-  if (soname_not_regex_prop)
-    soname_not_regex =
-      regex::compile(soname_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "soname_not_regexp", soname_not_regex);
 
   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"));
   regex_t_sptr name_regex;
-  if (name_regex_prop)
-    name_regex = regex::compile(name_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "name_regexp", name_regex);
 
-  ini::simple_property_sptr name_not_regex_prop =
-    is_simple_property(section.find_property("name_not_regexp"));
   regex_t_sptr name_not_regex;
-  if (name_not_regex_prop)
-    name_not_regex =
-      regex::compile(name_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "name_not_regexp", name_not_regex);
 
   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"));
   regex_t_sptr symbol_name_regex;
-  if (sym_name_regex_prop)
-    symbol_name_regex =
-      regex::compile(sym_name_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "symbol_name_regexp", symbol_name_regex);
 
-  ini::simple_property_sptr sym_name_not_regex_prop =
-    is_simple_property(section.find_property("symbol_name_not_regexp"));
   regex_t_sptr symbol_name_not_regex;
-  if (sym_name_not_regex_prop)
-    symbol_name_not_regex =
-      regex::compile(sym_name_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "symbol_name_not_regexp", symbol_name_not_regex);
 
   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"));
   regex_t_sptr symbol_version_regex;
-  if (sym_version_regex_prop)
-    symbol_version_regex =
-      regex::compile(sym_version_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "symbol_version_regexp", symbol_version_regex);
 
   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"));
   regex_t_sptr type_name_regex;
-  if (type_name_regex_prop)
-    type_name_regex =
-      regex::compile(type_name_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "type_name_regexp", type_name_regex);
 
   result.reset(new variable_suppression(label_str,
 					name_str,
@@ -4250,32 +4172,17 @@  read_file_suppression(const ini::config::section& section)
   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"));
   regex_t_sptr file_name_regex;
-  if (file_name_regex_prop)
-    file_name_regex =
-      regex::compile(file_name_regex_prop->get_value()->as_string());
-
- ini::simple_property_sptr file_name_not_regex_prop =
-    is_simple_property(section.find_property("file_name_not_regexp"));
- regex_t_sptr file_name_not_regex;
- if (file_name_not_regex_prop)
-   file_name_not_regex =
-     regex::compile(file_name_not_regex_prop->get_value()->as_string());
-
-  ini::simple_property_sptr soname_regex_prop =
-    is_simple_property(section.find_property("soname_regexp"));
+  maybe_get_regex_prop(section, "file_name_regexp", file_name_regex);
+
+  regex_t_sptr file_name_not_regex;
+  maybe_get_regex_prop(section, "file_name_not_regexp", file_name_not_regex);
+
   regex_t_sptr soname_regex;
-  if (soname_regex_prop)
-    soname_regex = regex::compile(soname_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "soname_regexp", soname_regex);
 
-  ini::simple_property_sptr soname_not_regex_prop =
-    is_simple_property(section.find_property("soname_not_regexp"));
   regex_t_sptr soname_not_regex;
-  if (soname_not_regex_prop)
-    soname_not_regex =
-      regex::compile(soname_not_regex_prop->get_value()->as_string());
+  maybe_get_regex_prop(section, "soname_not_regexp", soname_not_regex);
 
   result.reset(new file_suppression(label_str,
 				    file_name_regex,