blob: c3cc2c06026ce1d4f1efac5d588608aa1a10e014 [file] [log] [blame]
Eric Snowe4c431e2019-10-18 19:00:04 -07001import csv
2import os.path
3
4from c_analyzer.parser.declarations import extract_storage
5from c_analyzer.variables import known as _common
6from c_analyzer.variables.info import Variable
7
8from . import DATA_DIR
9
10
11# XXX need tests:
12# * from_file()
13# * look_up_variable()
14
15
16DATA_FILE = os.path.join(DATA_DIR, 'known.tsv')
17
18
19def _get_storage(decl, infunc):
20 # statics
21 if decl.startswith(('Py_LOCAL(', 'Py_LOCAL_INLINE(')):
22 return 'static'
23 if decl.startswith(('_Py_IDENTIFIER(', '_Py_static_string(')):
24 return 'static'
25 if decl.startswith('PyDoc_VAR('):
26 return 'static'
27 if decl.startswith(('SLOT1BINFULL(', 'SLOT1BIN(')):
28 return 'static'
29 if decl.startswith('WRAP_METHOD('):
30 return 'static'
31 # public extern
32 if decl.startswith('PyAPI_DATA('):
33 return 'extern'
34 # Fall back to the normal handler.
35 return extract_storage(decl, infunc=infunc)
36
37
38def _handle_var(varid, decl):
39# if varid.name == 'id' and decl == UNKNOWN:
40# # None of these are variables.
41# decl = 'int id';
42 storage = _get_storage(decl, varid.funcname)
43 return Variable(varid, storage, decl)
44
45
46def from_file(infile=DATA_FILE, *,
47 _from_file=_common.from_file,
48 _handle_var=_handle_var,
49 ):
50 """Return the info for known declarations in the given file."""
51 return _from_file(infile, handle_var=_handle_var)
52
53
54def look_up_variable(varid, knownvars, *,
55 _lookup=_common.look_up_variable,
56 ):
57 """Return the known variable matching the given ID.
58
59 "knownvars" is a mapping of ID to Variable.
60
61 "match_files" is used to verify if two filenames point to
62 the same file.
63
64 If no match is found then None is returned.
65 """
66 return _lookup(varid, knownvars)