Skip to content

Commit e1a2cec

Browse files
committed
Improve logging in generator
1 parent cad75c0 commit e1a2cec

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/generators/generator.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
from src.generators import utils as gu
3030
from src.generators.config import cfg
3131
from src.ir import ast, types as tp, type_utils as tu, kotlin_types as kt
32-
from src.ir.context import Context, get_decl
32+
from src.ir.context import Context
3333
from src.ir.builtins import BuiltinFactory
3434
from src.ir import BUILTIN_FACTORIES
35-
from src.modules.logging import Logger
35+
from src.modules.logging import Logger, log
3636

3737

3838
class Generator():
@@ -857,7 +857,10 @@ def generate_expr(self,
857857
if find_subtype:
858858
subtypes = tu.find_subtypes(expr_type, self.get_types(),
859859
include_self=True, concrete_only=True)
860+
old_type = expr_type
860861
expr_type = ut.random.choice(subtypes)
862+
msg = "Found subtype of {}: {}".format(old_type, expr_type)
863+
log(self.logger, msg)
861864
generators = self.get_generators(expr_type, only_leaves, subtype,
862865
exclude_var, sam_coercion=sam_coercion)
863866
expr = ut.random.choice(generators)(expr_type)
@@ -1022,7 +1025,8 @@ def gen_field_access(self,
10221025
self.depth += 1
10231026
objs = self._get_matching_objects(etype, subtype, 'fields')
10241027
if not objs:
1025-
type_f = self._get_matching_class(etype, subtype, 'fields')
1028+
type_f = self._get_matching_class(etype, subtype=subtype,
1029+
attr_name='fields')
10261030
if type_f is None:
10271031
type_f = self._gen_matching_class(
10281032
etype, 'fields', not_void=True,
@@ -1477,10 +1481,16 @@ def _gen_func_call(self,
14771481
only_leaves: do not generate new leaves except from `expr`.
14781482
subtype: The returned type could be a subtype of `etype`.
14791483
"""
1484+
log(self.logger, "Generating function call of type {}".format(etype))
14801485
funcs = self._get_matching_function_declarations(etype, subtype)
14811486
if not funcs:
1482-
type_fun = self._get_matching_class(etype, subtype, 'functions')
1487+
msg = "No compatible functions in the current scope for type {}"
1488+
log(self.logger, msg.format(etype))
1489+
type_fun = self._get_matching_class(etype, subtype=subtype,
1490+
attr_name='functions')
14831491
if type_fun is None:
1492+
msg = "No compatible classes for type {}"
1493+
log(self.logger, msg.format(etype))
14841494
# Here, we generate a function or a class containing a function
14851495
# whose return type is 'etype'.
14861496
type_fun = self._gen_matching_func(etype, not_void=True)
@@ -1489,7 +1499,7 @@ def _gen_func_call(self,
14891499
else self.generate_expr(type_fun.receiver_t, only_leaves)
14901500
)
14911501
funcs.append(gu.AttrReceiverInfo(receiver, type_fun.receiver_inst,
1492-
type_fun.attr_decl, type_fun.attr_inst))
1502+
type_fun.attr_decl, type_fun.attr_inst))
14931503

14941504
rand_func = ut.random.choice(funcs)
14951505
receiver = rand_func.receiver_expr
@@ -1498,6 +1508,10 @@ def _gen_func_call(self,
14981508
func_type_map = rand_func.attr_inst
14991509

15001510
params_map.update(func_type_map or {})
1511+
1512+
msg = ("Selected callee method {}: type {}; receiver {}; "
1513+
"TypeVarMap {}".format(func.name, etype, receiver, params_map))
1514+
log(self.logger, msg)
15011515
args = []
15021516
initial_depth = self.depth
15031517
self.depth += 1
@@ -1796,7 +1810,7 @@ def _gen_func_ref(self, etype: tp.Type,
17961810
# Get function references from methods of classes.
17971811
# ie create receiver
17981812
type_fun = self._get_matching_class(
1799-
etype, False, 'functions', signature=True)
1813+
etype, subtype=False, attr_name='functions', signature=True)
18001814

18011815
# Generate a matching function.
18021816
if not type_fun:
@@ -2007,6 +2021,8 @@ def select_type(self,
20072021
disable_variance_functions=self.disable_variance_functions,
20082022
variance_choices={}
20092023
)
2024+
msg = "Instantiating type constructor {}".format(stype)
2025+
log(self.logger, msg)
20102026
return stype
20112027

20122028
def gen_type_params(self,
@@ -2439,6 +2455,9 @@ def _get_matching_function_declarations(self,
24392455
)
24402456
# First find all top-level functions or methods included
24412457
# in the current class.
2458+
msg = ("Searching for function declarations that match type {};"
2459+
" checking signature {}")
2460+
log(self.logger, msg.format(etype, signature))
24422461
for func in self.context.get_funcs(self.namespace).values():
24432462
# The receiver object for this kind of functions is None.
24442463
if func.get_type() == self.bt_factory.get_void_type():
@@ -2524,14 +2543,16 @@ def _gen_matching_func(self,
25242543
func_type_var_map = tu.instantiate_parameterized_function(
25252544
func.type_parameters, self.get_types(),
25262545
only_regular=True, type_var_map={})
2546+
msg = "Generating a method {} of type {}; TypeVarMap {}".format(
2547+
func.name, etype, func_type_var_map)
2548+
log(self.logger, msg)
25272549
return gu.AttrAccessInfo(None, {}, func, func_type_var_map)
25282550
# Generate a class containing the requested function
25292551
return self._gen_matching_class(etype, 'functions',
25302552
signature=signature)
25312553

2532-
25332554
def _get_matching_class(self,
2534-
etype:tp.Type,
2555+
etype: tp.Type,
25352556
subtype: bool,
25362557
attr_name: str,
25372558
signature=False) -> gu.AttrAccessInfo:
@@ -2552,8 +2573,10 @@ def _get_matching_class(self,
25522573
An AttrAccessInfo with a matched class type and attribute
25532574
declaration (field or function).
25542575
"""
2576+
msg = "Searching for class that contains {} of type {}"
2577+
log(self.logger, msg.format(attr_name, etype))
25552578
class_decls = self._get_matching_class_decls(
2556-
etype, subtype, attr_name, signature)
2579+
etype, subtype=subtype, attr_name=attr_name, signature=signature)
25572580
if not class_decls:
25582581
return None
25592582
cls, type_var_map, attr = ut.random.choice(class_decls)
@@ -2575,6 +2598,9 @@ def _get_matching_class(self,
25752598
disable_variance_functions=self.disable_variance_functions,
25762599
variance_choices=variance_choices
25772600
)
2601+
msg = ("Found parameterized class {} with TypeVarMap {} and "
2602+
"incomplete TypeVarMap {}")
2603+
log(self.logger, msg.format(cls.name, params_map, type_var_map))
25782604
if is_parameterized_func:
25792605
# Here we have found a parameterized function in a
25802606
# parameterized class. So wee need to both instantiate
@@ -2611,6 +2637,12 @@ def _get_matching_class(self,
26112637
attr.type_parameters, self.get_types(),
26122638
only_regular=True, type_var_map=type_var_map)
26132639
cls_type, params_map = cls.get_type(), {}
2640+
2641+
attr_msg = "Attribute {}; type: {}, TypeVarMap{}".format(
2642+
attr_name, etype, func_type_var_map)
2643+
msg = "Selected class {} with TypeVarMap {};" " matches {}".format(
2644+
cls.name, params_map, attr_msg)
2645+
log(self.logger, msg)
26142646
return gu.AttrAccessInfo(cls_type, params_map, attr, func_type_var_map)
26152647

26162648
def _is_sigtype_compatible(self, attr, etype, type_var_map,
@@ -2807,6 +2839,8 @@ def _gen_matching_class(self,
28072839
attr.type_parameters, self.get_types(), only_regular=True,
28082840
type_var_map=params_map)
28092841

2842+
msg = "Generated a class {} with an attribute {} of type {}"
2843+
log(self.logger, msg.format(cls.name, attr_name, etype))
28102844
return gu.AttrAccessInfo(cls_type, params_map, attr,
28112845
func_type_var_map)
28122846
return None

src/modules/logging.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ def log(self, msg):
3232
with open(self.filename, 'a') as out:
3333
out.write(str(msg))
3434
out.write('\n')
35+
36+
37+
def log(logger: Logger, msg: str):
38+
if logger is not None:
39+
logger.log(msg)

0 commit comments

Comments
 (0)