2022-11-01 16:46:16 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Check or fix the code style by running Uncrustify.
|
2023-01-16 19:28:21 +01:00
|
|
|
|
|
|
|
This script must be run from the root of a Git work tree containing Mbed TLS.
|
2022-11-01 16:46:16 +01:00
|
|
|
"""
|
|
|
|
# Copyright The Mbed TLS Contributors
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
import argparse
|
|
|
|
import os
|
2022-12-19 00:48:58 +01:00
|
|
|
import re
|
2022-11-01 16:46:16 +01:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2022-12-19 00:48:58 +01:00
|
|
|
from typing import FrozenSet, List
|
2022-11-01 16:46:16 +01:00
|
|
|
|
2022-12-08 15:44:36 +01:00
|
|
|
UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1"
|
2022-12-08 18:03:01 +01:00
|
|
|
CONFIG_FILE = ".uncrustify.cfg"
|
2022-11-01 16:46:16 +01:00
|
|
|
UNCRUSTIFY_EXE = "uncrustify"
|
|
|
|
UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE]
|
2022-12-19 00:48:58 +01:00
|
|
|
CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh"
|
2022-11-01 16:46:16 +01:00
|
|
|
|
2022-12-08 15:33:52 +01:00
|
|
|
def print_err(*args):
|
2023-01-24 19:36:41 +01:00
|
|
|
print("Error: ", *args, file=sys.stderr)
|
2022-12-08 15:33:52 +01:00
|
|
|
|
2022-12-19 00:48:58 +01:00
|
|
|
# Match FILENAME(s) in "check SCRIPT (FILENAME...)"
|
|
|
|
CHECK_CALL_RE = re.compile(r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)",
|
|
|
|
re.ASCII)
|
|
|
|
def list_generated_files() -> FrozenSet[str]:
|
|
|
|
"""Return the names of generated files.
|
|
|
|
|
|
|
|
We don't reformat generated files, since the result might be different
|
|
|
|
from the output of the generator. Ideally the result of the generator
|
|
|
|
would conform to the code style, but this would be difficult, especially
|
|
|
|
with respect to the placement of line breaks in long logical lines.
|
|
|
|
"""
|
|
|
|
# Parse check-generated-files.sh to get an up-to-date list of
|
|
|
|
# generated files. Read the file rather than calling it so that
|
|
|
|
# this script only depends on Git, Python and uncrustify, and not other
|
|
|
|
# tools such as sh or grep which might not be available on Windows.
|
|
|
|
# This introduces a limitation: check-generated-files.sh must have
|
|
|
|
# the expected format and must list the files explicitly, not through
|
|
|
|
# wildcards or command substitution.
|
|
|
|
content = open(CHECK_GENERATED_FILES, encoding="utf-8").read()
|
|
|
|
checks = re.findall(CHECK_CALL_RE, content)
|
|
|
|
return frozenset(word for s in checks for word in s.split())
|
|
|
|
|
2022-11-01 16:46:16 +01:00
|
|
|
def get_src_files() -> List[str]:
|
|
|
|
"""
|
|
|
|
Use git ls-files to get a list of the source files
|
|
|
|
"""
|
2022-12-08 14:12:21 +01:00
|
|
|
git_ls_files_cmd = ["git", "ls-files",
|
2022-12-08 18:38:27 +01:00
|
|
|
"*.[hc]",
|
|
|
|
"tests/suites/*.function",
|
|
|
|
"scripts/data_files/*.fmt"]
|
2022-11-01 16:46:16 +01:00
|
|
|
|
2023-01-24 19:36:41 +01:00
|
|
|
result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE,
|
|
|
|
check=False)
|
2022-11-01 16:46:16 +01:00
|
|
|
|
|
|
|
if result.returncode != 0:
|
2022-12-08 16:04:20 +01:00
|
|
|
print_err("git ls-files returned: " + str(result.returncode))
|
2022-11-01 16:46:16 +01:00
|
|
|
return []
|
|
|
|
else:
|
2022-12-19 00:48:58 +01:00
|
|
|
generated_files = list_generated_files()
|
2022-11-01 16:46:16 +01:00
|
|
|
src_files = str(result.stdout, "utf-8").split()
|
2022-12-19 00:48:58 +01:00
|
|
|
# Don't correct style for third-party files (and, for simplicity,
|
|
|
|
# companion files in the same subtree), or for automatically
|
|
|
|
# generated files (we're correcting the templates instead).
|
|
|
|
src_files = [filename for filename in src_files
|
|
|
|
if not (filename.startswith("3rdparty/") or
|
|
|
|
filename in generated_files)]
|
2022-11-01 16:46:16 +01:00
|
|
|
return src_files
|
|
|
|
|
|
|
|
def get_uncrustify_version() -> str:
|
|
|
|
"""
|
|
|
|
Get the version string from Uncrustify
|
|
|
|
"""
|
2023-01-25 12:39:04 +01:00
|
|
|
result = subprocess.run([UNCRUSTIFY_EXE, "--version"],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
|
|
check=False)
|
2022-11-01 16:46:16 +01:00
|
|
|
if result.returncode != 0:
|
2022-12-08 15:33:52 +01:00
|
|
|
print_err("Could not get Uncrustify version:", str(result.stderr, "utf-8"))
|
2022-11-01 16:46:16 +01:00
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
return str(result.stdout, "utf-8")
|
|
|
|
|
|
|
|
def check_style_is_correct(src_file_list: List[str]) -> bool:
|
|
|
|
"""
|
2022-12-08 15:36:10 +01:00
|
|
|
Check the code style and output a diff for each file whose style is
|
2022-11-01 16:46:16 +01:00
|
|
|
incorrect.
|
|
|
|
"""
|
|
|
|
style_correct = True
|
|
|
|
for src_file in src_file_list:
|
|
|
|
uncrustify_cmd = [UNCRUSTIFY_EXE] + UNCRUSTIFY_ARGS + [src_file]
|
2023-01-25 12:39:04 +01:00
|
|
|
result = subprocess.run(uncrustify_cmd, stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE, check=False)
|
2023-01-04 19:33:25 +01:00
|
|
|
if result.returncode != 0:
|
2023-01-25 12:39:04 +01:00
|
|
|
print_err("Uncrustify returned " + str(result.returncode) +
|
|
|
|
" correcting file " + src_file)
|
2023-01-04 19:33:25 +01:00
|
|
|
return False
|
2022-11-01 16:46:16 +01:00
|
|
|
|
|
|
|
# Uncrustify makes changes to the code and places the result in a new
|
|
|
|
# file with the extension ".uncrustify". To get the changes (if any)
|
|
|
|
# simply diff the 2 files.
|
2022-12-08 16:04:20 +01:00
|
|
|
diff_cmd = ["diff", "-u", src_file, src_file + ".uncrustify"]
|
2023-01-24 19:08:49 +01:00
|
|
|
cp = subprocess.run(diff_cmd, check=False)
|
|
|
|
|
|
|
|
if cp.returncode == 1:
|
2023-01-24 19:36:41 +01:00
|
|
|
print(src_file + " changed - code style is incorrect.")
|
2022-11-01 16:46:16 +01:00
|
|
|
style_correct = False
|
2023-01-24 19:08:49 +01:00
|
|
|
elif cp.returncode != 0:
|
|
|
|
raise subprocess.CalledProcessError(cp.returncode, cp.args,
|
|
|
|
cp.stdout, cp.stderr)
|
2022-11-01 16:46:16 +01:00
|
|
|
|
|
|
|
# Tidy up artifact
|
2022-12-08 16:04:20 +01:00
|
|
|
os.remove(src_file + ".uncrustify")
|
2022-11-01 16:46:16 +01:00
|
|
|
|
|
|
|
return style_correct
|
|
|
|
|
2023-01-05 10:59:35 +01:00
|
|
|
def fix_style_single_pass(src_file_list: List[str]) -> bool:
|
2022-11-01 16:46:16 +01:00
|
|
|
"""
|
|
|
|
Run Uncrustify once over the source files.
|
|
|
|
"""
|
|
|
|
code_change_args = UNCRUSTIFY_ARGS + ["--no-backup"]
|
|
|
|
for src_file in src_file_list:
|
|
|
|
uncrustify_cmd = [UNCRUSTIFY_EXE] + code_change_args + [src_file]
|
2023-01-24 19:36:41 +01:00
|
|
|
result = subprocess.run(uncrustify_cmd, check=False)
|
2023-01-04 19:33:25 +01:00
|
|
|
if result.returncode != 0:
|
2023-01-25 12:39:04 +01:00
|
|
|
print_err("Uncrustify with file returned: " +
|
|
|
|
str(result.returncode) + " correcting file " +
|
|
|
|
src_file)
|
2023-01-04 19:33:25 +01:00
|
|
|
return False
|
2023-01-05 10:59:35 +01:00
|
|
|
return True
|
2022-11-01 16:46:16 +01:00
|
|
|
|
|
|
|
def fix_style(src_file_list: List[str]) -> int:
|
|
|
|
"""
|
|
|
|
Fix the code style. This takes 2 passes of Uncrustify.
|
|
|
|
"""
|
2023-01-05 11:02:09 +01:00
|
|
|
if not fix_style_single_pass(src_file_list):
|
2023-01-04 19:33:25 +01:00
|
|
|
return 1
|
2023-01-05 11:02:09 +01:00
|
|
|
if not fix_style_single_pass(src_file_list):
|
2023-01-04 19:33:25 +01:00
|
|
|
return 1
|
2022-11-01 16:46:16 +01:00
|
|
|
|
|
|
|
# Guard against future changes that cause the codebase to require
|
|
|
|
# more passes.
|
|
|
|
if not check_style_is_correct(src_file_list):
|
2023-01-16 19:32:56 +01:00
|
|
|
print_err("Code style still incorrect after second run of Uncrustify.")
|
2022-11-01 16:46:16 +01:00
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def main() -> int:
|
|
|
|
"""
|
|
|
|
Main with command line arguments.
|
|
|
|
"""
|
2022-12-08 15:44:36 +01:00
|
|
|
uncrustify_version = get_uncrustify_version().strip()
|
|
|
|
if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version:
|
2022-12-23 18:15:19 +01:00
|
|
|
print("Warning: Using unsupported Uncrustify version '" +
|
2023-01-24 19:36:41 +01:00
|
|
|
uncrustify_version + "'")
|
2022-12-23 18:15:19 +01:00
|
|
|
print("Note: The only supported version is " +
|
2023-01-24 19:36:41 +01:00
|
|
|
UNCRUSTIFY_SUPPORTED_VERSION)
|
2022-11-01 16:46:16 +01:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2022-12-22 16:34:01 +01:00
|
|
|
parser.add_argument('-f', '--fix', action='store_true',
|
2022-12-23 18:15:19 +01:00
|
|
|
help=('modify source files to fix the code style '
|
|
|
|
'(default: print diff, do not modify files)'))
|
2022-12-22 16:34:01 +01:00
|
|
|
# --files is almost useless: it only matters if there are no files
|
|
|
|
# ('code_style.py' without arguments checks all files known to Git,
|
2022-12-23 18:15:19 +01:00
|
|
|
# 'code_style.py --files' does nothing). In particular,
|
2023-01-12 15:45:32 +01:00
|
|
|
# 'code_style.py --fix --files ...' is intended as a stable ("porcelain")
|
|
|
|
# way to restyle a possibly empty set of files.
|
2022-12-22 16:34:01 +01:00
|
|
|
parser.add_argument('--files', action='store_true',
|
|
|
|
help='only check the specified files (default with non-option arguments)')
|
|
|
|
parser.add_argument('operands', nargs='*', metavar='FILE',
|
|
|
|
help='files to check (if none: check files that are known to git)')
|
2022-11-01 16:46:16 +01:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-12-22 16:34:01 +01:00
|
|
|
if args.files or args.operands:
|
|
|
|
src_files = args.operands
|
|
|
|
else:
|
|
|
|
src_files = get_src_files()
|
|
|
|
|
2022-11-01 16:46:16 +01:00
|
|
|
if args.fix:
|
|
|
|
# Fix mode
|
|
|
|
return fix_style(src_files)
|
|
|
|
else:
|
|
|
|
# Check mode
|
|
|
|
if check_style_is_correct(src_files):
|
2023-01-24 19:36:41 +01:00
|
|
|
print("Checked {} files, style ok.".format(len(src_files)))
|
2022-11-01 16:46:16 +01:00
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|