• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from ..common import files
2from ..common.info import UNKNOWN
3from ..parser import (
4        find as p_find,
5        )
6from ..symbols import (
7        info as s_info,
8        find as s_find,
9        )
10from .info import Variable
11
12# XXX need tests:
13# * vars_from_source
14
15
16def _remove_cached(cache, var):
17    if not cache:
18        return
19    try:
20        cached = cache[var.filename]
21        cached.remove(var)
22    except (KeyError, IndexError):
23        pass
24
25
26def vars_from_binary(binfile, *,
27                     known=None,
28                     filenames=None,
29                     handle_id=None,
30                     check_filename=None,
31                     handle_var=Variable.from_id,
32                     _iter_vars=s_find.variables,
33                     _get_symbol_resolver=s_find.get_resolver,
34                     ):
35    """Yield a Variable for each found Symbol.
36
37    Details are filled in from the given "known" variables and types.
38    """
39    cache = {}
40    resolve = _get_symbol_resolver(filenames, known,
41                                   handle_var=handle_var,
42                                   check_filename=check_filename,
43                                   perfilecache=cache,
44                                   )
45    for var, symbol in _iter_vars(binfile,
46                                  resolve=resolve,
47                                  handle_id=handle_id,
48                                  ):
49        if var is None:
50            var = Variable(symbol.id, UNKNOWN, UNKNOWN)
51        yield var
52        _remove_cached(cache, var)
53
54
55def vars_from_source(filenames, *,
56                     preprocessed=None,
57                     known=None,
58                     handle_id=None,
59                     handle_var=Variable.from_id,
60                     iter_vars=p_find.variables,
61                     ):
62    """Yield a Variable for each declaration in the raw source code.
63
64    Details are filled in from the given "known" variables and types.
65    """
66    cache = {}
67    for varid, decl in iter_vars(filenames or (),
68                                 perfilecache=cache,
69                                 preprocessed=preprocessed,
70                                 known=known,
71                                 handle_id=handle_id,
72                                 ):
73        var = handle_var(varid, decl)
74        yield var
75        _remove_cached(cache, var)
76