aboutsummaryrefslogtreecommitdiff
path: root/Tools/c-analyzer/c_parser/__init__.py
blob: fc10aff94505dade8c1a850d09912e2a4d219dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from c_common.fsutil import match_glob as _match_glob
from .parser import parse as _parse
from .preprocessor import get_preprocessor as _get_preprocessor


def parse_file(filename, *,
               match_kind=None,
               get_file_preprocessor=None,
               file_maxsizes=None,
               ):
    if get_file_preprocessor is None:
        get_file_preprocessor = _get_preprocessor()
    yield from _parse_file(
            filename, match_kind, get_file_preprocessor, file_maxsizes)


def parse_files(filenames, *,
                match_kind=None,
                get_file_preprocessor=None,
                file_maxsizes=None,
                ):
    if get_file_preprocessor is None:
        get_file_preprocessor = _get_preprocessor()
    for filename in filenames:
        yield from _parse_file(
                filename, match_kind, get_file_preprocessor, file_maxsizes)


def _parse_file(filename, match_kind, get_file_preprocessor, maxsizes):
    srckwargs = {}
    maxsize = _resolve_max_size(filename, maxsizes)
    if maxsize:
        srckwargs['maxtext'], srckwargs['maxlines'] = maxsize

    # Preprocess the file.
    preprocess = get_file_preprocessor(filename)
    preprocessed = preprocess()
    if preprocessed is None:
        return

    # Parse the lines.
    srclines = ((l.file, l.data) for l in preprocessed if l.kind == 'source')
    for item in _parse(srclines, **srckwargs):
        if match_kind is not None and not match_kind(item.kind):
            continue
        if not item.filename:
            raise NotImplementedError(repr(item))
        yield item


def _resolve_max_size(filename, maxsizes):
    for pattern, maxsize in (maxsizes.items() if maxsizes else ()):
        if _match_glob(filename, pattern):
            break
    else:
        return None
    if not maxsize:
        return None, None
    maxtext, maxlines = maxsize
    if maxtext is not None:
        maxtext = int(maxtext)
    if maxlines is not None:
        maxlines = int(maxlines)
    return maxtext, maxlines


def parse_signature(text):
    raise NotImplementedError


# aliases
from .info import resolve_parsed