@@ -17,6 +17,8 @@ Date: April 2017
1717
1818#include < goto-programs/goto_model.h>
1919
20+ #include < regex>
21+
2022// / Remove the body of function "identifier" such that an analysis will treat it
2123// / as a side-effect free function with non-deterministic return value.
2224// / \par parameters: symbol_table Input symbol table to be modified
@@ -73,3 +75,78 @@ void remove_functions(
7375 for (const auto &f : names)
7476 remove_function (goto_model, f, message_handler);
7577}
78+
79+ // / Remove functions matching a regular expression pattern
80+ // / \param goto_model: The goto model to modify
81+ // / \param pattern: The regex pattern to match function names against
82+ // / \param pattern_as_str: The string representation of \p pattern
83+ // / \param message_handler: For status/warning/error messages
84+ static void remove_functions_regex (
85+ goto_modelt &goto_model,
86+ const std::regex &pattern,
87+ const std::string &pattern_as_str,
88+ message_handlert &message_handler)
89+ {
90+ messaget message{message_handler};
91+
92+ message.debug () << " Removing functions matching pattern: " << pattern_as_str
93+ << messaget::eom;
94+
95+ // Collect matching function names first to avoid modifying the map while
96+ // iterating
97+ std::list<irep_idt> matching_functions;
98+
99+ for (const auto &entry : goto_model.goto_functions .function_map )
100+ {
101+ const std::string &function_name = id2string (entry.first );
102+ if (std::regex_match (function_name, pattern))
103+ {
104+ matching_functions.push_back (entry.first );
105+ }
106+ }
107+
108+ // Now remove all matching functions
109+ for (const auto &func : matching_functions)
110+ {
111+ remove_function (goto_model, func, message_handler);
112+ }
113+
114+ message.debug () << " Removed " << matching_functions.size ()
115+ << " function(s) matching pattern: " << pattern_as_str
116+ << messaget::eom;
117+ }
118+
119+ void remove_functions_regex (
120+ goto_modelt &goto_model,
121+ const std::list<std::string> &patterns,
122+ message_handlert &message_handler)
123+ {
124+ std::string combined_pattern;
125+ for (const auto &pattern : patterns)
126+ {
127+ if (pattern.empty ())
128+ continue ;
129+ if (!combined_pattern.empty ())
130+ combined_pattern += ' |' ;
131+ combined_pattern += pattern;
132+ }
133+
134+ if (combined_pattern.empty ())
135+ return ;
136+
137+ messaget message{message_handler};
138+
139+ try
140+ {
141+ std::regex regex_pattern{combined_pattern};
142+
143+ remove_functions_regex (
144+ goto_model, regex_pattern, combined_pattern, message_handler);
145+ }
146+ catch (const std::regex_error &e)
147+ {
148+ message.error () << " Invalid regular expression pattern: "
149+ << combined_pattern << " (" << e.what () << " )"
150+ << messaget::eom;
151+ }
152+ }
0 commit comments