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