-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
test_analysis.py
77 lines (67 loc) · 3.29 KB
/
test_analysis.py
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
67
68
69
70
71
72
73
74
75
76
77
"""Test runner for data-flow analysis test cases."""
import os.path
from typing import Set
from mypy.test.data import DataDrivenTestCase
from mypy.test.config import test_temp_dir
from mypy.errors import CompileError
from mypyc.common import TOP_LEVEL_NAME
from mypyc.analysis import dataflow
from mypyc.transform import exceptions
from mypyc.ir.pprint import format_func, generate_names_for_ir
from mypyc.ir.ops import Value
from mypyc.ir.func_ir import all_values
from mypyc.test.testutil import (
ICODE_GEN_BUILTINS, use_custom_builtins, MypycDataSuite, build_ir_for_single_file,
assert_test_output
)
files = [
'analysis.test'
]
class TestAnalysis(MypycDataSuite):
files = files
base_path = test_temp_dir
optional_out = True
def run_case(self, testcase: DataDrivenTestCase) -> None:
"""Perform a data-flow analysis test case."""
with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
try:
ir = build_ir_for_single_file(testcase.input)
except CompileError as e:
actual = e.messages
else:
actual = []
for fn in ir:
if (fn.name == TOP_LEVEL_NAME
and not testcase.name.endswith('_toplevel')):
continue
exceptions.insert_exception_handling(fn)
actual.extend(format_func(fn))
cfg = dataflow.get_cfg(fn.blocks)
args = set(fn.arg_regs) # type: Set[Value]
name = testcase.name
if name.endswith('_MaybeDefined'):
# Forward, maybe
analysis_result = dataflow.analyze_maybe_defined_regs(fn.blocks, cfg, args)
elif name.endswith('_Liveness'):
# Backward, maybe
analysis_result = dataflow.analyze_live_regs(fn.blocks, cfg)
elif name.endswith('_MustDefined'):
# Forward, must
analysis_result = dataflow.analyze_must_defined_regs(
fn.blocks, cfg, args,
regs=all_values(fn.arg_regs, fn.blocks))
elif name.endswith('_BorrowedArgument'):
# Forward, must
analysis_result = dataflow.analyze_borrowed_arguments(fn.blocks, cfg, args)
else:
assert False, 'No recognized _AnalysisName suffix in test case'
names = generate_names_for_ir(fn.arg_regs, fn.blocks)
for key in sorted(analysis_result.before.keys(),
key=lambda x: (x[0].label, x[1])):
pre = ', '.join(sorted(names[reg]
for reg in analysis_result.before[key]))
post = ', '.join(sorted(names[reg]
for reg in analysis_result.after[key]))
actual.append('%-8s %-23s %s' % ((key[0].label, key[1]),
'{%s}' % pre, '{%s}' % post))
assert_test_output(testcase, actual, 'Invalid source code output')