|
| 1 | + |
| 2 | +/*******************************************************************\ |
| 3 | +
|
| 4 | +Module: Java Bytecode |
| 5 | +
|
| 6 | +Author: Diffblue Ltd. |
| 7 | +
|
| 8 | +\*******************************************************************/ |
| 9 | + |
| 10 | +#include "load_method_by_regex.h" |
| 11 | + |
| 12 | +#include <regex> |
| 13 | + |
| 14 | +#include <util/symbol_table.h> |
| 15 | + |
| 16 | +/// For a given user provided pattern, return a regex, having dealt with the |
| 17 | +/// cases where the user has not prefixed with java:: or suffixed with the |
| 18 | +/// descriptor |
| 19 | +/// \param pattern: The user provided pattern |
| 20 | +/// \return The regex to match with |
| 21 | +static std::regex build_regex_from_pattern(const std::string &pattern) |
| 22 | +{ |
| 23 | + std::string modified_pattern = pattern; |
| 24 | + if(does_pattern_miss_descriptor(pattern)) |
| 25 | + modified_pattern += R"(:\(.*\).*)"; |
| 26 | + |
| 27 | + if(!has_prefix(pattern, "java::")) |
| 28 | + modified_pattern = "java::" + modified_pattern; |
| 29 | + |
| 30 | + return std::regex{modified_pattern}; |
| 31 | +} |
| 32 | + |
| 33 | +/// Identify if a parameter includes a part that will match a descriptor. That |
| 34 | +/// is, does it have a colon separtor. |
| 35 | +/// \param pattern: The user provided pattern |
| 36 | +/// \return True if no descriptor is found (that is, the only : relates to the |
| 37 | +/// java:: prefix. |
| 38 | +bool does_pattern_miss_descriptor(const std::string &pattern) |
| 39 | +{ |
| 40 | + const size_t descriptor_index = pattern.rfind(':'); |
| 41 | + if(descriptor_index == std::string::npos) |
| 42 | + return true; |
| 43 | + |
| 44 | + static const std::string java_prefix = "java::"; |
| 45 | + return descriptor_index == java_prefix.length() - 1 && |
| 46 | + has_prefix(pattern, java_prefix); |
| 47 | +} |
| 48 | + |
| 49 | +/// Create a lambda that returns the symbols that the given pattern should be |
| 50 | +/// loaded.If the pattern doesn't include a colon for matching the descriptor, |
| 51 | +/// append a `:\(.*\).*` to the regex. Note this will mean all overloaded |
| 52 | +/// methods will be marked as extra entry points for CI lazy loading. |
| 53 | +/// If the pattern doesn't include the java:: prefix, prefix that |
| 54 | +/// \param pattern: The user provided pattern |
| 55 | +/// \return The lambda to execute. |
| 56 | +std::function<std::vector<irep_idt>(const symbol_tablet &symbol_table)> |
| 57 | +build_load_method_by_regex(const std::string &pattern) |
| 58 | +{ |
| 59 | + std::regex regex = build_regex_from_pattern(pattern); |
| 60 | + |
| 61 | + return [=](const symbol_tablet &symbol_table) { |
| 62 | + std::vector<irep_idt> matched_methods; |
| 63 | + for(const auto &symbol : symbol_table.symbols) |
| 64 | + { |
| 65 | + if( |
| 66 | + symbol.second.is_function() && |
| 67 | + std::regex_match(id2string(symbol.first), regex)) |
| 68 | + { |
| 69 | + matched_methods.push_back(symbol.first); |
| 70 | + } |
| 71 | + } |
| 72 | + return matched_methods; |
| 73 | + }; |
| 74 | +} |
0 commit comments