8000 gh-112301: Add macOS warning tracking tooling by nohlson · Pull Request #122211 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112301: Add macOS warning tracking tooling #122211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5148727
Create simple warning check tool and add to ubuntu build and test job
nohlson Jul 13, 2024
3bd5a10
Add flags to check warnings script to fail on regression or improvement
nohlson Jul 13, 2024
6813003
Remove redundant comment
nohlson Jul 13, 2024
ab4d754
Rename warnigore file to warningignore
nohlson Jul 13, 2024
615d228
Use regex to extract json arrays
nohlson Jul 13, 2024
fc0a60b
Trim whitespace
nohlson Jul 13, 2024
7793d80
Test on github unexpected improvement
nohlson Jul 13, 2024
99715d2
Add config for improve fail check
nohlson Jul 13, 2024
bc44ec2
Revert to prod check warning state
nohlson Jul 13, 2024
e2ca75f
📜🤖 Added by blurb_it.
blurb-it[bot] Jul 13, 2024
bd1634e
Refactor creating set of files with warnings to a dedicated function
nohlson Jul 17, 2024
b07b1d6
Move cflags configure option to top level build configuration
nohlson Jul 23, 2024
e1954a5
Add json diagnostics to ubuntu configuration as first argument
nohlson Jul 23, 2024
5935143
Add newline to news
nohlson Jul 23, 2024
7f1a238
Create simple warning check tool and add to ubuntu build and test job
nohlson Jul 13, 2024
144136e
Add macos warning checks to GitHub actions
nohlson Jul 22, 2024
3dd40fd
Revert reusable-macos.yml for environment variables
nohlson Jul 22, 2024 8000
1158f53
Update paths
nohlson Jul 22, 2024
cb51b4f
Test unexpected improvement
nohlson Jul 22, 2024
77e0f6e
Remove warning ignore
nohlson Jul 22, 2024
3e1d75f
Add json output option to macos configure job
nohlson Jul 22, 2024
b5cd58a
Add common dictionary format when parsing warnings
nohlson Jul 23, 2024
02f313e
Remove configure option for macos job
nohlson Jul 23, 2024
83d1ed7
Print out json version of compiler output
nohlson Jul 23, 2024
08a6f6d
Remove old version of warning ignore file
nohlson Jul 23, 2024
7be8ee6
Remove compiler output print diagnostic
nohlson Jul 24, 2024
8b0a2ee
📜🤖 Added by blurb_it.
blurb-it[bot] Jul 24, 2024
49cbd87
Remove superfluous comment
nohlson Jul 24, 2024
522f27c
oMerge branch 'main' into add-macos-warnings-tracking-tooling
nohlson Jul 30, 2024
b654a84
Add period to news
nohlson Jul 30, 2024
7780da6
Merge branch 'main' into add-macos-warnings-tracking-tooling
nohlson Jul 31, 2024
3688c5c
Make warning ignore file optional
nohlson Jul 31, 2024
cb1f276
Add write compiler output to log and file
nohlson Jul 31, 2024
fb91e3e
Fix formatting and update regex
nohlson Jul 31, 2024
f35ba60
Fix comment formatting
nohlson Aug 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
oMerge branch 'main' into add-macos-warnings-tracking-tooling
  • Loading branch information
nohlson committed Jul 30, 2024
commit 522f27c36b1d09a2c62f0db2f8b1f6286c24febe
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Add tooling to check for changes in compiler warnings.
Patch by Nate Ohlson
Patch by Nate Ohlson.
119 changes: 82 additions & 37 deletions Tools/build/check_warnings.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,100 @@
#!/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.
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_clang(compiler_output: str) -> list[dict]:

def extract_warnings_from_compiler_output_clang(
compiler_output: str,
) -> list[dict]:
"""
Extracts warnings from the compiler output when using clang
"""
# Regex to find warnings in the compiler output
clang_warning_regex = re.compile(r'(?P<file>.*):(?P<line>\d+):(?P<column>\d+): warning: (?P<message>.*)')
clang_warning_regex = re.compile(
r"(?P<file>.*):(?P<line>\d+):(?P<column>\d+): warning: (?P<message>.*)"
)
compiler_warnings = []
for line in compiler_output.splitlines():
match = clang_warning_regex.match(line)
if match:
compiler_warnings.append({
'file': match.group('file'),
'line': match.group('line'),
'column': match.group('column'),
'message': match.group('message'),
})
compiler_warnings.append(
{
"file": match.group("file"),
"line": match.group("line"),
"column": match.group("column"),
"message": match.group("message"),
}
)

return compiler_warnings

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

def extract_warnings_from_compiler_output_json(
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)
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]
warning_list = [entry for entry in json_objects_in_array if entry.get('kind') == 'warning']
warning_list = [
entry
for entry in json_objects_in_array
if entry.get("kind") == "warning"
]
for warning in warning_list:
locations = warning['locations']
locations = warning["locations"]
for location in locations:
for key in ['caret', 'start', 'end']:
for key in ["caret", "start", "end"]:
if key in location:
compiler_warnings.append({
'file': location[key]['file'].lstrip('./'), # Remove leading current directory if present
'line': location[key]['line'],
'column': location[key]['column'],
'message': warning['message'],
})

compiler_warnings.append(
{
"file": location[key]["file"].lstrip(
"./"
), # Remove leading current directory if present
"line": location[key]["line"],
"column": location[key]["column"],
"message": warning["message"],
}
)

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:
file = warning['file']
file = warning["file"]
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(
files_with_expected_warnings: set[str],
files_with_warnings: set[str],
Expand All @@ -95,6 +116,7 @@ def get_unexpected_warnings(

return 0


def get_unexpected_improvements(
files_with_expected_warnings: set[str],
files_with_warnings: set[str],
Expand All @@ -116,38 +138,44 @@ def get_unexpected_improvements(

return 0


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

args = parser.parse_args(argv)
Expand All @@ -156,36 +184,53 @@ def main(argv: list[str] | None = None) -> int:

# Check that the compiler output file is a valid path
if not Path(args.compiler_output_file_path).is_file():
print(f"Compiler output file does not exist: {args.compiler_output_file_path}")
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}")
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()

with Path(args.warning_ignore_file_path).open(encoding="UTF-8") as clean_files:
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("#")
}

if args.compiler_output_type == "json":
warnings = extract_warnings_from_compiler_output_json(compiler_output_file_contents)
warnings = extract_warnings_from_compiler_output_json(
compiler_output_file_contents
)
elif args.compiler_output_type == "clang":
warnings = extract_warnings_from_compiler_output_clang(compiler_output_file_contents)
warnings = extract_warnings_from_compiler_output_clang(
compiler_output_file_contents
)

files_with_warnings = get_warnings_by_file(warnings)

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

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

return exit_code


if __name__ == "__main__":
sys.exit(main())
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
0