David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | """Check or fix the code style by running Uncrustify. |
David Horstmann | 8b5a449 | 2023-01-16 18:28:21 +0000 | [diff] [blame] | 3 | |
| 4 | This script must be run from the root of a Git work tree containing Mbed TLS. |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 5 | """ |
| 6 | # Copyright The Mbed TLS Contributors |
| 7 | # SPDX-License-Identifier: Apache-2.0 |
| 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. |
| 11 | # You may obtain a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | # See the License for the specific language governing permissions and |
| 19 | # limitations under the License. |
| 20 | import argparse |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 21 | import os |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 22 | import re |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 23 | import subprocess |
| 24 | import sys |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 25 | from typing import FrozenSet, List |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 26 | |
David Horstmann | 2cf779c | 2022-12-08 14:44:36 +0000 | [diff] [blame] | 27 | UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1" |
David Horstmann | ae93a3f | 2022-12-08 17:03:01 +0000 | [diff] [blame] | 28 | CONFIG_FILE = ".uncrustify.cfg" |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 29 | UNCRUSTIFY_EXE = "uncrustify" |
| 30 | UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE] |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 31 | CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh" |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 32 | |
David Horstmann | ca13c4f | 2022-12-08 14:33:52 +0000 | [diff] [blame] | 33 | def print_err(*args): |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 34 | print("Error: ", *args, file=sys.stderr) |
David Horstmann | ca13c4f | 2022-12-08 14:33:52 +0000 | [diff] [blame] | 35 | |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 36 | # Match FILENAME(s) in "check SCRIPT (FILENAME...)" |
| 37 | CHECK_CALL_RE = re.compile(r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)", |
| 38 | re.ASCII) |
| 39 | def list_generated_files() -> FrozenSet[str]: |
| 40 | """Return the names of generated files. |
| 41 | |
| 42 | We don't reformat generated files, since the result might be different |
| 43 | from the output of the generator. Ideally the result of the generator |
| 44 | would conform to the code style, but this would be difficult, especially |
| 45 | with respect to the placement of line breaks in long logical lines. |
| 46 | """ |
| 47 | # Parse check-generated-files.sh to get an up-to-date list of |
| 48 | # generated files. Read the file rather than calling it so that |
| 49 | # this script only depends on Git, Python and uncrustify, and not other |
| 50 | # tools such as sh or grep which might not be available on Windows. |
| 51 | # This introduces a limitation: check-generated-files.sh must have |
| 52 | # the expected format and must list the files explicitly, not through |
| 53 | # wildcards or command substitution. |
| 54 | content = open(CHECK_GENERATED_FILES, encoding="utf-8").read() |
| 55 | checks = re.findall(CHECK_CALL_RE, content) |
| 56 | return frozenset(word for s in checks for word in s.split()) |
| 57 | |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 58 | def get_src_files() -> List[str]: |
| 59 | """ |
| 60 | Use git ls-files to get a list of the source files |
| 61 | """ |
David Horstmann | b7dab41 | 2022-12-08 13:12:21 +0000 | [diff] [blame] | 62 | git_ls_files_cmd = ["git", "ls-files", |
David Horstmann | c6b604e | 2022-12-08 17:38:27 +0000 | [diff] [blame] | 63 | "*.[hc]", |
| 64 | "tests/suites/*.function", |
| 65 | "scripts/data_files/*.fmt"] |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 66 | |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 67 | result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE, |
| 68 | check=False) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 69 | |
| 70 | if result.returncode != 0: |
David Horstmann | 0ebc12e | 2022-12-08 15:04:20 +0000 | [diff] [blame] | 71 | print_err("git ls-files returned: " + str(result.returncode)) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 72 | return [] |
| 73 | else: |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 74 | generated_files = list_generated_files() |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 75 | src_files = str(result.stdout, "utf-8").split() |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 76 | # Don't correct style for third-party files (and, for simplicity, |
| 77 | # companion files in the same subtree), or for automatically |
| 78 | # generated files (we're correcting the templates instead). |
| 79 | src_files = [filename for filename in src_files |
| 80 | if not (filename.startswith("3rdparty/") or |
| 81 | filename in generated_files)] |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 82 | return src_files |
| 83 | |
| 84 | def get_uncrustify_version() -> str: |
| 85 | """ |
| 86 | Get the version string from Uncrustify |
| 87 | """ |
David Horstmann | 04bdbe3 | 2023-01-25 11:39:04 +0000 | [diff] [blame] | 88 | result = subprocess.run([UNCRUSTIFY_EXE, "--version"], |
| 89 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 90 | check=False) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 91 | if result.returncode != 0: |
David Horstmann | ca13c4f | 2022-12-08 14:33:52 +0000 | [diff] [blame] | 92 | print_err("Could not get Uncrustify version:", str(result.stderr, "utf-8")) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 93 | return "" |
| 94 | else: |
| 95 | return str(result.stdout, "utf-8") |
| 96 | |
| 97 | def check_style_is_correct(src_file_list: List[str]) -> bool: |
| 98 | """ |
David Horstmann | 9711f4e | 2022-12-08 14:36:10 +0000 | [diff] [blame] | 99 | Check the code style and output a diff for each file whose style is |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 100 | incorrect. |
| 101 | """ |
| 102 | style_correct = True |
| 103 | for src_file in src_file_list: |
| 104 | uncrustify_cmd = [UNCRUSTIFY_EXE] + UNCRUSTIFY_ARGS + [src_file] |
David Horstmann | 04bdbe3 | 2023-01-25 11:39:04 +0000 | [diff] [blame] | 105 | result = subprocess.run(uncrustify_cmd, stdout=subprocess.PIPE, |
| 106 | stderr=subprocess.PIPE, check=False) |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 107 | if result.returncode != 0: |
David Horstmann | 04bdbe3 | 2023-01-25 11:39:04 +0000 | [diff] [blame] | 108 | print_err("Uncrustify returned " + str(result.returncode) + |
| 109 | " correcting file " + src_file) |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 110 | return False |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 111 | |
| 112 | # Uncrustify makes changes to the code and places the result in a new |
| 113 | # file with the extension ".uncrustify". To get the changes (if any) |
| 114 | # simply diff the 2 files. |
David Horstmann | 0ebc12e | 2022-12-08 15:04:20 +0000 | [diff] [blame] | 115 | diff_cmd = ["diff", "-u", src_file, src_file + ".uncrustify"] |
David Horstmann | ce42cc2 | 2023-01-24 18:08:49 +0000 | [diff] [blame] | 116 | cp = subprocess.run(diff_cmd, check=False) |
| 117 | |
| 118 | if cp.returncode == 1: |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 119 | print(src_file + " changed - code style is incorrect.") |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 120 | style_correct = False |
David Horstmann | ce42cc2 | 2023-01-24 18:08:49 +0000 | [diff] [blame] | 121 | elif cp.returncode != 0: |
| 122 | raise subprocess.CalledProcessError(cp.returncode, cp.args, |
| 123 | cp.stdout, cp.stderr) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 124 | |
| 125 | # Tidy up artifact |
David Horstmann | 0ebc12e | 2022-12-08 15:04:20 +0000 | [diff] [blame] | 126 | os.remove(src_file + ".uncrustify") |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 127 | |
| 128 | return style_correct |
| 129 | |
David Horstmann | 8d1d6ed | 2023-01-05 09:59:35 +0000 | [diff] [blame] | 130 | def fix_style_single_pass(src_file_list: List[str]) -> bool: |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 131 | """ |
| 132 | Run Uncrustify once over the source files. |
| 133 | """ |
| 134 | code_change_args = UNCRUSTIFY_ARGS + ["--no-backup"] |
| 135 | for src_file in src_file_list: |
| 136 | uncrustify_cmd = [UNCRUSTIFY_EXE] + code_change_args + [src_file] |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 137 | result = subprocess.run(uncrustify_cmd, check=False) |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 138 | if result.returncode != 0: |
David Horstmann | 04bdbe3 | 2023-01-25 11:39:04 +0000 | [diff] [blame] | 139 | print_err("Uncrustify with file returned: " + |
| 140 | str(result.returncode) + " correcting file " + |
| 141 | src_file) |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 142 | return False |
David Horstmann | 8d1d6ed | 2023-01-05 09:59:35 +0000 | [diff] [blame] | 143 | return True |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 144 | |
| 145 | def fix_style(src_file_list: List[str]) -> int: |
| 146 | """ |
| 147 | Fix the code style. This takes 2 passes of Uncrustify. |
| 148 | """ |
David Horstmann | 78d566b | 2023-01-05 10:02:09 +0000 | [diff] [blame] | 149 | if not fix_style_single_pass(src_file_list): |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 150 | return 1 |
David Horstmann | 78d566b | 2023-01-05 10:02:09 +0000 | [diff] [blame] | 151 | if not fix_style_single_pass(src_file_list): |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 152 | return 1 |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 153 | |
| 154 | # Guard against future changes that cause the codebase to require |
| 155 | # more passes. |
| 156 | if not check_style_is_correct(src_file_list): |
David Horstmann | 28d2157 | 2023-01-16 18:32:56 +0000 | [diff] [blame] | 157 | print_err("Code style still incorrect after second run of Uncrustify.") |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 158 | return 1 |
| 159 | else: |
| 160 | return 0 |
| 161 | |
| 162 | def main() -> int: |
| 163 | """ |
| 164 | Main with command line arguments. |
| 165 | """ |
David Horstmann | 2cf779c | 2022-12-08 14:44:36 +0000 | [diff] [blame] | 166 | uncrustify_version = get_uncrustify_version().strip() |
| 167 | if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version: |
Gilles Peskine | 9d34cf3 | 2022-12-23 18:15:19 +0100 | [diff] [blame] | 168 | print("Warning: Using unsupported Uncrustify version '" + |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 169 | uncrustify_version + "'") |
Gilles Peskine | 9d34cf3 | 2022-12-23 18:15:19 +0100 | [diff] [blame] | 170 | print("Note: The only supported version is " + |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 171 | UNCRUSTIFY_SUPPORTED_VERSION) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 172 | |
| 173 | parser = argparse.ArgumentParser() |
Gilles Peskine | 59803db | 2022-12-22 16:34:01 +0100 | [diff] [blame] | 174 | parser.add_argument('-f', '--fix', action='store_true', |
Gilles Peskine | 9d34cf3 | 2022-12-23 18:15:19 +0100 | [diff] [blame] | 175 | help=('modify source files to fix the code style ' |
| 176 | '(default: print diff, do not modify files)')) |
Pengyu Lv | 8c6325c | 2023-02-06 14:29:02 +0800 | [diff] [blame^] | 177 | parser.add_argument('--subset', action='store_true', |
| 178 | help=('check a subset of the files known to git ' |
| 179 | '(default: empty FILE means full set)')) |
Gilles Peskine | 59803db | 2022-12-22 16:34:01 +0100 | [diff] [blame] | 180 | parser.add_argument('operands', nargs='*', metavar='FILE', |
Pengyu Lv | 8c6325c | 2023-02-06 14:29:02 +0800 | [diff] [blame^] | 181 | help='files to check') |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 182 | |
| 183 | args = parser.parse_args() |
| 184 | |
Pengyu Lv | 8c6325c | 2023-02-06 14:29:02 +0800 | [diff] [blame^] | 185 | all_src_files = get_src_files() |
| 186 | src_files = args.operands if args.operands else all_src_files |
| 187 | if args.subset: |
| 188 | # We are to check a subset of the default list |
| 189 | src_files = [f for f in args.operands if f in all_src_files] |
Gilles Peskine | 59803db | 2022-12-22 16:34:01 +0100 | [diff] [blame] | 190 | |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 191 | if args.fix: |
| 192 | # Fix mode |
| 193 | return fix_style(src_files) |
| 194 | else: |
| 195 | # Check mode |
| 196 | if check_style_is_correct(src_files): |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 197 | print("Checked {} files, style ok.".format(len(src_files))) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 198 | return 0 |
| 199 | else: |
| 200 | return 1 |
| 201 | |
| 202 | if __name__ == '__main__': |
| 203 | sys.exit(main()) |