-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Uncrustify warnings #8522
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
Closed
gilles-peskine-arm
wants to merge
4
commits into
Mbed-TLS:development
from
gilles-peskine-arm:uncrustify-warnings
+38
−30
Closed
Uncrustify warnings #8522
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8a29ec8
Make a wrong uncrustify version a fatal error
gilles-peskine-arm 67c682c
Allow specifying a diffent uncrustify command
gilles-peskine-arm 03d003f
Hide spurious error message if uncrustify is not present
gilles-peskine-arm 0819854
Also apply --uncrustify to --fix mode
gilles-peskine-arm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,27 +88,30 @@ def get_src_files(since: Optional[str]) -> List[str]: | |
| filename in generated_files)] | ||
| return src_files | ||
|
|
||
| def get_uncrustify_version() -> str: | ||
| def get_uncrustify_version(uncrustify_exe: str) -> str: | ||
| """ | ||
| Get the version string from Uncrustify | ||
| """ | ||
| result = subprocess.run([UNCRUSTIFY_EXE, "--version"], | ||
| stdout=subprocess.PIPE, stderr=subprocess.PIPE, | ||
| check=False) | ||
| if result.returncode != 0: | ||
| print_err("Could not get Uncrustify version:", str(result.stderr, "utf-8")) | ||
| return "" | ||
| else: | ||
| return str(result.stdout, "utf-8") | ||
| Get the version string from Uncrustify. | ||
|
|
||
| def check_style_is_correct(src_file_list: List[str]) -> bool: | ||
| Return an empty string if Uncrustify is not found. | ||
| """ | ||
| try: | ||
| output = subprocess.check_output([uncrustify_exe, "--version"], | ||
| stderr=subprocess.PIPE) | ||
| return str(output, "utf-8").strip() | ||
| except FileNotFoundError: | ||
| sys.stderr.write('Fatal: command {} not found in PATH.\n' | ||
| .format(uncrustify_exe)) | ||
| return '' | ||
|
|
||
| def check_style_is_correct(uncrustify_exe: str, | ||
| src_file_list: List[str]) -> bool: | ||
| """ | ||
| Check the code style and output a diff for each file whose style is | ||
| incorrect. | ||
| """ | ||
| style_correct = True | ||
| for src_file in src_file_list: | ||
| uncrustify_cmd = [UNCRUSTIFY_EXE] + UNCRUSTIFY_ARGS + [src_file] | ||
| uncrustify_cmd = [uncrustify_exe] + UNCRUSTIFY_ARGS + [src_file] | ||
| result = subprocess.run(uncrustify_cmd, stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, check=False) | ||
| if result.returncode != 0: | ||
|
|
@@ -149,7 +152,7 @@ def fix_style_single_pass(src_file_list: List[str]) -> bool: | |
| return False | ||
| return True | ||
|
|
||
| def fix_style(src_file_list: List[str]) -> int: | ||
| def fix_style(uncrustify_exe: str, src_file_list: List[str]) -> int: | ||
| """ | ||
| Fix the code style. This takes 2 passes of Uncrustify. | ||
| """ | ||
|
|
@@ -160,7 +163,7 @@ def fix_style(src_file_list: List[str]) -> int: | |
|
|
||
| # Guard against future changes that cause the codebase to require | ||
| # more passes. | ||
| if not check_style_is_correct(src_file_list): | ||
| if not check_style_is_correct(uncrustify_exe, src_file_list): | ||
| print_err("Code style still incorrect after second run of Uncrustify.") | ||
| return 1 | ||
| else: | ||
|
|
@@ -170,13 +173,6 @@ def main() -> int: | |
| """ | ||
| Main with command line arguments. | ||
| """ | ||
| uncrustify_version = get_uncrustify_version().strip() | ||
| if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version: | ||
| print("Warning: Using unsupported Uncrustify version '" + | ||
| uncrustify_version + "'") | ||
| print("Note: The only supported version is " + | ||
| UNCRUSTIFY_SUPPORTED_VERSION) | ||
|
|
||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('-f', '--fix', action='store_true', | ||
| help=('modify source files to fix the code style ' | ||
|
|
@@ -192,11 +188,23 @@ def main() -> int: | |
| # way to restyle a possibly empty set of files. | ||
| parser.add_argument('--subset', action='store_true', | ||
| help='only check the specified files (default with non-option arguments)') | ||
| parser.add_argument('--uncrustify', | ||
| default='uncrustify', | ||
| help='uncrustify command to run (default: uncrustify)') | ||
| parser.add_argument('operands', nargs='*', metavar='FILE', | ||
| help='files to check (files MUST be known to git, if none: check all)') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| uncrustify_version = get_uncrustify_version(args.uncrustify) | ||
| if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version: | ||
| if uncrustify_version != '': | ||
| sys.stderr.write('Fatal: wrong uncrustify version ({}).\n' | ||
| .format(uncrustify_version)) | ||
| sys.stderr.write('You need uncrustify {} for correct results.\n' | ||
| .format(UNCRUSTIFY_SUPPORTED_VERSION)) | ||
| return 2 | ||
|
|
||
| covered = frozenset(get_src_files(args.since)) | ||
| # We only check files that are known to git | ||
| if args.subset or args.operands: | ||
|
|
@@ -209,10 +217,10 @@ def main() -> int: | |
|
|
||
| if args.fix: | ||
| # Fix mode | ||
| return fix_style(src_files) | ||
| return fix_style(args.uncrustify, src_files) | ||
|
Contributor
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.
Contributor
Author
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.
|
||
| else: | ||
| # Check mode | ||
| if check_style_is_correct(src_files): | ||
| if check_style_is_correct(args.uncrustify, src_files): | ||
| print("Checked {} files, style ok.".format(len(src_files))) | ||
| return 0 | ||
| else: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is a very nice practise.