Eric Snow | e4c431e | 2019-10-18 19:00:04 -0700 | [diff] [blame] | 1 | import csv |
| 2 | import os.path |
| 3 | |
| 4 | from c_analyzer.parser.declarations import extract_storage |
| 5 | from c_analyzer.variables import known as _common |
| 6 | from c_analyzer.variables.info import Variable |
| 7 | |
| 8 | from . import DATA_DIR |
| 9 | |
| 10 | |
| 11 | # XXX need tests: |
| 12 | # * from_file() |
| 13 | # * look_up_variable() |
| 14 | |
| 15 | |
| 16 | DATA_FILE = os.path.join(DATA_DIR, 'known.tsv') |
| 17 | |
| 18 | |
| 19 | def _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 | |
| 38 | def _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 | |
| 46 | def 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 | |
| 54 | def 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) |