| 1 |
"""Test runner for data-flow analysis test cases.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os.path |
| 6 |
|
| 7 |
from mypy.errors import CompileError |
| 8 |
from mypy.test.config import test_temp_dir |
| 9 |
from mypy.test.data import DataDrivenTestCase |
| 10 |
from mypyc.analysis import dataflow |
| 11 |
from mypyc.common import TOP_LEVEL_NAME |
| 12 |
from mypyc.ir.func_ir import all_values |
| 13 |
from mypyc.ir.ops import Value |
| 14 |
from mypyc.ir.pprint import format_func, generate_names_for_ir |
| 15 |
from mypyc.test.testutil import ( |
| 16 |
ICODE_GEN_BUILTINS, |
| 17 |
MypycDataSuite, |
| 18 |
assert_test_output, |
| 19 |
build_ir_for_single_file, |
| 20 |
use_custom_builtins, |
| 21 |
) |
| 22 |
from mypyc.transform import exceptions |
| 23 |
|
| 24 |
files = ["analysis.test"] |
| 25 |
|
| 26 |
|
| 27 |
class TestAnalysis(MypycDataSuite): |
| 28 |
files = files |
| 29 |
base_path = test_temp_dir |
| 30 |
optional_out = True |
| 31 |
|
| 32 |
def run_case(self, testcase: DataDrivenTestCase) -> None: |
| 33 |
"""Perform a data-flow analysis test case.""" |
| 34 |
|
| 35 |
with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase): |
| 36 |
try: |
| 37 |
ir = build_ir_for_single_file(testcase.input) |
| 38 |
except CompileError as e: |
| 39 |
actual = e.messages |
| 40 |
else: |
| 41 |
actual = [] |
| 42 |
for fn in ir: |
| 43 |
if fn.name == TOP_LEVEL_NAME and not testcase.name.endswith("_toplevel"): |
| 44 |
continue |
| 45 |
exceptions.insert_exception_handling(fn, True) |
| 46 |
actual.extend(format_func(fn)) |
| 47 |
cfg = dataflow.get_cfg(fn.blocks) |
| 48 |
args: set[Value] = set(fn.arg_regs) |
| 49 |
name = testcase.name |
| 50 |
if name.endswith("_MaybeDefined"): |
| 51 |
# Forward, maybe |
| 52 |
analysis_result = dataflow.analyze_maybe_defined_regs(fn.blocks, cfg, args) |
| 53 |
elif name.endswith("_Liveness"): |
| 54 |
# Backward, maybe |
| 55 |
analysis_result = dataflow.analyze_live_regs(fn.blocks, cfg) |
| 56 |
elif name.endswith("_MustDefined"): |
| 57 |
# Forward, must |
| 58 |
analysis_result = dataflow.analyze_must_defined_regs( |
| 59 |
fn.blocks, cfg, args, regs=all_values(fn.arg_regs, fn.blocks) |
| 60 |
) |
| 61 |
elif name.endswith("_BorrowedArgument"): |
| 62 |
# Forward, must |
| 63 |
analysis_result = dataflow.analyze_borrowed_arguments(fn.blocks, cfg, args) |
| 64 |
else: |
| 65 |
assert False, "No recognized _AnalysisName suffix in test case" |
| 66 |
|
| 67 |
names = generate_names_for_ir(fn.arg_regs, fn.blocks) |
| 68 |
|
| 69 |
for key in sorted( |
| 70 |
analysis_result.before.keys(), key=lambda x: (x[0].label, x[1]) |
| 71 |
): |
| 72 |
pre = ", ".join(sorted(names[reg] for reg in analysis_result.before[key])) |
| 73 |
post = ", ".join(sorted(names[reg] for reg in analysis_result.after[key])) |
| 74 |
actual.append( |
| 75 |
"%-8s %-23s %s" % ((key[0].label, key[1]), "{%s}" % pre, "{%s}" % post) |
| 76 |
) |
| 77 |
assert_test_output(testcase, actual, "Invalid source code output") |