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 | 43838b8 | 2023-06-22 20:29:41 +0200 | [diff] [blame] | 25 | from typing import FrozenSet, List, Optional |
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 | |
Pengyu Lv | acbeb7f | 2023-02-06 14:27:30 +0800 | [diff] [blame] | 36 | # Print the file names that will be skipped and the help message |
| 37 | def print_skip(files_to_skip): |
| 38 | print() |
| 39 | print(*files_to_skip, sep=", SKIP\n", end=", SKIP\n") |
Pengyu Lv | c36743f | 2023-02-15 10:20:40 +0800 | [diff] [blame] | 40 | print("Warning: The listed files will be skipped because\n" |
| 41 | "they are not known to git.") |
Pengyu Lv | acbeb7f | 2023-02-06 14:27:30 +0800 | [diff] [blame] | 42 | print() |
| 43 | |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 44 | # Match FILENAME(s) in "check SCRIPT (FILENAME...)" |
| 45 | CHECK_CALL_RE = re.compile(r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)", |
| 46 | re.ASCII) |
| 47 | def list_generated_files() -> FrozenSet[str]: |
| 48 | """Return the names of generated files. |
| 49 | |
| 50 | We don't reformat generated files, since the result might be different |
| 51 | from the output of the generator. Ideally the result of the generator |
| 52 | would conform to the code style, but this would be difficult, especially |
| 53 | with respect to the placement of line breaks in long logical lines. |
| 54 | """ |
| 55 | # Parse check-generated-files.sh to get an up-to-date list of |
| 56 | # generated files. Read the file rather than calling it so that |
| 57 | # this script only depends on Git, Python and uncrustify, and not other |
| 58 | # tools such as sh or grep which might not be available on Windows. |
| 59 | # This introduces a limitation: check-generated-files.sh must have |
| 60 | # the expected format and must list the files explicitly, not through |
| 61 | # wildcards or command substitution. |
| 62 | content = open(CHECK_GENERATED_FILES, encoding="utf-8").read() |
| 63 | checks = re.findall(CHECK_CALL_RE, content) |
| 64 | return frozenset(word for s in checks for word in s.split()) |
| 65 | |
Gilles Peskine | 43838b8 | 2023-06-22 20:29:41 +0200 | [diff] [blame] | 66 | def get_src_files(since: Optional[str]) -> List[str]: |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 67 | """ |
Gilles Peskine | 22eb82c | 2023-06-22 19:45:01 +0200 | [diff] [blame] | 68 | Use git to get a list of the source files. |
| 69 | |
Gilles Peskine | 43838b8 | 2023-06-22 20:29:41 +0200 | [diff] [blame] | 70 | The optional argument since is a commit, indicating to only list files |
| 71 | that have changed since that commit. Without this argument, list all |
| 72 | files known to git. |
| 73 | |
Gilles Peskine | 22eb82c | 2023-06-22 19:45:01 +0200 | [diff] [blame] | 74 | Only C files are included, and certain files (generated, or 3rdparty) |
| 75 | are excluded. |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 76 | """ |
Gilles Peskine | 163ec40 | 2023-06-25 22:18:40 +0200 | [diff] [blame] | 77 | file_patterns = ["*.[hc]", |
| 78 | "tests/suites/*.function", |
| 79 | "scripts/data_files/*.fmt"] |
| 80 | output = subprocess.check_output(["git", "ls-files"] + file_patterns, |
| 81 | universal_newlines=True) |
Gilles Peskine | 22eb82c | 2023-06-22 19:45:01 +0200 | [diff] [blame] | 82 | src_files = output.split() |
Gilles Peskine | 163ec40 | 2023-06-25 22:18:40 +0200 | [diff] [blame] | 83 | if since: |
| 84 | output = subprocess.check_output(["git", "diff", "--name-only", |
| 85 | since, "--"] + |
| 86 | src_files, |
| 87 | universal_newlines=True) |
| 88 | src_files = output.split() |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 89 | |
Gilles Peskine | 22eb82c | 2023-06-22 19:45:01 +0200 | [diff] [blame] | 90 | generated_files = list_generated_files() |
| 91 | # Don't correct style for third-party files (and, for simplicity, |
| 92 | # companion files in the same subtree), or for automatically |
| 93 | # generated files (we're correcting the templates instead). |
| 94 | src_files = [filename for filename in src_files |
| 95 | if not (filename.startswith("3rdparty/") or |
| 96 | filename in generated_files)] |
| 97 | return src_files |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 98 | |
| 99 | def get_uncrustify_version() -> str: |
| 100 | """ |
| 101 | Get the version string from Uncrustify |
| 102 | """ |
David Horstmann | 04bdbe3 | 2023-01-25 11:39:04 +0000 | [diff] [blame] | 103 | result = subprocess.run([UNCRUSTIFY_EXE, "--version"], |
| 104 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 105 | check=False) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 106 | if result.returncode != 0: |
David Horstmann | ca13c4f | 2022-12-08 14:33:52 +0000 | [diff] [blame] | 107 | print_err("Could not get Uncrustify version:", str(result.stderr, "utf-8")) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 108 | return "" |
| 109 | else: |
| 110 | return str(result.stdout, "utf-8") |
| 111 | |
| 112 | def check_style_is_correct(src_file_list: List[str]) -> bool: |
| 113 | """ |
David Horstmann | 9711f4e | 2022-12-08 14:36:10 +0000 | [diff] [blame] | 114 | 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] | 115 | incorrect. |
| 116 | """ |
| 117 | style_correct = True |
| 118 | for src_file in src_file_list: |
| 119 | uncrustify_cmd = [UNCRUSTIFY_EXE] + UNCRUSTIFY_ARGS + [src_file] |
David Horstmann | 04bdbe3 | 2023-01-25 11:39:04 +0000 | [diff] [blame] | 120 | result = subprocess.run(uncrustify_cmd, stdout=subprocess.PIPE, |
| 121 | stderr=subprocess.PIPE, check=False) |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 122 | if result.returncode != 0: |
David Horstmann | 04bdbe3 | 2023-01-25 11:39:04 +0000 | [diff] [blame] | 123 | print_err("Uncrustify returned " + str(result.returncode) + |
| 124 | " correcting file " + src_file) |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 125 | return False |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 126 | |
| 127 | # Uncrustify makes changes to the code and places the result in a new |
| 128 | # file with the extension ".uncrustify". To get the changes (if any) |
| 129 | # simply diff the 2 files. |
David Horstmann | 0ebc12e | 2022-12-08 15:04:20 +0000 | [diff] [blame] | 130 | diff_cmd = ["diff", "-u", src_file, src_file + ".uncrustify"] |
David Horstmann | ce42cc2 | 2023-01-24 18:08:49 +0000 | [diff] [blame] | 131 | cp = subprocess.run(diff_cmd, check=False) |
| 132 | |
| 133 | if cp.returncode == 1: |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 134 | print(src_file + " changed - code style is incorrect.") |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 135 | style_correct = False |
David Horstmann | ce42cc2 | 2023-01-24 18:08:49 +0000 | [diff] [blame] | 136 | elif cp.returncode != 0: |
| 137 | raise subprocess.CalledProcessError(cp.returncode, cp.args, |
| 138 | cp.stdout, cp.stderr) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 139 | |
| 140 | # Tidy up artifact |
David Horstmann | 0ebc12e | 2022-12-08 15:04:20 +0000 | [diff] [blame] | 141 | os.remove(src_file + ".uncrustify") |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 142 | |
| 143 | return style_correct |
| 144 | |
David Horstmann | 8d1d6ed | 2023-01-05 09:59:35 +0000 | [diff] [blame] | 145 | def fix_style_single_pass(src_file_list: List[str]) -> bool: |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 146 | """ |
| 147 | Run Uncrustify once over the source files. |
| 148 | """ |
| 149 | code_change_args = UNCRUSTIFY_ARGS + ["--no-backup"] |
| 150 | for src_file in src_file_list: |
| 151 | uncrustify_cmd = [UNCRUSTIFY_EXE] + code_change_args + [src_file] |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 152 | result = subprocess.run(uncrustify_cmd, check=False) |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 153 | if result.returncode != 0: |
David Horstmann | 04bdbe3 | 2023-01-25 11:39:04 +0000 | [diff] [blame] | 154 | print_err("Uncrustify with file returned: " + |
| 155 | str(result.returncode) + " correcting file " + |
| 156 | src_file) |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 157 | return False |
David Horstmann | 8d1d6ed | 2023-01-05 09:59:35 +0000 | [diff] [blame] | 158 | return True |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 159 | |
| 160 | def fix_style(src_file_list: List[str]) -> int: |
| 161 | """ |
| 162 | Fix the code style. This takes 2 passes of Uncrustify. |
| 163 | """ |
David Horstmann | 78d566b | 2023-01-05 10:02:09 +0000 | [diff] [blame] | 164 | if not fix_style_single_pass(src_file_list): |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 165 | return 1 |
David Horstmann | 78d566b | 2023-01-05 10:02:09 +0000 | [diff] [blame] | 166 | if not fix_style_single_pass(src_file_list): |
David Horstmann | c571c5b | 2023-01-04 18:33:25 +0000 | [diff] [blame] | 167 | return 1 |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 168 | |
| 169 | # Guard against future changes that cause the codebase to require |
| 170 | # more passes. |
| 171 | if not check_style_is_correct(src_file_list): |
David Horstmann | 28d2157 | 2023-01-16 18:32:56 +0000 | [diff] [blame] | 172 | print_err("Code style still incorrect after second run of Uncrustify.") |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 173 | return 1 |
| 174 | else: |
| 175 | return 0 |
| 176 | |
| 177 | def main() -> int: |
| 178 | """ |
| 179 | Main with command line arguments. |
| 180 | """ |
David Horstmann | 2cf779c | 2022-12-08 14:44:36 +0000 | [diff] [blame] | 181 | uncrustify_version = get_uncrustify_version().strip() |
| 182 | if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version: |
Gilles Peskine | 9d34cf3 | 2022-12-23 18:15:19 +0100 | [diff] [blame] | 183 | print("Warning: Using unsupported Uncrustify version '" + |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 184 | uncrustify_version + "'") |
Gilles Peskine | 9d34cf3 | 2022-12-23 18:15:19 +0100 | [diff] [blame] | 185 | print("Note: The only supported version is " + |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 186 | UNCRUSTIFY_SUPPORTED_VERSION) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 187 | |
| 188 | parser = argparse.ArgumentParser() |
Gilles Peskine | 59803db | 2022-12-22 16:34:01 +0100 | [diff] [blame] | 189 | parser.add_argument('-f', '--fix', action='store_true', |
Gilles Peskine | 9d34cf3 | 2022-12-23 18:15:19 +0100 | [diff] [blame] | 190 | help=('modify source files to fix the code style ' |
| 191 | '(default: print diff, do not modify files)')) |
Gilles Peskine | 43838b8 | 2023-06-22 20:29:41 +0200 | [diff] [blame] | 192 | parser.add_argument('-s', '--since', metavar='COMMIT', |
| 193 | help=('only check files modified since the specified commit' |
| 194 | ' (e.g. --since=HEAD~3 or --since=development)')) |
Pengyu Lv | c36743f | 2023-02-15 10:20:40 +0800 | [diff] [blame] | 195 | # --subset is almost useless: it only matters if there are no files |
| 196 | # ('code_style.py' without arguments checks all files known to Git, |
| 197 | # 'code_style.py --subset' does nothing). In particular, |
| 198 | # 'code_style.py --fix --subset ...' is intended as a stable ("porcelain") |
| 199 | # way to restyle a possibly empty set of files. |
Pengyu Lv | 8c6325c | 2023-02-06 14:29:02 +0800 | [diff] [blame] | 200 | parser.add_argument('--subset', action='store_true', |
Pengyu Lv | c36743f | 2023-02-15 10:20:40 +0800 | [diff] [blame] | 201 | help='only check the specified files (default with non-option arguments)') |
Gilles Peskine | 59803db | 2022-12-22 16:34:01 +0100 | [diff] [blame] | 202 | parser.add_argument('operands', nargs='*', metavar='FILE', |
Pengyu Lv | c36743f | 2023-02-15 10:20:40 +0800 | [diff] [blame] | 203 | help='files to check (files MUST be known to git, if none: check all)') |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 204 | |
| 205 | args = parser.parse_args() |
| 206 | |
Gilles Peskine | 43838b8 | 2023-06-22 20:29:41 +0200 | [diff] [blame] | 207 | covered = frozenset(get_src_files(args.since)) |
Pengyu Lv | c36743f | 2023-02-15 10:20:40 +0800 | [diff] [blame] | 208 | # We only check files that are known to git |
| 209 | if args.subset or args.operands: |
Pengyu Lv | e19b51b | 2023-02-14 10:29:53 +0800 | [diff] [blame] | 210 | src_files = [f for f in args.operands if f in covered] |
| 211 | skip_src_files = [f for f in args.operands if f not in covered] |
Pengyu Lv | acbeb7f | 2023-02-06 14:27:30 +0800 | [diff] [blame] | 212 | if skip_src_files: |
| 213 | print_skip(skip_src_files) |
Pengyu Lv | c36743f | 2023-02-15 10:20:40 +0800 | [diff] [blame] | 214 | else: |
Pengyu Lv | 10f4144 | 2023-02-15 16:58:09 +0800 | [diff] [blame] | 215 | src_files = list(covered) |
Gilles Peskine | 59803db | 2022-12-22 16:34:01 +0100 | [diff] [blame] | 216 | |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 217 | if args.fix: |
| 218 | # Fix mode |
| 219 | return fix_style(src_files) |
| 220 | else: |
| 221 | # Check mode |
| 222 | if check_style_is_correct(src_files): |
David Horstmann | 6b3ce30 | 2023-01-24 18:36:41 +0000 | [diff] [blame] | 223 | print("Checked {} files, style ok.".format(len(src_files))) |
David Horstmann | fa928f1 | 2022-11-01 15:46:16 +0000 | [diff] [blame] | 224 | return 0 |
| 225 | else: |
| 226 | return 1 |
| 227 | |
| 228 | if __name__ == '__main__': |
| 229 | sys.exit(main()) |