From 7ab2fbfbd651a9e30bf7d3e536647a947409a6dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20C=C5=93ur?= Date: Sat, 14 May 2022 07:48:33 +0800 Subject: [PATCH] Adopting .clang-format-include and .clang-format-ignore (#3047) --- .clang-format-ignore | 22 ++++++++++++++++++++++ .clang-format-include | 5 +++++ code_style.sh | 35 ++++++++++++++++------------------- 3 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 .clang-format-ignore create mode 100644 .clang-format-include diff --git a/.clang-format-ignore b/.clang-format-ignore new file mode 100644 index 000000000..53c1970f7 --- /dev/null +++ b/.clang-format-ignore @@ -0,0 +1,22 @@ +# Skipped files must be justified as untracked or third-party maintained + +# untracked files +.*/* +build/* +libtransmission/version.h +web/node_modules/* + +# third-party maintained projects +third-party/* + +# maintained at https://github.com/sparkle-project/Sparkle +macosx/Sparkle.framework/* + +# maintained at https://github.com/bdkjones/VDKQueue +macosx/VDKQueue/* + +# maintained at https://github.com/mnunberg/jsonsl +libtransmission/jsonsl.* + +# missing official maintainer (XConsortium: wildmat.c,v 1.2 94/04/13); TODO: clang-format it +libtransmission/wildmat.* diff --git a/.clang-format-include b/.clang-format-include new file mode 100644 index 000000000..e20e8c4c6 --- /dev/null +++ b/.clang-format-include @@ -0,0 +1,5 @@ +*.c +*.cc +*.h +*.m +*.mm diff --git a/code_style.sh b/code_style.sh index 5dd18fa6a..9ff28d562 100755 --- a/code_style.sh +++ b/code_style.sh @@ -5,6 +5,12 @@ set -o noglob +# PATH workaround for SourceTree +# for Intel Mac +PATH="${PATH}:/usr/local/bin" +# for Apple Silicon Mac +PATH="${PATH}:/opt/homebrew/bin" + if [[ "x$1" == *"check"* ]]; then echo "checking code format" else @@ -15,24 +21,13 @@ root="$(dirname "$0")" root="$(cd "${root}" && pwd)" cd "${root}" || exit 1 -cfile_includes=( - '*.c' - '*.cc' - '*.h' - '*.m' - '*.mm' -) -cfile_excludes=( - 'build/*' - 'libtransmission/ConvertUTF.*' - 'libtransmission/jsonsl.*' - 'libtransmission/wildmat.*' - 'macosx/Sparkle.framework/*' - 'macosx/VDKQueue/*' - 'third-party/*' - 'web/*' - '.git/*' -) +trim_comments() { + # 1. remove comments, ignoring backslash-escaped characters + # 2. trim spaces + # 3. remove empty lines + # note: GNU extensions like +?| aren't supported on macOS + sed 's/^\(\(\(\\.\)*[^\\#]*\)*\)#.*/\1/;s/^[[:blank:]]*//;s/[[:blank:]]*$//;/^$/d' "$@" +} get_find_path_args() { local args=$(printf " -o -path ./%s" "$@") @@ -40,7 +35,9 @@ get_find_path_args() { } find_cfiles() { - find . \( $(get_find_path_args "${cfile_includes[@]}") \) ! \( $(get_find_path_args "${cfile_excludes[@]}") \) "$@" + # We use the same files as Meson: https://mesonbuild.com/Code-formatting.html + find . \( $(get_find_path_args $(trim_comments .clang-format-include)) \)\ + ! \( $(get_find_path_args $(trim_comments .clang-format-ignore)) \) "$@" } # We're targeting clang-format version 12 and other versions give slightly