From 5b98d73864b189349dacaaf07c16207e72bcf3a2 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 10 Nov 2022 18:55:00 +0000 Subject: [PATCH 01/18] Linewrap for the benefit of Uncrustify The last line of programs/psa/key_ladder_demo.c is of the following form: #endif /* Very long comment ... */ Uncrustify tries to reduce the length: #endif \ /* Very long comment ... */ and causes a compiler error as there is a continuation line with no actual code in it. Work around this by linewrapping the comment in advance. Signed-off-by: David Horstmann --- programs/psa/key_ladder_demo.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/programs/psa/key_ladder_demo.c b/programs/psa/key_ladder_demo.c index 13037190dd..f40874e1c0 100644 --- a/programs/psa/key_ladder_demo.c +++ b/programs/psa/key_ladder_demo.c @@ -713,4 +713,6 @@ usage_failure: usage( ); return( EXIT_FAILURE ); } -#endif /* MBEDTLS_SHA256_C && MBEDTLS_MD_C && MBEDTLS_AES_C && MBEDTLS_CCM_C && MBEDTLS_PSA_CRYPTO_C && MBEDTLS_FS_IO */ +#endif /* MBEDTLS_SHA256_C && MBEDTLS_MD_C && + MBEDTLS_AES_C && MBEDTLS_CCM_C && + MBEDTLS_PSA_CRYPTO_C && MBEDTLS_FS_IO */ From e6cb118d56c234ed662c98b36ca768e51fcad210 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Fri, 7 Oct 2022 19:27:43 +0100 Subject: [PATCH 02/18] Add configuration file for Uncrustify Signed-off-by: David Horstmann --- codestyle.cfg | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 codestyle.cfg diff --git a/codestyle.cfg b/codestyle.cfg new file mode 100644 index 0000000000..f4b47b246a --- /dev/null +++ b/codestyle.cfg @@ -0,0 +1,252 @@ +# 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. + +# Configuration options for Mbed TLS code style + +# Line length options + +# Wrap lines at 100 characters +code_width = 100 + +# Allow splitting long for statements between the condition statements +ls_for_split_full = true + +# Allow splitting function calls between arguments +ls_func_split_full = true + +input_tab_size = 4 + +# Spaces-only indentation +indent_with_tabs = 0 + +indent_columns = 4 + +# Indent 'case' 1 level from 'switch' +indent_switch_case = indent_columns + +# Line-up strings broken by '\' +indent_align_string = true + +# Braces on the same line (Egyptian-style braces) +nl_enum_brace = remove +nl_union_brace = remove +nl_struct_brace = remove +nl_do_brace = remove +nl_if_brace = remove +nl_for_brace = remove +nl_else_brace = remove # Note: this option may cause problems for macro-guarded if-else partials +nl_while_brace = remove +nl_switch_brace = remove + +# Braces on same line as keywords that follow them - 'else' and the 'while' in 'do {} while ()'; +nl_brace_else = remove +nl_brace_while = remove +# Space before else on the same line +sp_brace_else = add +# If else is on the same line as '{', force exactly 1 space between them +sp_else_brace = force + +# Functions are the exception and have braces on the next line +nl_fcall_brace = add +nl_fdef_brace = add + +# Force exactly one space between ')' and '{' in statements +sp_sparen_brace = force + +# At least 1 space around assignment +sp_assign = add + +# Remove spaces around the preprocessor '##' token-concatenate +sp_pp_concat = ignore + +# At least 1 space around '||' and '&&' +sp_bool = add + +# But no space after the '!' operator +sp_not = remove + +# No space after the bitwise-not '~' operator +sp_inv = remove + +# No space after the addressof '&' operator +sp_addr = remove + +# No space around the member '.' and '->' operators +sp_member = remove + +# No space after the dereference '*' operator +sp_deref = remove + +# No space after a unary negation '-' +sp_sign = remove + +# No space between the '++'/'--' operator and its operand +sp_incdec = remove + +# At least 1 space around comparison operators +sp_compare = add + +# Remove spaces inside all kinds of parentheses: + +# Remove spaces inside parentheses +sp_inside_paren = remove + +# No spaces inside statement parentheses +sp_inside_sparen = remove + +# No spaces inside cast parentheses '( char )x' -> '(char)x' +sp_inside_paren_cast = remove + +# No spaces inside function parentheses +sp_inside_fparen = remove +# (The case where the function has no parameters/arguments) +sp_inside_fparens = remove + +# No spaces inside the first parentheses in a function type +sp_inside_tparen = remove + +# (Uncrustify >= 0.74.0) No spaces inside parens in for statements +sp_inside_for = remove + +# Remove spaces between nested parentheses '( (' -> '((' +sp_paren_paren = remove +# (Uncrustify >= 0.74.0) +sp_sparen_paren = remove + +# Remove spaces between ')' and adjacent '(' +sp_cparen_oparen = remove + +# (Uncrustify >= 0.73.0) space between 'do' and '{' +sp_do_brace_open = force + +# (Uncrustify >= 0.73.0) space between '}' and 'while' +sp_brace_close_while = force + +# At least 1 space before a '*' pointer star +sp_before_ptr_star = add + +# Remove spaces between pointer stars +sp_between_ptr_star = remove + +# No space after a pointer star +sp_after_ptr_star = remove + +# But allow a space in the case of e.g. char * const x; +sp_after_ptr_star_qualifier = ignore + +# Remove space after star in a function return type +sp_after_ptr_star_func = remove + +# At least 1 space after a type in variable definition etc +sp_after_type = add + +# Force exactly 1 space between a statement keyword (e.g. 'if') and an opening parenthesis +sp_before_sparen = force + +# Remove a space before a ';' +sp_before_semi = remove +# (Uncrustify >= 0.73.0) Remove space before a semi in a non-empty for +sp_before_semi_for = remove +# (Uncrustify >= 0.73.0) Remove space in empty first statement of a for +sp_before_semi_for_empty = remove +# (Uncrustify >= 0.74.0) Remove space in empty middle statement of a for +sp_between_semi_for_empty = remove + +# Add a space after a ';' (unless a comment follows) +sp_after_semi = add +# (Uncrustify >= 0.73.0) Add a space after a semi in non-empty for statements +sp_after_semi_for = add +# (Uncrustify >= 0.73.0) No space after final semi in empty for statements +sp_after_semi_for_empty = remove + +# Remove spaces on the inside of square brackets '[]' +sp_inside_square = remove + +# Must have at least 1 space after a comma +sp_after_comma = add + +# Must not have a space before a comma +sp_before_comma = remove + +# No space before the ':' in a case statement +sp_before_case_colon = remove + +# No space after a cast - '(char) x' -> '(char)x' +sp_after_cast = remove + +# No space between 'sizeof' and '(' +sp_sizeof_paren = remove + +# At least 1 space inside '{ }' +sp_inside_braces = add + +# At least 1 space inside '{ }' in an enum +sp_inside_braces_enum = add + +# At least 1 space inside '{ }' in a struct +sp_inside_braces_struct = add + +# Except for empty braces which should not have a space + +# At least 1 space between a function return type and the function name +sp_type_func = add + +# No space between a function name and its arguments/parameters +sp_func_proto_paren = remove +sp_func_def_paren = remove +sp_func_call_paren = remove + +# No space between '__attribute__' and '(' +sp_attribute_paren = remove + +# No space between 'defined' and '(' in preprocessor conditions +sp_defined_paren = remove + +# At least 1 space between a macro's name and its definition +sp_macro = add +sp_macro_func = add + +# Force exactly 1 space between a '}' and the name of a typedef if on the same line +sp_brace_typedef = force + +# At least 1 space before a '\' line continuation +sp_before_nl_cont = add + +# At least 1 space around '?' and ':' in ternary statements +sp_cond_colon = add +sp_cond_question = add + +# Space between #else/#endif and comment afterwards +sp_endif_cmt = add + +# Remove newlines at the start of a file +nl_start_of_file = remove + +# At least 1 newline at the end of a file +nl_end_of_file = add +nl_end_of_file_min = 1 + +# Add braces in single-line statements +mod_full_brace_do = add +mod_full_brace_for = add +mod_full_brace_if = add +mod_full_brace_while = add + +# Remove parentheses from return statements +mod_paren_on_return = remove + +# Disable removal of leading spaces in a multi-line comment if the first and +# last lines are the same length +cmt_multi_check_last = false From fa928f1c44a79cf050527f7f2ff24098d08bd044 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Tue, 1 Nov 2022 15:46:16 +0000 Subject: [PATCH 03/18] Add script to run Uncrustify Signed-off-by: David Horstmann --- scripts/code_style.py | 153 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100755 scripts/code_style.py diff --git a/scripts/code_style.py b/scripts/code_style.py new file mode 100755 index 0000000000..7923db451e --- /dev/null +++ b/scripts/code_style.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python3 +"""Check or fix the code style by running Uncrustify. +""" +# 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 io +import os +import subprocess +import sys +from typing import List + +CONFIG_FILE = "codestyle.cfg" +UNCRUSTIFY_EXE = "uncrustify" +UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE] +STDOUT_UTF8 = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') +STDERR_UTF8 = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') + +def get_src_files() -> List[str]: + """ + Use git ls-files to get a list of the source files + """ + git_ls_files_cmd = ["git", "ls-files", \ + "*.[hc]", \ + "tests/suites/*.function", \ + "scripts/data_files/*.fmt"] + + result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE, \ + stderr=STDERR_UTF8, check=False) + + if result.returncode != 0: + print("Error: git ls-files returned: "+str(result.returncode), \ + file=STDERR_UTF8) + return [] + else: + src_files = str(result.stdout, "utf-8").split() + # Don't correct style for files in 3rdparty/ + src_files = list(filter( \ + lambda filename: not filename.startswith("3rdparty/"), \ + src_files)) + return src_files + +def get_uncrustify_version() -> str: + """ + Get the version string from Uncrustify + """ + version_args = ["--version"] + result = subprocess.run([UNCRUSTIFY_EXE] + version_args, \ + stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) + if result.returncode != 0: + print("Error getting version: "+str(result.stderr, "utf-8"), \ + file=STDERR_UTF8) + return "" + else: + return str(result.stdout, "utf-8") + +def check_style_is_correct(src_file_list: List[str]) -> bool: + """ + Check the code style and output a diff foir each file whose style is + incorrect. + """ + style_correct = True + for src_file in src_file_list: + uncrustify_cmd = [UNCRUSTIFY_EXE] + UNCRUSTIFY_ARGS + [src_file] + subprocess.run(uncrustify_cmd, stdout=subprocess.PIPE, \ + stderr=subprocess.PIPE, check=False) + + # 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. + diff_cmd = ["diff", "-u", src_file, src_file+".uncrustify"] + result = subprocess.run(diff_cmd, stdout=subprocess.PIPE, \ + stderr=STDERR_UTF8, check=False) + if len(result.stdout) > 0: + print(src_file+" - Incorrect code style.", file=STDOUT_UTF8) + print("File changed - diff:", file=STDOUT_UTF8) + print(str(result.stdout, "utf-8"), file=STDOUT_UTF8) + style_correct = False + else: + print(src_file+" - OK.", file=STDOUT_UTF8) + + # Tidy up artifact + os.remove(src_file+".uncrustify") + + return style_correct + +def fix_style_single_pass(src_file_list: List[str]) -> None: + """ + 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] + subprocess.run(uncrustify_cmd, check=False, stdout=STDOUT_UTF8, \ + stderr=STDERR_UTF8) + +def fix_style(src_file_list: List[str]) -> int: + """ + Fix the code style. This takes 2 passes of Uncrustify. + """ + fix_style_single_pass(src_file_list) + fix_style_single_pass(src_file_list) + + # Guard against future changes that cause the codebase to require + # more passes. + if not check_style_is_correct(src_file_list): + print("Error: Code style still incorrect after second run of Uncrustify.", \ + file=STDERR_UTF8) + return 1 + else: + return 0 + +def main() -> int: + """ + Main with command line arguments. + """ + uncrustify_version = get_uncrustify_version() + if "0.75.1" not in uncrustify_version: + print("Warning: Using unsupported Uncrustify version '" \ + + uncrustify_version + "'", file=STDOUT_UTF8) + + src_files = get_src_files() + + parser = argparse.ArgumentParser() + parser.add_argument('-f', '--fix', action='store_true', \ + help='modify source files to fix the code style') + + args = parser.parse_args() + + if args.fix: + # Fix mode + return fix_style(src_files) + else: + # Check mode + if check_style_is_correct(src_files): + return 0 + else: + return 1 + +if __name__ == '__main__': + sys.exit(main()) From 1b84781184cb35c6dee9e79615c92ad642355ca0 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Mon, 14 Nov 2022 15:40:46 +0000 Subject: [PATCH 04/18] Disable code style correction in check_config.h Code style correction currently messes up check_names.py Signed-off-by: David Horstmann --- include/mbedtls/check_config.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/mbedtls/check_config.h b/include/mbedtls/check_config.h index d36db4a9ec..90246fe874 100644 --- a/include/mbedtls/check_config.h +++ b/include/mbedtls/check_config.h @@ -23,6 +23,7 @@ #ifndef MBEDTLS_CHECK_CONFIG_H #define MBEDTLS_CHECK_CONFIG_H +/* *INDENT-OFF* */ /* * We assume CHAR_BIT is 8 in many places. In practice, this is true on our * target platforms, so not an issue, but let's just be extra sure. @@ -1069,4 +1070,5 @@ */ typedef int mbedtls_iso_c_forbids_empty_translation_units; +/* *INDENT-ON* */ #endif /* MBEDTLS_CHECK_CONFIG_H */ From 6e5bc033117255503782c979fe322d13c82ddae6 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 1 Dec 2022 15:49:03 +0000 Subject: [PATCH 05/18] Add ChangeLog entry for code style script Signed-off-by: David Horstmann --- ChangeLog.d/add_code_style_correction_script.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 ChangeLog.d/add_code_style_correction_script.txt diff --git a/ChangeLog.d/add_code_style_correction_script.txt b/ChangeLog.d/add_code_style_correction_script.txt new file mode 100644 index 0000000000..32ec767d84 --- /dev/null +++ b/ChangeLog.d/add_code_style_correction_script.txt @@ -0,0 +1,3 @@ +Changes + * Add a config file for Uncrustify and a script to correct the code style + using Uncrustify. From 92b5ac1a4788762686928a3d2b23f42cbba09ab8 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 10 Nov 2022 18:30:52 +0000 Subject: [PATCH 06/18] Add all.sh component to test with code style Run the main test suites after running code style correction to check that code style correction does not break these tests. Signed-off-by: David Horstmann --- tests/scripts/all.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 9295c9d00f..028248228b 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -3537,6 +3537,26 @@ support_test_psa_compliance () { [ "$ver_major" -eq 3 ] && [ "$ver_minor" -ge 10 ] } +component_test_corrected_code_style () { + ./scripts/code_style.py --fix + + msg "build: make, default config (out-of-box), corrected code style" + make + + msg "test: main suites make, default config (out-of-box), corrected code style" + make test + + # Clean up code-style corrections + git checkout -- . +} + +support_test_corrected_code_style() { + case $(uncrustify --version) in + *0.75.1*) true;; + *) false;; + esac +} + component_check_python_files () { msg "Lint: Python scripts" tests/scripts/check-python-files.sh From b7dab4131575b17e6c4b1f51c332d3b8dac271f9 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 13:12:21 +0000 Subject: [PATCH 07/18] Miscellaneous improvements to code style script Signed-off-by: David Horstmann --- scripts/code_style.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 7923db451e..7de223a166 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -32,9 +32,9 @@ def get_src_files() -> List[str]: """ Use git ls-files to get a list of the source files """ - git_ls_files_cmd = ["git", "ls-files", \ - "*.[hc]", \ - "tests/suites/*.function", \ + git_ls_files_cmd = ["git", "ls-files", + "*.[hc]", + "tests/suites/*.function", "scripts/data_files/*.fmt"] result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE, \ @@ -56,8 +56,7 @@ def get_uncrustify_version() -> str: """ Get the version string from Uncrustify """ - version_args = ["--version"] - result = subprocess.run([UNCRUSTIFY_EXE] + version_args, \ + result = subprocess.run([UNCRUSTIFY_EXE, "--version"], \ stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) if result.returncode != 0: print("Error getting version: "+str(result.stderr, "utf-8"), \ From ca13c4f59f1f85e152786f341a58dc07443c034e Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 14:33:52 +0000 Subject: [PATCH 08/18] Use helper function for error printing Signed-off-by: David Horstmann --- scripts/code_style.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 7de223a166..068298aec6 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -28,6 +28,9 @@ UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE] STDOUT_UTF8 = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') STDERR_UTF8 = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') +def print_err(*args): + print("Error: ", *args, file=STDERR_UTF8) + def get_src_files() -> List[str]: """ Use git ls-files to get a list of the source files @@ -41,8 +44,7 @@ def get_src_files() -> List[str]: stderr=STDERR_UTF8, check=False) if result.returncode != 0: - print("Error: git ls-files returned: "+str(result.returncode), \ - file=STDERR_UTF8) + print_err("git ls-files returned: "+str(result.returncode)) return [] else: src_files = str(result.stdout, "utf-8").split() @@ -59,8 +61,7 @@ def get_uncrustify_version() -> str: result = subprocess.run([UNCRUSTIFY_EXE, "--version"], \ stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) if result.returncode != 0: - print("Error getting version: "+str(result.stderr, "utf-8"), \ - file=STDERR_UTF8) + print_err("Could not get Uncrustify version:", str(result.stderr, "utf-8")) return "" else: return str(result.stdout, "utf-8") @@ -115,8 +116,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): - print("Error: Code style still incorrect after second run of Uncrustify.", \ - file=STDERR_UTF8) + print("Code style still incorrect after second run of Uncrustify.") return 1 else: return 0 From 9711f4eeb4e8453b626c50ee3f3f92f4a44e0bc4 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 14:36:10 +0000 Subject: [PATCH 09/18] Fix typo in code style script Signed-off-by: David Horstmann --- scripts/code_style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 068298aec6..be333f093f 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -68,7 +68,7 @@ def get_uncrustify_version() -> str: def check_style_is_correct(src_file_list: List[str]) -> bool: """ - Check the code style and output a diff foir each file whose style is + Check the code style and output a diff for each file whose style is incorrect. """ style_correct = True From 2cf779ceff908b1e7bb54cb609f5e7d35e48d33e Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 14:44:36 +0000 Subject: [PATCH 10/18] Use constant for supported Uncrustify version Define and report the supported Uncrustify version (and remove extra newlines from version output). Signed-off-by: David Horstmann --- scripts/code_style.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index be333f093f..4aa9a40a0f 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -22,6 +22,7 @@ import subprocess import sys from typing import List +UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1" CONFIG_FILE = "codestyle.cfg" UNCRUSTIFY_EXE = "uncrustify" UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE] @@ -125,10 +126,11 @@ def main() -> int: """ Main with command line arguments. """ - uncrustify_version = get_uncrustify_version() - if "0.75.1" not in uncrustify_version: + uncrustify_version = get_uncrustify_version().strip() + if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version: print("Warning: Using unsupported Uncrustify version '" \ - + uncrustify_version + "'", file=STDOUT_UTF8) + + uncrustify_version + "' (Note: The only supported version" \ + "is " + UNCRUSTIFY_SUPPORTED_VERSION + ")", file=STDOUT_UTF8) src_files = get_src_files() From b488ca34971f0856ae47d4de8d01bff4fca422a2 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 14:53:15 +0000 Subject: [PATCH 11/18] Remove ChangeLog entry, style is not yet official Signed-off-by: David Horstmann --- ChangeLog.d/add_code_style_correction_script.txt | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 ChangeLog.d/add_code_style_correction_script.txt diff --git a/ChangeLog.d/add_code_style_correction_script.txt b/ChangeLog.d/add_code_style_correction_script.txt deleted file mode 100644 index 32ec767d84..0000000000 --- a/ChangeLog.d/add_code_style_correction_script.txt +++ /dev/null @@ -1,3 +0,0 @@ -Changes - * Add a config file for Uncrustify and a script to correct the code style - using Uncrustify. From 190041d2b1684f635e8efd921beb59f6bc2ceaa5 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 14:56:18 +0000 Subject: [PATCH 12/18] Explain that the script is only for the future Signed-off-by: David Horstmann --- scripts/code_style.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/code_style.py b/scripts/code_style.py index 4aa9a40a0f..f93df7b6f1 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 """Check or fix the code style by running Uncrustify. + +Note: The code style enforced by this script is not yet introduced to +Mbed TLS. At present this script will only be used to prepare for a future +change of code style. """ # Copyright The Mbed TLS Contributors # SPDX-License-Identifier: Apache-2.0 From 0e648f041c2f38b311bbd1fd11f3ee833a4a826e Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 14:57:28 +0000 Subject: [PATCH 13/18] Rename Uncrustify configuration Make it more obvious that this is an Uncrustify configuration file. Signed-off-by: David Horstmann --- codestyle.cfg => .uncrustify.cfg | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename codestyle.cfg => .uncrustify.cfg (100%) diff --git a/codestyle.cfg b/.uncrustify.cfg similarity index 100% rename from codestyle.cfg rename to .uncrustify.cfg From be88e80e3d9296bff748677cb46c75dcaf5fd055 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 15:00:20 +0000 Subject: [PATCH 14/18] Improvements to the top of the code style config Signed-off-by: David Horstmann --- .uncrustify.cfg | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.uncrustify.cfg b/.uncrustify.cfg index f4b47b246a..ce3ee75c45 100644 --- a/.uncrustify.cfg +++ b/.uncrustify.cfg @@ -1,3 +1,8 @@ +# Configuration options for Uncrustify specifying the Mbed TLS code style. +# +# Note: The code style represented by this file has not yet been introduced +# to Mbed TLS. +# # Copyright The Mbed TLS Contributors # SPDX-License-Identifier: Apache-2.0 # @@ -13,7 +18,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Configuration options for Mbed TLS code style # Line length options From d021032d51977dc42d27ff3d112b2f848cbb6416 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 15:02:14 +0000 Subject: [PATCH 15/18] Remove legacy comments from config Signed-off-by: David Horstmann --- .uncrustify.cfg | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.uncrustify.cfg b/.uncrustify.cfg index ce3ee75c45..ac9173e18d 100644 --- a/.uncrustify.cfg +++ b/.uncrustify.cfg @@ -50,7 +50,7 @@ nl_struct_brace = remove nl_do_brace = remove nl_if_brace = remove nl_for_brace = remove -nl_else_brace = remove # Note: this option may cause problems for macro-guarded if-else partials +nl_else_brace = remove nl_while_brace = remove nl_switch_brace = remove @@ -202,8 +202,6 @@ sp_inside_braces_enum = add # At least 1 space inside '{ }' in a struct sp_inside_braces_struct = add -# Except for empty braces which should not have a space - # At least 1 space between a function return type and the function name sp_type_func = add From 0ebc12ea41c051b3c58140efd8f26f2f478cd5fa Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 15:04:20 +0000 Subject: [PATCH 16/18] Add spaces around '+' Signed-off-by: David Horstmann --- scripts/code_style.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index f93df7b6f1..5884848564 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -49,7 +49,7 @@ def get_src_files() -> List[str]: stderr=STDERR_UTF8, check=False) if result.returncode != 0: - print_err("git ls-files returned: "+str(result.returncode)) + print_err("git ls-files returned: " + str(result.returncode)) return [] else: src_files = str(result.stdout, "utf-8").split() @@ -85,19 +85,19 @@ def check_style_is_correct(src_file_list: List[str]) -> bool: # 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. - diff_cmd = ["diff", "-u", src_file, src_file+".uncrustify"] + diff_cmd = ["diff", "-u", src_file, src_file + ".uncrustify"] result = subprocess.run(diff_cmd, stdout=subprocess.PIPE, \ stderr=STDERR_UTF8, check=False) if len(result.stdout) > 0: - print(src_file+" - Incorrect code style.", file=STDOUT_UTF8) + print(src_file + " - Incorrect code style.", file=STDOUT_UTF8) print("File changed - diff:", file=STDOUT_UTF8) print(str(result.stdout, "utf-8"), file=STDOUT_UTF8) style_correct = False else: - print(src_file+" - OK.", file=STDOUT_UTF8) + print(src_file + " - OK.", file=STDOUT_UTF8) # Tidy up artifact - os.remove(src_file+".uncrustify") + os.remove(src_file + ".uncrustify") return style_correct From ae93a3f98468fc93c22ac3964bd657a59415eabe Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 17:03:01 +0000 Subject: [PATCH 17/18] Fixup: Config file name in code style script Signed-off-by: David Horstmann --- scripts/code_style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 5884848564..ebf0cd48e2 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -27,7 +27,7 @@ import sys from typing import List UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1" -CONFIG_FILE = "codestyle.cfg" +CONFIG_FILE = ".uncrustify.cfg" UNCRUSTIFY_EXE = "uncrustify" UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE] STDOUT_UTF8 = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') From c6b604e842af5da4cc5135cdcc49a07eb38ebfe0 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 8 Dec 2022 17:38:27 +0000 Subject: [PATCH 18/18] Reindent line continuations for pylint Signed-off-by: David Horstmann --- scripts/code_style.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index ebf0cd48e2..68cd556200 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -41,9 +41,9 @@ def get_src_files() -> List[str]: Use git ls-files to get a list of the source files """ git_ls_files_cmd = ["git", "ls-files", - "*.[hc]", - "tests/suites/*.function", - "scripts/data_files/*.fmt"] + "*.[hc]", + "tests/suites/*.function", + "scripts/data_files/*.fmt"] result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE, \ stderr=STDERR_UTF8, check=False)