-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-112301: Compiler warning management tooling #121730
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
Changes from 1 commit
5148727
3bd5a10
6813003
ab4d754
615d228
fc0a60b
7793d80
99715d2
bc44ec2
e2ca75f
bd1634e
b07b1d6
e1954a5
5935143
c19d953
b9b9500
0c0fc31
e568d6c
9f6281e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||||
from pathlib import Path | ||||||
import argparse | ||||||
import json | ||||||
import re | ||||||
|
||||||
def extract_warnings_from_compiler_output(compiler_output: str) -> list[dict]: | ||||||
""" | ||||||
|
@@ -15,26 +16,17 @@ def extract_warnings_from_compiler_output(compiler_output: str) -> list[dict]: | |||||
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. | ||||||
""" | ||||||
compiler_output_json_objects = [] | ||||||
stack = [] | ||||||
start_index = None | ||||||
for index, char in enumerate(compiler_output): | ||||||
if char == '[': | ||||||
if len(stack) == 0: | ||||||
start_index = index # Start of a new JSON array | ||||||
stack.append(char) | ||||||
elif char == ']': | ||||||
if len(stack) > 0: | ||||||
stack.pop() | ||||||
if len(stack) == 0 and start_index is not None: | ||||||
try: | ||||||
json_data = json.loads(compiler_output[start_index:index+1]) | ||||||
compiler_output_json_objects.extend(json_data) | ||||||
start_index = None | ||||||
except json.JSONDecodeError: | ||||||
continue # Skip malformed JSON | ||||||
|
||||||
compiler_warnings = [entry for entry in compiler_output_json_objects if entry.get('kind') == 'warning'] | ||||||
|
||||||
# Regex to find json arrays at the top level of the file in the compiler output | ||||||
json_arrays = re.findall(r'\[(?:[^\[\]]|\[(?:[^\[\]]|\[[^\[\]]*\])*\])*\]', compiler_output) | ||||||
sethmlarson marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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']) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please could you limit lines to 79 chars? https://peps.python.org/pep-0008/#maximum-line-length This file is in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Broke this line into two |
||||||
except json.JSONDecodeError: | ||||||
continue # Skip malformed JSON | ||||||
sethmlarson marked this conversation as resolved.
Show resolved
Hide resolved
sethmlarson marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
return compiler_warnings | ||||||
|
||||||
|
@@ -52,8 +44,9 @@ def get_unexpected_warnings( | |||||
for location in locations: | ||||||
for key in ['caret', 'start', 'end']: | ||||||
if key in location: | ||||||
filename = location[key]['file'] | ||||||
if filename not in files_with_expected_warnings: | ||||||
file = location[key]['file'] | ||||||
file = file.lstrip('./') # Remove leading curdir if present | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed comment |
||||||
if file not in files_with_expected_warnings: | ||||||
unexpected_warnings.append(warning) | ||||||
|
||||||
if unexpected_warnings: | ||||||
|
@@ -81,18 +74,19 @@ def get_unexpected_improvements( | |||||
for location in locations: | ||||||
for key in ['caret', 'start', 'end']: | ||||||
if key in location: | ||||||
filename = location[key]['file'] | ||||||
files_with_warnings.add(filename) | ||||||
file = location[key]['file'] | ||||||
file = file.lstrip('./') # Remove leading curdir if present | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed comment |
||||||
files_with_warnings.add(file) | ||||||
|
||||||
unexpected_improvements = [] | ||||||
for filename in files_with_expected_warnings: | ||||||
if filename not in files_with_warnings: | ||||||
unexpected_improvements.append(filename) | ||||||
for file in files_with_expected_warnings: | ||||||
if file not in files_with_warnings: | ||||||
unexpected_improvements.append(file) | ||||||
|
||||||
if unexpected_improvements: | ||||||
print("Unexpected improvements:") | ||||||
for filename in unexpected_improvements: | ||||||
print(filename) | ||||||
for file in unexpected_improvements: | ||||||
print(file) | ||||||
return 1 | ||||||
|
||||||
return 0 | ||||||
|
@@ -143,22 +137,19 @@ def main(argv: list[str] | None = None) -> int: | |||||
|
||||||
with Path(args.warning_ignore_file_path).open(encoding="UTF-8") as clean_files: | ||||||
files_with_expected_warnings = { | ||||||
filename.strip() | ||||||
for filename in clean_files | ||||||
if filename.strip() and not filename.startswith("#") | ||||||
file.strip() | ||||||
for file in clean_files | ||||||
if file.strip() and not file.startswith("#") | ||||||
} | ||||||
|
||||||
if not len(compiler_output_file_contents) > 0:exit_code = 1 | ||||||
|
||||||
warnings = extract_warnings_from_compiler_output(compiler_output_file_contents) | ||||||
|
||||||
status = get_unexpected_warnings(warnings, files_with_expected_warnings) | ||||||
if args.fail_on_regression: exit_code |= status | ||||||
sethmlarson marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
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__": | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks almost good to merge, just some style nits :)
Technically we're outside the stdlib so strictly speaking PEP 8 doesn't apply, but let's follow it anyway. For example, please could you add extra newlines before these defs and wrap things to 79 chars?
The easiest way is to run Black (which we don't run on the stdlib but again we're outside the stdlib :)
Then some comments and long strings will need manual wrapping.
Also, please don't force push in this repo, it makes it easier to review new commits, and we squash merge everything at the end anyway. I think force-pushing is sometimes to blame for pinging lots of unrelated CODEOWNERS too.
https://devguide.python.org/getting-started/pull-request-lifecycle/#quick-guide
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to adhere to PEP8.
After a rebase there were several commits that came from branches from different forks. Not really sure how that happened but I had to revert to my last commit and force push to get rid of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I just pushed a commit to also wrap comments and strings to 79 chars as well.