aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Tools/build/check_warnings.py
blob: f0c0067f4ab2553a4e89480bb3a76a5e48029a54 (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
"""
Parses compiler output with -fdiagnostics-format=json and checks that warnings
exist only in files that are expected to have warnings.
"""
import argparse
import json
import re
import sys
from pathlib import Path


def extract_warnings_from_compiler_output(compiler_output: str) -> list[dict]:
    """
    Extracts warnings from the compiler output when using
    -fdiagnostics-format=json

    Compiler output as a whole is not a valid json document, but includes many
    json objects and may include other output that is not json.
    """

    # Regex to find json arrays at the top level of the file
    # in the compiler output
    json_arrays = re.findall(
        r"\[(?:[^\[\]]|\[(?:[^\[\]]|\[[^\[\]]*\])*\])*\]", compiler_output
    )
    compiler_warnings = []
    for array in json_arrays:
        try:
            json_data = json.loads(array)
            json_objects_in_array = [entry for entry in json_data]
            compiler_warnings.extend(
                [
                    entry
                    for entry in json_objects_in_array
                    if entry.get("kind") == "warning"
                ]
            )
        except json.JSONDecodeError:
            continue  # Skip malformed JSON

    return compiler_warnings


def get_warnings_by_file(warnings: list[dict]) -> dict[str, list[dict]]:
    """
    Returns a dictionary where the key is the file and the data is the warnings
    in that file
    """
    warnings_by_file = {}
    for warning in warnings:
        locations = warning["locations"]
        for location in locations:
            for key in ["caret", "start", "end"]:
                if key in location:
                    file = location[key]["file"]
                    file = file.lstrip(
                        "./"
                    )  # Remove leading current directory if present
                    if file not in warnings_by_file:
                        warnings_by_file[file] = []
                    warnings_by_file[file].append(warning)

    return warnings_by_file


def get_unexpected_warnings(
    warnings: list[dict],
    files_with_expected_warnings: set[str],
    files_with_warnings: set[str],
) -> int:
    """
    Returns failure status if warnings discovered in list of warnings
    are associated with a file that is not found in the list of files
    with expected warnings
    """
    unexpected_warnings = []
    for file in files_with_warnings.keys():
        if file not in files_with_expected_warnings:
            unexpected_warnings.extend(files_with_warnings[file])

    if unexpected_warnings:
        print("Unexpected warnings:")
        for warning in unexpected_warnings:
            print(warning)
        return 1

    return 0


def get_unexpected_improvements(
    warnings: list[dict],
    files_with_expected_warnings: set[str],
    files_with_warnings: set[str],
) -> int:
    """
    Returns failure status if there are no warnings in the list of warnings for
    a file that is in the list of files with expected warnings
    """
    unexpected_improvements = []
    for file in files_with_expected_warnings:
        if file not in files_with_warnings.keys():
            unexpected_improvements.append(file)

    if unexpected_improvements:
        print("Unexpected improvements:")
        for file in unexpected_improvements:
            print(file)
        return 1

    return 0


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--compiler-output-file-path",
        type=str,
        required=True,
        help="Path to the compiler output file",
    )
    parser.add_argument(
        "--warning-ignore-file-path",
        type=str,
        required=True,
        help="Path to the warning ignore file",
    )
    parser.add_argument(
        "--fail-on-regression",
        action="store_true",
        default=False,
        help="Flag to fail if new warnings are found",
    )
    parser.add_argument(
        "--fail-on-improvement",
        action="store_true",
        default=False,
        help="Flag to fail if files that were expected "
        "to have warnings have no warnings",
    )

    args = parser.parse_args(argv)

    exit_code = 0

    # Check that the compiler output file is a valid path
    if not Path(args.compiler_output_file_path).is_file():
        print(
            "Compiler output file does not exist: "
            f"{args.compiler_output_file_path}"
        )
        return 1

    # Check that the warning ignore file is a valid path
    if not Path(args.warning_ignore_file_path).is_file():
        print(
            "Warning ignore file does not exist: "
            f"{args.warning_ignore_file_path}"
        )
        return 1

    with Path(args.compiler_output_file_path).open(encoding="UTF-8") as f:
        compiler_output_file_contents = f.read()

    with Path(args.warning_ignore_file_path).open(
        encoding="UTF-8"
    ) as clean_files:
        files_with_expected_warnings = {
            file.strip()
            for file in clean_files
            if file.strip() and not file.startswith("#")
        }

    warnings = extract_warnings_from_compiler_output(
        compiler_output_file_contents
    )
    files_with_warnings = get_warnings_by_file(warnings)

    status = get_unexpected_warnings(
        warnings, files_with_expected_warnings, files_with_warnings
    )
    if args.fail_on_regression:
        exit_code |= status

    status = get_unexpected_improvements(
        warnings, files_with_expected_warnings, files_with_warnings
    )
    if args.fail_on_improvement:
        exit_code |= status

    return exit_code


if __name__ == "__main__":
    sys.exit(main())