summaryrefslogtreecommitdiff
path: root/Tools/c-analyzer/cpython/known.py
blob: c3cc2c06026ce1d4f1efac5d588608aa1a10e014 (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
import csv
import os.path

from c_analyzer.parser.declarations import extract_storage
from c_analyzer.variables import known as _common
from c_analyzer.variables.info import Variable

from . import DATA_DIR


# XXX need tests:
# * from_file()
# * look_up_variable()


DATA_FILE = os.path.join(DATA_DIR, 'known.tsv')


def _get_storage(decl, infunc):
    # statics
    if decl.startswith(('Py_LOCAL(', 'Py_LOCAL_INLINE(')):
        return 'static'
    if decl.startswith(('_Py_IDENTIFIER(', '_Py_static_string(')):
        return 'static'
    if decl.startswith('PyDoc_VAR('):
        return 'static'
    if decl.startswith(('SLOT1BINFULL(', 'SLOT1BIN(')):
        return 'static'
    if decl.startswith('WRAP_METHOD('):
        return 'static'
    # public extern
    if decl.startswith('PyAPI_DATA('):
        return 'extern'
    # Fall back to the normal handler.
    return extract_storage(decl, infunc=infunc)


def _handle_var(varid, decl):
#    if varid.name == 'id' and decl == UNKNOWN:
#        # None of these are variables.
#        decl = 'int id';
    storage = _get_storage(decl, varid.funcname)
    return Variable(varid, storage, decl)


def from_file(infile=DATA_FILE, *,
              _from_file=_common.from_file,
              _handle_var=_handle_var,
              ):
    """Return the info for known declarations in the given file."""
    return _from_file(infile, handle_var=_handle_var)


def look_up_variable(varid, knownvars, *,
                     _lookup=_common.look_up_variable,
                     ):
    """Return the known variable matching the given ID.

    "knownvars" is a mapping of ID to Variable.

    "match_files" is used to verify if two filenames point to
    the same file.

    If no match is found then None is returned.
    """
    return _lookup(varid, knownvars)