test_analysis.py 3.2 KB

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