1# -*- Python -*- vim: set syntax=python tabstop=4 expandtab cc=80:
2#===----------------------------------------------------------------------===##
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8#===----------------------------------------------------------------------===##
9"""
10match - A set of functions for matching symbols in a list to a list of regexs
11"""
12
13import re
14
15
16def find_and_report_matching(symbol_list, regex_list):
17    report = ''
18    found_count = 0
19    for regex_str in regex_list:
20        report += 'Matching regex "%s":\n' % regex_str
21        matching_list = find_matching_symbols(symbol_list, regex_str)
22        if not matching_list:
23            report += '    No matches found\n\n'
24            continue
25        # else
26        found_count += len(matching_list)
27        for m in matching_list:
28            report += '    MATCHES: %s\n' % m['name']
29        report += '\n'
30    return found_count, report
31
32
33def find_matching_symbols(symbol_list, regex_str):
34    regex = re.compile(regex_str)
35    matching_list = []
36    for s in symbol_list:
37        if regex.match(s['name']):
38            matching_list += [s]
39    return matching_list
40