Skip to content

Commit

Permalink
Add flags to check warnings script to fail on regression or improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
nohlson committed Jul 13, 2024
1 parent 7a39d57 commit 4530506
Showing 1 changed file with 33 additions and 48 deletions.
81 changes: 33 additions & 48 deletions Tools/build/check_warnings.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#!/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 sys
from pathlib import Path
import argparse
import json


def extract_json_objects(compiler_output: str) -> list[dict]:

return json_objects


def extract_warnings_from_compiler_output(compiler_output: str) -> list[dict]:
"""
Extracts warnings from the compiler output when using -fdiagnostics-format=json
Expand Down Expand Up @@ -41,14 +39,13 @@ def extract_warnings_from_compiler_output(compiler_output: str) -> list[dict]:

return compiler_warnings



def get_unexpected_warnings(
warnings: list[dict],
files_with_expected_warnings: set[str],
) -> int:
"""
Fails if there are unexpected warnings
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 warning in warnings:
Expand All @@ -66,7 +63,6 @@ def get_unexpected_warnings(
print(warning)
return 1


return 0


Expand All @@ -75,24 +71,23 @@ def get_unexpected_improvements(
files_with_expected_warnings: set[str],
) -> int:
"""
Fails if files that were expected to have warnings have no warnings
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
"""

# Create set of files with warnings
files_with_ewarnings = set()
files_with_warnings = set()
for warning in warnings:
locations = warning['locations']
for location in locations:
for key in ['caret', 'start', 'end']:
if key in location:
filename = location[key]['file']
files_with_ewarnings.add(filename)


files_with_warnings.add(filename)

unexpected_improvements = []
for filename in files_with_expected_warnings:
if filename not in files_with_ewarnings:
if filename not in files_with_warnings:
unexpected_improvements.append(filename)

if unexpected_improvements:
Expand All @@ -101,29 +96,34 @@ def get_unexpected_improvements(
print(filename)
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 file"
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)

Expand All @@ -133,12 +133,12 @@ def main(argv: list[str] | None = None) -> int:
if not Path(args.compiler_output_file_path).is_file():
print(f"Compiler output file does not exist: {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(f"Warning ignore file does not exist: {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()

Expand All @@ -149,33 +149,18 @@ def main(argv: list[str] | None = None) -> int:
if filename.strip() and not filename.startswith("#")
}


if len(compiler_output_file_contents) > 0:
print("Successfully got compiler output")
else:
exit_code = 1

if len(files_with_expected_warnings) > 0:
print("we have some exceptions")

if not len(compiler_output_file_contents) > 0:exit_code = 1

warnings = extract_warnings_from_compiler_output(compiler_output_file_contents)

exit_code = get_unexpected_warnings(warnings, files_with_expected_warnings)

exit_code = get_unexpected_improvements(warnings, files_with_expected_warnings)


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

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

print("Returning exit code: ", exit_code)
return exit_code








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

0 comments on commit 4530506

Please sign in to comment.