import re


def _filter_files(sources, headers, exclude_patterns):
    """
    Filter out files that should not be checked.

    Returns a (sources, headers) tuple.
    """

    # Filter out explicitly excluded files
    if len(exclude_patterns) > 0:
        exclude_re = re.compile("|".join(exclude_patterns))
        sources = [s for s in sources if not exclude_re.search(s)]
        headers = [h for h in headers if not exclude_re.search(h)]

    # Filter out sources in the build directory
    # This avoids checking generated code, configuration checks, and so on
    sources = [s for s in sources if s.startswith("..")]

    return (sources, headers)