xref: /openbsd-src/gnu/llvm/clang/utils/analyzer/SATestUpdateDiffs.py (revision d0fc3bb68efd6c434b4053cd7adb29023cbec341)
1#!/usr/bin/env python
2
3"""
4Update reference results for static analyzer.
5"""
6import SATestBuild
7from ProjectMap import ProjectInfo, ProjectMap
8
9import os
10import shutil
11import sys
12
13from subprocess import check_call
14
15Verbose = 0
16
17
18def update_reference_results(project: ProjectInfo):
19    test_info = SATestBuild.TestInfo(project)
20    tester = SATestBuild.ProjectTester(test_info)
21    project_dir = tester.get_project_dir()
22
23    tester.is_reference_build = True
24    ref_results_path = tester.get_output_dir()
25
26    tester.is_reference_build = False
27    created_results_path = tester.get_output_dir()
28
29    if not os.path.exists(created_results_path):
30        print("New results not found, was SATestBuild.py previously run?",
31              file=sys.stderr)
32        sys.exit(1)
33
34    build_log_path = SATestBuild.get_build_log_path(ref_results_path)
35    build_log_dir = os.path.dirname(os.path.abspath(build_log_path))
36
37    os.makedirs(build_log_dir)
38
39    with open(build_log_path, "w+") as build_log_file:
40        def run_cmd(command: str):
41            if Verbose:
42                print(f"Executing {command}")
43            check_call(command, shell=True, stdout=build_log_file)
44
45        # Remove reference results: in git, and then again for a good measure
46        # with rm, as git might not remove things fully if there are empty
47        # directories involved.
48        run_cmd(f"git rm -r -q '{ref_results_path}'")
49        shutil.rmtree(ref_results_path)
50
51        # Replace reference results with a freshly computed once.
52        shutil.copytree(created_results_path, ref_results_path, symlinks=True)
53
54        # Run cleanup script.
55        SATestBuild.run_cleanup_script(project_dir, build_log_file)
56
57        SATestBuild.normalize_reference_results(
58            project_dir, ref_results_path, project.mode)
59
60        # Clean up the generated difference results.
61        SATestBuild.cleanup_reference_results(ref_results_path)
62
63        run_cmd(f"git add '{ref_results_path}'")
64
65
66# TODO: use argparse
67def main(argv):
68    if len(argv) == 2 and argv[1] in ("-h", "--help"):
69        print("Update static analyzer reference results based "
70              "\non the previous run of SATestBuild.py.\n"
71              "\nN.B.: Assumes that SATestBuild.py was just run",
72              file=sys.stderr)
73        sys.exit(1)
74
75    project_map = ProjectMap()
76    for project in project_map.projects:
77        update_reference_results(project)
78
79
80if __name__ == '__main__':
81    main(sys.argv)
82